WSServer: Run asio loop in std::thread and join on stop

This commit is contained in:
tt2468 2021-06-01 18:00:24 -07:00
parent 1266b3f3fe
commit c1cd1adb08
2 changed files with 33 additions and 6 deletions

View File

@ -60,6 +60,21 @@ WSServer::~WSServer()
stop(); stop();
} }
void WSServer::serverRunner()
{
blog(LOG_INFO, "IO thread started.");
try {
_server.run();
} catch (websocketpp::exception const & e) {
blog(LOG_ERROR, "websocketpp instance returned an error: %s", e.what());
} catch (const std::exception & e) {
blog(LOG_ERROR, "websocketpp instance returned an error: %s", e.what());
} catch (...) {
blog(LOG_ERROR, "websocketpp instance returned an error");
}
blog(LOG_INFO, "IO thread exited.");
}
void WSServer::start(quint16 port, bool lockToIPv4) void WSServer::start(quint16 port, bool lockToIPv4)
{ {
if (_server.is_listening() && (port == _serverPort && _lockToIPv4 == lockToIPv4)) { if (_server.is_listening() && (port == _serverPort && _lockToIPv4 == lockToIPv4)) {
@ -102,11 +117,7 @@ void WSServer::start(quint16 port, bool lockToIPv4)
_server.start_accept(); _server.start_accept();
QtConcurrent::run([=]() { _serverThread = std::thread(&WSServer::serverRunner, this);
blog(LOG_INFO, "io thread started");
_server.run();
blog(LOG_INFO, "io thread exited");
});
blog(LOG_INFO, "server started successfully on port %d", _serverPort); blog(LOG_INFO, "server started successfully on port %d", _serverPort);
} }
@ -119,7 +130,18 @@ void WSServer::stop()
_server.stop_listening(); _server.stop_listening();
for (connection_hdl hdl : _connections) { for (connection_hdl hdl : _connections) {
_server.close(hdl, websocketpp::close::status::going_away, "Server stopping"); websocketpp::lib::error_code errorCode;
_server.pause_reading(hdl, errorCode);
if (errorCode) {
blog(LOG_ERROR, "Error: %s", errorCode.message().c_str());
continue;
}
_server.close(hdl, websocketpp::close::status::going_away, "Server stopping", errorCode);
if (errorCode) {
blog(LOG_ERROR, "Error: %s", errorCode.message().c_str());
continue;
}
} }
_threadPool.waitForDone(); _threadPool.waitForDone();
@ -128,6 +150,8 @@ void WSServer::stop()
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} }
_serverThread.join();
blog(LOG_INFO, "server stopped successfully"); blog(LOG_INFO, "server stopped successfully");
} }

View File

@ -53,6 +53,8 @@ public:
} }
private: private:
void serverRunner();
void onOpen(connection_hdl hdl); void onOpen(connection_hdl hdl);
void onMessage(connection_hdl hdl, server::message_ptr message); void onMessage(connection_hdl hdl, server::message_ptr message);
void onClose(connection_hdl hdl); void onClose(connection_hdl hdl);
@ -61,6 +63,7 @@ private:
void notifyConnection(QString clientIp); void notifyConnection(QString clientIp);
void notifyDisconnection(QString clientIp); void notifyDisconnection(QString clientIp);
std::thread _serverThread;
server _server; server _server;
quint16 _serverPort; quint16 _serverPort;
bool _lockToIPv4; bool _lockToIPv4;