From 785842de03b46562e20d81695b5eec15753f8ce2 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sat, 15 Aug 2020 16:03:40 -0700 Subject: [PATCH] (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. --- docs/CHANGELOG.md | 4 ++++ ixwebsocket/IXConnectionState.cpp | 10 ++++++++++ ixwebsocket/IXConnectionState.h | 9 +++++++++ ixwebsocket/IXSocketServer.cpp | 10 ++++++++-- ixwebsocket/IXSocketServer.h | 1 + ixwebsocket/IXWebSocketVersion.h | 2 +- 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1df1d94b..c1a8cff2 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,10 @@ 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 (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 diff --git a/ixwebsocket/IXConnectionState.cpp b/ixwebsocket/IXConnectionState.cpp index a7c6c75b..8c273738 100644 --- a/ixwebsocket/IXConnectionState.cpp +++ b/ixwebsocket/IXConnectionState.cpp @@ -31,6 +31,11 @@ namespace ix return std::make_shared(); } + void ConnectionState::setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback) + { + _onSetTerminatedCallback = callback; + } + bool ConnectionState::isTerminated() const { return _terminated; @@ -39,5 +44,10 @@ namespace ix void ConnectionState::setTerminated() { _terminated = true; + + if (_onSetTerminatedCallback) + { + _onSetTerminatedCallback(); + } } } // namespace ix diff --git a/ixwebsocket/IXConnectionState.h b/ixwebsocket/IXConnectionState.h index 17df5bca..e45c8d40 100644 --- a/ixwebsocket/IXConnectionState.h +++ b/ixwebsocket/IXConnectionState.h @@ -7,12 +7,15 @@ #pragma once #include +#include #include #include #include namespace ix { + using OnSetTerminatedCallback = std::function; + class ConnectionState { public: @@ -27,10 +30,16 @@ namespace ix static std::shared_ptr createConnectionState(); + private: + void setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback); + protected: std::atomic _terminated; std::string _id; + OnSetTerminatedCallback _onSetTerminatedCallback; static std::atomic _globalId; + + friend class SocketServer; }; } // namespace ix diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 8ae0bed7..f0779071 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -8,11 +8,11 @@ #include "IXNetSystem.h" #include "IXSelectInterrupt.h" +#include "IXSelectInterruptFactory.h" #include "IXSetThreadName.h" #include "IXSocket.h" #include "IXSocketConnect.h" #include "IXSocketFactory.h" -#include "IXSelectInterruptFactory.h" #include #include #include @@ -259,7 +259,7 @@ namespace ix if (_stop) return; // Use poll to check whether a new connection is in progress - int timeoutMs = 10; + int timeoutMs = 10000; bool readyToRead = true; PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, _serverFd, _acceptSelectInterrupt); @@ -355,6 +355,7 @@ namespace ix { connectionState = _connectionStateFactory(); } + connectionState->setOnSetTerminatedCallback([this] { onSetTerminatedCallback(); }); if (_stop) return; @@ -423,4 +424,9 @@ namespace ix { _socketTLSOptions = socketTLSOptions; } + + void SocketServer::onSetTerminatedCallback() + { + ; + } } // namespace ix diff --git a/ixwebsocket/IXSocketServer.h b/ixwebsocket/IXSocketServer.h index feac52e1..c9c286cc 100644 --- a/ixwebsocket/IXSocketServer.h +++ b/ixwebsocket/IXSocketServer.h @@ -85,6 +85,7 @@ namespace ix // background thread to wait for incoming connections std::thread _thread; void run(); + void onSetTerminatedCallback(); // background thread to cleanup (join) terminated threads std::atomic _stopGc; diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index 1560aeb6..e659c34d 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "10.2.2" +#define IX_WEBSOCKET_VERSION "10.2.3"