From 8067cfb686552868e65e05a542bd0001ae17eef4 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Tue, 27 Apr 2021 14:52:48 -0700 Subject: [PATCH] Base: Tons more shit --- CMakeLists.txt | 3 ++ data/locale/en-US.ini | 2 +- src/Config.cpp | 2 +- src/Config.h | 2 +- src/WebSocketServer.cpp | 5 ++- src/WebSocketServer.h | 48 ++++++++++++++++++++++-- src/forms/SettingsDialog.cpp | 2 +- src/forms/SettingsDialog.h | 2 +- src/obs-websocket.cpp | 2 +- src/obs-websocket.h | 2 +- src/plugin-macros.generated.h | 2 + src/plugin-macros.h.in | 4 +- src/requesthandler/RequestHandler.cpp | 2 +- src/requesthandler/RequestHandler.h | 2 +- src/requesthandler/rpc/Request.cpp | 2 +- src/requesthandler/rpc/Request.h | 2 +- src/requesthandler/rpc/RequestResult.cpp | 2 +- src/requesthandler/rpc/RequestResult.h | 2 +- src/utils/JsonUtils.cpp | 2 +- src/utils/JsonUtils.h | 2 +- 20 files changed, 71 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 699f69c9..1c48bbdf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.16...3.20) project(obs-websocket VERSION 5.0.0) +set(OBS_WEBSOCKET_RPC_VERSION 1) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -72,6 +73,7 @@ set(obs-websocket_SOURCES src/obs-websocket.cpp src/Config.cpp src/WebSocketServer.cpp + src/WebSocketSession.cpp src/requesthandler/RequestHandler.cpp src/requesthandler/rpc/Request.cpp src/requesthandler/rpc/RequestResult.cpp @@ -82,6 +84,7 @@ set(obs-websocket_HEADERS src/obs-websocket.h src/Config.h src/WebSocketServer.h + src/WebSocketSession.h src/requesthandler/RequestHandler.h src/requesthandler/rpc/Request.h src/requesthandler/rpc/RequestResult.h diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 88ee3528..f5e7c3a8 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -20,4 +20,4 @@ OBSWebSocket.NotifyDisconnect.Title="WebSocket client disconnected." OBSWebSocket.NotifyDisconnect.Message="Client %1 disconnected" OBSWebSocket.Server.StartFailed.Title="WebSocket Server Failure" -OBSWebSocket.Server.StartFailed.Message="The WebSocket server failed to start. TCP port %1 may already be in use elsewhere on this system by another application. Try setting a different TCP port in the WebSocket server settings, or stop any application that could be using this port.\n Error message: %2" \ No newline at end of file +OBSWebSocket.Server.StartFailed.Message="The WebSocket server failed to start. TCP port %1 may already be in use elsewhere on this system by another application. Try setting a different TCP port in the WebSocket server settings, or stop any application that could be using this port.\n Error message: %2" diff --git a/src/Config.cpp b/src/Config.cpp index b0991a07..dc083817 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -78,4 +78,4 @@ void Config::SetDefaultsToGlobalStore() config_t* Config::GetConfigStore() { return obs_frontend_get_global_config(); -} \ No newline at end of file +} diff --git a/src/Config.h b/src/Config.h index 1a8e43bd..a202ba12 100644 --- a/src/Config.h +++ b/src/Config.h @@ -22,4 +22,4 @@ class Config { private: ; -}; \ No newline at end of file +}; diff --git a/src/WebSocketServer.cpp b/src/WebSocketServer.cpp index adb0e9bd..0cbd740b 100644 --- a/src/WebSocketServer.cpp +++ b/src/WebSocketServer.cpp @@ -1 +1,4 @@ -#include "WebSocketServer.h" \ No newline at end of file +#include + +#include "WebSocketServer.h" +#include "requesthandler/RequestHandler.h" \ No newline at end of file diff --git a/src/WebSocketServer.h b/src/WebSocketServer.h index 0438b029..e2e3c391 100644 --- a/src/WebSocketServer.h +++ b/src/WebSocketServer.h @@ -1,17 +1,57 @@ #pragma once #include -#include +#include +#include +#include +#include +#include + +#include "WebSocketSession.h" #include "requesthandler/RequestHandler.h" +using json = nlohmann::json; + class WebSocketServer : public QObject { Q_OBJECT public: - ; + WebSocketServer(); + ~WebSocketServer(); + + void Start(); + void Stop(); + void InvalidateSession(websocketpp::connection_hdl hdl); + + struct WebSocketState { + websocketpp::connection_hdl hdl; + std::string remoteAddress; + uint64_t durationSeconds; + uint64_t incomingMessages; + uint64_t outgoingMessages; + }; + + std::vector GetWebSocketSessions(); + + QThreadPool *GetThreadPool() { + return &_threadPool; + } + + public Q_SLOTS: + void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr); private: - ; -}; \ No newline at end of file + void onOpen(websocketpp::connection_hdl hdl); + void onClose(websocketpp::connection_hdl hdl); + void onMessage(websocketpp::connection_hdl hdl); + + WebSocketSession *GetWebSocketSession(websocketpp::connection_hdl hdl); + + websocketpp::server _server; + QThreadPool _threadPool; + + QMutex _sessionMutex; + std::map> _sessions; +}; diff --git a/src/forms/SettingsDialog.cpp b/src/forms/SettingsDialog.cpp index 22627924..65f448f1 100644 --- a/src/forms/SettingsDialog.cpp +++ b/src/forms/SettingsDialog.cpp @@ -115,4 +115,4 @@ void SettingsDialog::CopyPasswordButtonClicked() QClipboard *clipboard = QGuiApplication::clipboard(); clipboard->setText(ui->serverPasswordLineEdit->text()); ui->serverPasswordLineEdit->selectAll(); -} \ No newline at end of file +} diff --git a/src/forms/SettingsDialog.h b/src/forms/SettingsDialog.h index 046eea5b..c33340c9 100644 --- a/src/forms/SettingsDialog.h +++ b/src/forms/SettingsDialog.h @@ -23,4 +23,4 @@ private: Ui::SettingsDialog* ui; void FillSessionTable(); -}; \ No newline at end of file +}; diff --git a/src/obs-websocket.cpp b/src/obs-websocket.cpp index 6002f7c5..4e31a375 100644 --- a/src/obs-websocket.cpp +++ b/src/obs-websocket.cpp @@ -72,4 +72,4 @@ ConfigPtr GetConfig() WebSocketServerPtr GetWebSocketServer() { return _webSocketServer; -} \ No newline at end of file +} diff --git a/src/obs-websocket.h b/src/obs-websocket.h index 02ada487..a2a87b39 100644 --- a/src/obs-websocket.h +++ b/src/obs-websocket.h @@ -36,4 +36,4 @@ typedef std::shared_ptr WebSocketServerPtr; ConfigPtr GetConfig(); -WebSocketServerPtr GetWebSocketServer(); \ No newline at end of file +WebSocketServerPtr GetWebSocketServer(); diff --git a/src/plugin-macros.generated.h b/src/plugin-macros.generated.h index dc3a53b5..2a629770 100644 --- a/src/plugin-macros.generated.h +++ b/src/plugin-macros.generated.h @@ -21,6 +21,8 @@ with this program. If not, see #define OBS_WEBSOCKET_VERSION "5.0.0" +#define OBS_WEBSOCKET_RPC_VERSION 1 + #define QT_TO_UTF8(str) str.toUtf8().constData() #define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__) diff --git a/src/plugin-macros.h.in b/src/plugin-macros.h.in index 3a8e93f6..1c614051 100644 --- a/src/plugin-macros.h.in +++ b/src/plugin-macros.h.in @@ -21,8 +21,10 @@ with this program. If not, see #define OBS_WEBSOCKET_VERSION "@CMAKE_PROJECT_VERSION@" +#define OBS_WEBSOCKET_RPC_VERSION @OBS_WEBSOCKET_RPC_VERSION@ + #define QT_TO_UTF8(str) str.toUtf8().constData() #define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__) -#endif // PLUGINNAME_H \ No newline at end of file +#endif // PLUGINNAME_H diff --git a/src/requesthandler/RequestHandler.cpp b/src/requesthandler/RequestHandler.cpp index 207c28c6..e0c02c4f 100644 --- a/src/requesthandler/RequestHandler.cpp +++ b/src/requesthandler/RequestHandler.cpp @@ -1 +1 @@ -#include "RequestHandler.h" \ No newline at end of file +#include "RequestHandler.h" diff --git a/src/requesthandler/RequestHandler.h b/src/requesthandler/RequestHandler.h index ae026626..68333399 100644 --- a/src/requesthandler/RequestHandler.h +++ b/src/requesthandler/RequestHandler.h @@ -1,4 +1,4 @@ #pragma once #include "rpc/Request.h" -#include "rpc/RequestResult.h" \ No newline at end of file +#include "rpc/RequestResult.h" diff --git a/src/requesthandler/rpc/Request.cpp b/src/requesthandler/rpc/Request.cpp index 4fcf5444..f6cdf2b0 100644 --- a/src/requesthandler/rpc/Request.cpp +++ b/src/requesthandler/rpc/Request.cpp @@ -1 +1 @@ -#include "Request.h" \ No newline at end of file +#include "Request.h" diff --git a/src/requesthandler/rpc/Request.h b/src/requesthandler/rpc/Request.h index 9e79fd71..5e183513 100644 --- a/src/requesthandler/rpc/Request.h +++ b/src/requesthandler/rpc/Request.h @@ -1,3 +1,3 @@ #pragma once -#include \ No newline at end of file +#include diff --git a/src/requesthandler/rpc/RequestResult.cpp b/src/requesthandler/rpc/RequestResult.cpp index 12636a9f..b2392843 100644 --- a/src/requesthandler/rpc/RequestResult.cpp +++ b/src/requesthandler/rpc/RequestResult.cpp @@ -1 +1 @@ -#include "RequestResult.h" \ No newline at end of file +#include "RequestResult.h" diff --git a/src/requesthandler/rpc/RequestResult.h b/src/requesthandler/rpc/RequestResult.h index 9e79fd71..5e183513 100644 --- a/src/requesthandler/rpc/RequestResult.h +++ b/src/requesthandler/rpc/RequestResult.h @@ -1,3 +1,3 @@ #pragma once -#include \ No newline at end of file +#include diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index 0a543c49..3c672d4b 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -145,4 +145,4 @@ json JsonUtils::ObsDataToJson(obs_data_t *d, bool includeDefault) } return j; -} \ No newline at end of file +} diff --git a/src/utils/JsonUtils.h b/src/utils/JsonUtils.h index 1c958ccb..31bdfed4 100644 --- a/src/utils/JsonUtils.h +++ b/src/utils/JsonUtils.h @@ -12,4 +12,4 @@ namespace JsonUtils { bool JsonArrayIsValidObsArray(json j); obs_data_t *JsonToObsData(json j); json ObsDataToJson(obs_data_t *d, bool includeDefault = false); -}; \ No newline at end of file +};