Cleaned up Listener code

Now inherits a 'Listener' class, instead of Socket.
This commit is contained in:
Fred Nicolson
2017-03-31 12:15:13 +01:00
parent 4512bd49e7
commit e15afb3d87
6 changed files with 50 additions and 36 deletions
+32
View 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
+4 -22
View File
@@ -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;}
};
}
+4 -11
View File
@@ -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;}
};
}