Cleaned up Listener code
Now inherits a 'Listener' class, instead of Socket.
This commit is contained in:
parent
4512bd49e7
commit
e15afb3d87
@ -17,7 +17,7 @@ endif()
|
||||
set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" )
|
||||
set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" )
|
||||
|
||||
set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpSocket.cpp include/frnetlib/HttpSocket.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h)
|
||||
set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpSocket.cpp include/frnetlib/HttpSocket.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
|
||||
32
include/frnetlib/Listener.h
Normal file
32
include/frnetlib/Listener.h
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by fred.nicolson on 3/31/17.
|
||||
//
|
||||
|
||||
#ifndef FRNETLIB_LISTENER_H
|
||||
#define FRNETLIB_LISTENER_H
|
||||
#include "Socket.h"
|
||||
|
||||
namespace fr
|
||||
{
|
||||
class Listener
|
||||
{
|
||||
/*!
|
||||
* Listens to the given port for connections
|
||||
*
|
||||
* @param port The port to bind to
|
||||
* @return If the operation was successful
|
||||
*/
|
||||
virtual Socket::Status listen(const std::string &port)=0;
|
||||
|
||||
/*!
|
||||
* Accepts a new connection.
|
||||
*
|
||||
* @param client Where to store the connection information
|
||||
* @return True on success. False on failure.
|
||||
*/
|
||||
virtual Socket::Status accept(Socket &client)=0;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //FRNETLIB_LISTENER_H
|
||||
@ -15,13 +15,13 @@
|
||||
#include <mbedtls/error.h>
|
||||
#include <mbedtls/certs.h>
|
||||
|
||||
#include "TcpListener.h"
|
||||
#include "SSLSocket.h"
|
||||
#include "Listener.h"
|
||||
|
||||
|
||||
namespace fr
|
||||
{
|
||||
class SSLListener : public Socket
|
||||
class SSLListener : public Listener
|
||||
{
|
||||
public:
|
||||
SSLListener(std::shared_ptr<SSLContext> ssl_context, const std::string &crt_path, const std::string &pem_path, const std::string &private_key_path) noexcept;
|
||||
@ -34,7 +34,7 @@ namespace fr
|
||||
* @param port The port to bind to
|
||||
* @return If the operation was successful
|
||||
*/
|
||||
virtual Socket::Status listen(const std::string &port);
|
||||
virtual Socket::Status listen(const std::string &port) override;
|
||||
|
||||
/*!
|
||||
* Accepts a new connection.
|
||||
@ -42,19 +42,7 @@ namespace fr
|
||||
* @param client Where to store the connection information
|
||||
* @return True on success. False on failure.
|
||||
*/
|
||||
virtual Socket::Status accept(SSLSocket &client);
|
||||
|
||||
/*!
|
||||
* Enables or disables blocking on the socket.
|
||||
*
|
||||
* @param should_block True to block, false otherwise.
|
||||
*/
|
||||
virtual void set_blocking(bool should_block) override {abort();}; //Not implemented
|
||||
|
||||
virtual int32_t get_socket_descriptor() const override
|
||||
{
|
||||
return listen_fd.fd;
|
||||
}
|
||||
virtual Socket::Status accept(Socket &client) override;
|
||||
|
||||
private:
|
||||
mbedtls_net_context listen_fd;
|
||||
@ -63,12 +51,6 @@ namespace fr
|
||||
mbedtls_pk_context pkey;
|
||||
|
||||
std::shared_ptr<SSLContext> ssl_context;
|
||||
|
||||
//Stubs
|
||||
virtual void close_socket(){}
|
||||
virtual Socket::Status connect(const std::string &address, const std::string &port){return Socket::Error;}
|
||||
virtual Status send_raw(const char *data, size_t size) {return Socket::Error;}
|
||||
virtual Status receive_raw(void *data, size_t data_size, size_t &received) {return Socket::Error;}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -8,11 +8,12 @@
|
||||
#include "TcpSocket.h"
|
||||
#include "Socket.h"
|
||||
#include "NetworkEncoding.h"
|
||||
#include "Listener.h"
|
||||
|
||||
namespace fr
|
||||
{
|
||||
|
||||
class TcpListener : public Socket
|
||||
class TcpListener : public Listener
|
||||
{
|
||||
public:
|
||||
TcpListener() noexcept = default;
|
||||
@ -25,7 +26,7 @@ public:
|
||||
* @param port The port to bind to
|
||||
* @return If the operation was successful
|
||||
*/
|
||||
virtual Socket::Status listen(const std::string &port);
|
||||
virtual Socket::Status listen(const std::string &port) override;
|
||||
|
||||
/*!
|
||||
* Accepts a new connection.
|
||||
@ -33,18 +34,10 @@ public:
|
||||
* @param client Where to store the connection information
|
||||
* @return True on success. False on failure.
|
||||
*/
|
||||
virtual Socket::Status accept(TcpSocket &client);
|
||||
virtual Socket::Status accept(Socket &client) override;
|
||||
|
||||
private:
|
||||
int32_t socket_descriptor;
|
||||
|
||||
//Stubs
|
||||
virtual void close_socket(){}
|
||||
virtual Socket::Status connect(const std::string &address, const std::string &port){return Socket::Error;}
|
||||
virtual void set_blocking(bool val){}
|
||||
virtual fr::Socket::Status send_raw(const char*, size_t){return Socket::Error;}
|
||||
virtual fr::Socket::Status receive_raw(void*, size_t, size_t&){return Socket::Error;}
|
||||
virtual int32_t get_socket_descriptor() const {return socket_descriptor;}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -77,8 +77,12 @@ namespace fr
|
||||
return Socket::Success;
|
||||
}
|
||||
|
||||
Socket::Status SSLListener::accept(SSLSocket &client)
|
||||
Socket::Status SSLListener::accept(Socket &client_)
|
||||
{
|
||||
//Cast to SSLSocket. Will throw bad cast on failure.
|
||||
SSLSocket &client = dynamic_cast<SSLSocket&>(client_);
|
||||
|
||||
//Initialise mbedtls
|
||||
int error = 0;
|
||||
std::unique_ptr<mbedtls_ssl_context> ssl(new mbedtls_ssl_context);
|
||||
mbedtls_ssl_init(ssl.get());
|
||||
|
||||
@ -66,8 +66,11 @@ namespace fr
|
||||
return Socket::Success;
|
||||
}
|
||||
|
||||
Socket::Status TcpListener::accept(TcpSocket &client)
|
||||
Socket::Status TcpListener::accept(Socket &client_)
|
||||
{
|
||||
//Cast to TcpSocket. Will throw bad cast on failure.
|
||||
TcpSocket &client = dynamic_cast<TcpSocket&>(client_);
|
||||
|
||||
//Prepare to wait for the client
|
||||
sockaddr_storage client_addr;
|
||||
int client_descriptor;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user