From d5fc3befb8735d10e74df4d96ac9897bb5285edb Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Fri, 21 Jan 2022 21:28:43 +0100 Subject: [PATCH] Remove QtConcurrent dependency --- src/headers/switch-network.hpp | 15 +++++++++++++++ src/switch-network.cpp | 25 ++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/headers/switch-network.hpp b/src/headers/switch-network.hpp index 45d5fa5b..5f519457 100644 --- a/src/headers/switch-network.hpp +++ b/src/headers/switch-network.hpp @@ -12,6 +12,8 @@ Most of this code is based on https://github.com/Palakis/obs-websocket #include #include #include +#include +#include #include #include @@ -117,3 +119,16 @@ enum class ClientStatus { CONNECTED, FAIL, }; + +namespace Compatability { +// Reimplement QRunnable for std::function. Retrocompatability for Qt < 5.15 +class StdFunctionRunnable : public QRunnable { + std::function cb; + +public: + StdFunctionRunnable(std::function func); + void run() override; +}; + +QRunnable *CreateFunctionRunnable(std::function func); +} diff --git a/src/switch-network.cpp b/src/switch-network.cpp index 197c7d0d..1abd6910 100644 --- a/src/switch-network.cpp +++ b/src/switch-network.cpp @@ -3,7 +3,6 @@ Most of this code is based on https://github.com/Palakis/obs-websocket */ #include -#include #include #include @@ -188,11 +187,11 @@ void WSServer::start(quint16 port, bool lockToIPv4) _server.start_accept(); - QtConcurrent::run([=]() { + _threadPool.start(Compatability::CreateFunctionRunnable([=]() { blog(LOG_INFO, "WSServer::start: io thread started"); _server.run(); blog(LOG_INFO, "WSServer::start: io thread exited"); - }); + })); switcher->serverStatus = ServerStatus::RUNNING; blog(LOG_INFO, @@ -320,12 +319,12 @@ void WSServer::onMessage(connection_hdl, server::message_ptr message) return; } - QtConcurrent::run(&_threadPool, [=]() { + _threadPool.start(Compatability::CreateFunctionRunnable([=]() { if (message->get_payload() != "message ok") { blog(LOG_WARNING, "received response: %s", message->get_payload().c_str()); } - }); + })); } void WSServer::onClose(connection_hdl hdl) @@ -693,3 +692,19 @@ void AdvSceneSwitcher::updateClientStatus() break; } } + +void Compatability::StdFunctionRunnable::run() +{ + cb(); +} + +QRunnable *Compatability::CreateFunctionRunnable(std::function func) +{ + return new Compatability::StdFunctionRunnable(std::move(func)); +} + +Compatability::StdFunctionRunnable::StdFunctionRunnable( + std::function func) + : cb(std::move(func)) +{ +}