WebSocketServer: Remove QtConcurrent dependency

We can avoid requiring QtConcurrent by using QRunnables. Thanks to
micolous for the idea.
This commit is contained in:
tt2468 2021-09-24 18:11:31 -07:00
parent 78d02696d0
commit 0c2e40263a
3 changed files with 12 additions and 9 deletions

View File

@ -62,7 +62,7 @@ find_package(LibObs REQUIRED)
# Find Qt5
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Svg Concurrent Network)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Svg Network)
# Find nlohmann
@ -115,6 +115,7 @@ set(obs-websocket_SOURCES
src/utils/Json.cpp
src/utils/Obs.cpp
src/utils/Platform.cpp
src/utils/Compat.cpp
deps/qr/cpp/QrCode.cpp)
set(obs-websocket_HEADERS
@ -134,6 +135,7 @@ set(obs-websocket_HEADERS
src/utils/Json.h
src/utils/Obs.h
src/utils/Platform.h
src/utils/Compat.h
src/utils/Utils.h
deps/qr/cpp/QrCode.hpp)
@ -148,7 +150,6 @@ include_directories(
${Qt5Core_INCLUDES}
${Qt5Widgets_INCLUDES}
${Qt5Svg_INCLUDES}
${Qt5Concurrent_INCLUDES}
${Qt5Network_INCLUDES}
"${CMAKE_SOURCE_DIR}/deps/asio/asio/include"
"${CMAKE_SOURCE_DIR}/deps/websocketpp")
@ -158,7 +159,6 @@ target_link_libraries(obs-websocket
Qt5::Core
Qt5::Widgets
Qt5::Svg
Qt5::Concurrent
Qt5::Network
nlohmann_json::nlohmann_json)

View File

@ -1,6 +1,5 @@
#include <chrono>
#include <thread>
#include <QtConcurrent>
#include <QDateTime>
#include <obs-module.h>
#include <obs-frontend-api.h>
@ -11,6 +10,7 @@
#include "Config.h"
#include "utils/Crypto.h"
#include "utils/Platform.h"
#include "utils/Compat.h"
WebSocketServer::WebSocketServer() :
QObject(nullptr),
@ -269,6 +269,9 @@ void WebSocketServer::onOpen(websocketpp::connection_hdl hdl)
// Log connection
blog(LOG_INFO, "[WebSocketServer::onOpen] New WebSocket client has connected from %s", session->RemoteAddress().c_str());
if (_debugEnabled)
blog(LOG_INFO, "[WebSocketServer::onOpen] Sending Op 0 (Hello) message:\n%s", helloMessage.dump(2).c_str());
// Send object to client
websocketpp::lib::error_code errorCode;
auto sessionEncoding = session->Encoding();
@ -338,7 +341,7 @@ void WebSocketServer::onMessage(websocketpp::connection_hdl hdl, websocketpp::se
{
auto opcode = message->get_opcode();
std::string payload = message->get_payload();
QtConcurrent::run(&_threadPool, [=]() {
_threadPool.start(Utils::Compat::CreateFunctionRunnable([=]() {
std::unique_lock<std::mutex> lock(_sessionMutex);
SessionPtr session;
try {
@ -445,5 +448,5 @@ skipProcessing:
if (errorCode)
blog(LOG_WARNING, "[WebSocketServer::onMessage] Sending message to client failed: %s", errorCode.message().c_str());
}
});
}));
}

View File

@ -1,4 +1,3 @@
#include <QtConcurrent>
#include <obs-module.h>
#include "WebSocketServer.h"
@ -8,6 +7,7 @@
#include "Config.h"
#include "utils/Crypto.h"
#include "utils/Platform.h"
#include "utils/Compat.h"
namespace WebSocketOpCode {
enum WebSocketOpCode: uint8_t {
@ -261,7 +261,7 @@ void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventT
if (!_server.is_listening())
return;
QtConcurrent::run(&_threadPool, [=]() {
_threadPool.start(Utils::Compat::CreateFunctionRunnable([=]() {
// Populate message object
json eventMessage;
eventMessage["op"] = 5;
@ -309,5 +309,5 @@ void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventT
lock.unlock();
if (_debugEnabled && (EventSubscription::All & requiredIntent) != 0) // Don't log high volume events
blog(LOG_INFO, "[WebSocketServer::BroadcastEvent] Outgoing event:\n%s", eventMessage.dump(2).c_str());
});
}));
}