(doc) Add more doc to SocketServer

This commit is contained in:
Benjamin Sergeant 2019-04-17 20:31:34 -07:00
parent d486c72e02
commit 52f460f66d
9 changed files with 34 additions and 20 deletions

View File

@ -155,7 +155,13 @@ namespace ix
_connectionStateFactory = connectionStateFactory; _connectionStateFactory = connectionStateFactory;
} }
//
// join the threads for connections that have been closed // join the threads for connections that have been closed
//
// When a connection is closed by a client, the connection state terminated
// field becomes true, and we can use that to know that we can join that thread
// and remove it from our _connectionsThreads data structure (a list).
//
void SocketServer::closeTerminatedThreads() void SocketServer::closeTerminatedThreads()
{ {
auto it = _connectionsThreads.begin(); auto it = _connectionsThreads.begin();

View File

@ -25,6 +25,7 @@ namespace ix
public: public:
using ConnectionStateFactory = std::function<std::shared_ptr<ConnectionState>()>; using ConnectionStateFactory = std::function<std::shared_ptr<ConnectionState>()>;
// Each connection is handled by its own worker thread.
// We use a list as we only care about remove and append operations. // We use a list as we only care about remove and append operations.
using ConnectionThreads = std::list<std::pair<std::shared_ptr<ConnectionState>, using ConnectionThreads = std::list<std::pair<std::shared_ptr<ConnectionState>,
std::thread>>; std::thread>>;
@ -36,6 +37,9 @@ namespace ix
virtual ~SocketServer(); virtual ~SocketServer();
virtual void stop(); virtual void stop();
// It is possible to override ConnectionState through inheritance
// this method allows user to change the factory by returning an object
// that inherits from ConnectionState but has its own methods.
void setConnectionStateFactory(const ConnectionStateFactory& connectionStateFactory); void setConnectionStateFactory(const ConnectionStateFactory& connectionStateFactory);
const static int kDefaultPort; const static int kDefaultPort;
@ -65,15 +69,19 @@ namespace ix
std::mutex _logMutex; std::mutex _logMutex;
// background thread to wait for incoming connections
std::atomic<bool> _stop; std::atomic<bool> _stop;
std::thread _thread; std::thread _thread;
// the list of (connectionState, threads) for each connections
ConnectionThreads _connectionsThreads; ConnectionThreads _connectionsThreads;
// used to have the main control thread for a server
// wait for a 'terminate' notification without busy polling
std::condition_variable _conditionVariable; std::condition_variable _conditionVariable;
std::mutex _conditionVariableMutex; std::mutex _conditionVariableMutex;
// // the factory to create ConnectionState objects
ConnectionStateFactory _connectionStateFactory; ConnectionStateFactory _connectionStateFactory;
// Methods // Methods