server: handle requests in a separate thread

This commit is contained in:
Stéphane Lepin 2019-04-26 13:39:18 +02:00
parent 1ec69cbc0d
commit 2d84ad8963

View File

@ -166,24 +166,26 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
return; return;
} }
std::string payload = message->get_payload(); QtConcurrent::run([=]() {
std::string payload = message->get_payload();
QMutexLocker locker(&_clMutex); QMutexLocker locker(&_clMutex);
QVariantHash connProperties = _connectionProperties[hdl]; QVariantHash connProperties = _connectionProperties[hdl];
locker.unlock(); locker.unlock();
WSRequestHandler handler(connProperties); WSRequestHandler handler(connProperties);
std::string response = handler.processIncomingMessage(payload); std::string response = handler.processIncomingMessage(payload);
_server.send(hdl, response, websocketpp::frame::opcode::text); _server.send(hdl, response, websocketpp::frame::opcode::text);
locker.relock(); locker.relock();
// In multithreaded processing this would be problematic to put back // In multithreaded processing this would be problematic to put back
// a copy of the connection properties, because there might conflicts // a copy of the connection properties, because there might conflicts
// between several simultaneous handlers. // between several simultaneous handlers.
// In our case, it's fine because all messages are processed in one thread. // In our case, it's fine because all messages are processed in one thread.
_connectionProperties[hdl] = connProperties; _connectionProperties[hdl] = connProperties;
locker.unlock(); locker.unlock();
});
} }
void WSServer::onClose(connection_hdl hdl) void WSServer::onClose(connection_hdl hdl)