From 2d84ad89630d6189242faee44ecce955efd03af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lepin?= Date: Fri, 26 Apr 2019 13:39:18 +0200 Subject: [PATCH] server: handle requests in a separate thread --- src/WSServer.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/WSServer.cpp b/src/WSServer.cpp index bec085b0..5230f482 100644 --- a/src/WSServer.cpp +++ b/src/WSServer.cpp @@ -166,24 +166,26 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message) return; } - std::string payload = message->get_payload(); + QtConcurrent::run([=]() { + std::string payload = message->get_payload(); - QMutexLocker locker(&_clMutex); - QVariantHash connProperties = _connectionProperties[hdl]; - locker.unlock(); + QMutexLocker locker(&_clMutex); + QVariantHash connProperties = _connectionProperties[hdl]; + locker.unlock(); - WSRequestHandler handler(connProperties); - std::string response = handler.processIncomingMessage(payload); + WSRequestHandler handler(connProperties); + std::string response = handler.processIncomingMessage(payload); - _server.send(hdl, response, websocketpp::frame::opcode::text); + _server.send(hdl, response, websocketpp::frame::opcode::text); - locker.relock(); - // In multithreaded processing this would be problematic to put back - // a copy of the connection properties, because there might conflicts - // between several simultaneous handlers. - // In our case, it's fine because all messages are processed in one thread. - _connectionProperties[hdl] = connProperties; - locker.unlock(); + locker.relock(); + // In multithreaded processing this would be problematic to put back + // a copy of the connection properties, because there might conflicts + // between several simultaneous handlers. + // In our case, it's fine because all messages are processed in one thread. + _connectionProperties[hdl] = connProperties; + locker.unlock(); + }); } void WSServer::onClose(connection_hdl hdl)