mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
WSServer: Run asio loop in std::thread and join on stop
This commit is contained in:
parent
1266b3f3fe
commit
c1cd1adb08
@ -60,6 +60,21 @@ WSServer::~WSServer()
|
||||
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)
|
||||
{
|
||||
if (_server.is_listening() && (port == _serverPort && _lockToIPv4 == lockToIPv4)) {
|
||||
@ -102,11 +117,7 @@ void WSServer::start(quint16 port, bool lockToIPv4)
|
||||
|
||||
_server.start_accept();
|
||||
|
||||
QtConcurrent::run([=]() {
|
||||
blog(LOG_INFO, "io thread started");
|
||||
_server.run();
|
||||
blog(LOG_INFO, "io thread exited");
|
||||
});
|
||||
_serverThread = std::thread(&WSServer::serverRunner, this);
|
||||
|
||||
blog(LOG_INFO, "server started successfully on port %d", _serverPort);
|
||||
}
|
||||
@ -119,7 +130,18 @@ void WSServer::stop()
|
||||
|
||||
_server.stop_listening();
|
||||
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();
|
||||
@ -128,6 +150,8 @@ void WSServer::stop()
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
_serverThread.join();
|
||||
|
||||
blog(LOG_INFO, "server stopped successfully");
|
||||
}
|
||||
|
||||
|
@ -53,6 +53,8 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
void serverRunner();
|
||||
|
||||
void onOpen(connection_hdl hdl);
|
||||
void onMessage(connection_hdl hdl, server::message_ptr message);
|
||||
void onClose(connection_hdl hdl);
|
||||
@ -61,6 +63,7 @@ private:
|
||||
void notifyConnection(QString clientIp);
|
||||
void notifyDisconnection(QString clientIp);
|
||||
|
||||
std::thread _serverThread;
|
||||
server _server;
|
||||
quint16 _serverPort;
|
||||
bool _lockToIPv4;
|
||||
|
Loading…
x
Reference in New Issue
Block a user