(socket server) add a callback to the ConnectionState to be invoked when the connection is terminated. This will be used by the SocketServer in the future to know on time that the associated connection thread can be terminated.

This commit is contained in:
Benjamin Sergeant 2020-08-15 16:03:40 -07:00
parent 261095fa12
commit 785842de03
6 changed files with 33 additions and 3 deletions

View File

@ -2,6 +2,10 @@
All changes to this project will be documented in this file. All changes to this project will be documented in this file.
## [10.2.2] - 2020-08-15
(socket server) add a callback to the ConnectionState to be invoked when the connection is terminated. This will be used by the SocketServer in the future to know on time that the associated connection thread can be terminated.
## [10.2.1] - 2020-08-15 ## [10.2.1] - 2020-08-15
(socket server) do not create a select interrupt object everytime when polling for notifications while waiting for new connections, instead use a persistent one which is a member variable (socket server) do not create a select interrupt object everytime when polling for notifications while waiting for new connections, instead use a persistent one which is a member variable

View File

@ -31,6 +31,11 @@ namespace ix
return std::make_shared<ConnectionState>(); return std::make_shared<ConnectionState>();
} }
void ConnectionState::setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback)
{
_onSetTerminatedCallback = callback;
}
bool ConnectionState::isTerminated() const bool ConnectionState::isTerminated() const
{ {
return _terminated; return _terminated;
@ -39,5 +44,10 @@ namespace ix
void ConnectionState::setTerminated() void ConnectionState::setTerminated()
{ {
_terminated = true; _terminated = true;
if (_onSetTerminatedCallback)
{
_onSetTerminatedCallback();
}
} }
} // namespace ix } // namespace ix

View File

@ -7,12 +7,15 @@
#pragma once #pragma once
#include <atomic> #include <atomic>
#include <functional>
#include <memory> #include <memory>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
namespace ix namespace ix
{ {
using OnSetTerminatedCallback = std::function<void()>;
class ConnectionState class ConnectionState
{ {
public: public:
@ -27,10 +30,16 @@ namespace ix
static std::shared_ptr<ConnectionState> createConnectionState(); static std::shared_ptr<ConnectionState> createConnectionState();
private:
void setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback);
protected: protected:
std::atomic<bool> _terminated; std::atomic<bool> _terminated;
std::string _id; std::string _id;
OnSetTerminatedCallback _onSetTerminatedCallback;
static std::atomic<uint64_t> _globalId; static std::atomic<uint64_t> _globalId;
friend class SocketServer;
}; };
} // namespace ix } // namespace ix

View File

@ -8,11 +8,11 @@
#include "IXNetSystem.h" #include "IXNetSystem.h"
#include "IXSelectInterrupt.h" #include "IXSelectInterrupt.h"
#include "IXSelectInterruptFactory.h"
#include "IXSetThreadName.h" #include "IXSetThreadName.h"
#include "IXSocket.h" #include "IXSocket.h"
#include "IXSocketConnect.h" #include "IXSocketConnect.h"
#include "IXSocketFactory.h" #include "IXSocketFactory.h"
#include "IXSelectInterruptFactory.h"
#include <assert.h> #include <assert.h>
#include <sstream> #include <sstream>
#include <stdio.h> #include <stdio.h>
@ -259,7 +259,7 @@ namespace ix
if (_stop) return; if (_stop) return;
// Use poll to check whether a new connection is in progress // Use poll to check whether a new connection is in progress
int timeoutMs = 10; int timeoutMs = 10000;
bool readyToRead = true; bool readyToRead = true;
PollResultType pollResult = PollResultType pollResult =
Socket::poll(readyToRead, timeoutMs, _serverFd, _acceptSelectInterrupt); Socket::poll(readyToRead, timeoutMs, _serverFd, _acceptSelectInterrupt);
@ -355,6 +355,7 @@ namespace ix
{ {
connectionState = _connectionStateFactory(); connectionState = _connectionStateFactory();
} }
connectionState->setOnSetTerminatedCallback([this] { onSetTerminatedCallback(); });
if (_stop) return; if (_stop) return;
@ -423,4 +424,9 @@ namespace ix
{ {
_socketTLSOptions = socketTLSOptions; _socketTLSOptions = socketTLSOptions;
} }
void SocketServer::onSetTerminatedCallback()
{
;
}
} // namespace ix } // namespace ix

View File

@ -85,6 +85,7 @@ namespace ix
// background thread to wait for incoming connections // background thread to wait for incoming connections
std::thread _thread; std::thread _thread;
void run(); void run();
void onSetTerminatedCallback();
// background thread to cleanup (join) terminated threads // background thread to cleanup (join) terminated threads
std::atomic<bool> _stopGc; std::atomic<bool> _stopGc;

View File

@ -6,4 +6,4 @@
#pragma once #pragma once
#define IX_WEBSOCKET_VERSION "10.2.2" #define IX_WEBSOCKET_VERSION "10.2.3"