diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c48bbdf..49bddd2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,7 +89,7 @@ set(obs-websocket_HEADERS src/requesthandler/rpc/Request.h src/requesthandler/rpc/RequestResult.h src/forms/SettingsDialog.h - src/utils/JsonUtils.h) + src/utils/Utils.h) # Platform-independent build settings diff --git a/src/Config.cpp b/src/Config.cpp index dc083817..54d2ca0c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,6 +1,8 @@ #include #include +#include "plugin-macros.generated.h" + #include "Config.h" #define CONFIG_SECTION_NAME "OBSWebSocket" diff --git a/src/Config.h b/src/Config.h index a202ba12..6cb4fe1f 100644 --- a/src/Config.h +++ b/src/Config.h @@ -3,8 +3,6 @@ #include #include -#include "plugin-macros.generated.h" - class Config { public: Config(); diff --git a/src/WebSocketServer.cpp b/src/WebSocketServer.cpp index 0cbd740b..6097ef1f 100644 --- a/src/WebSocketServer.cpp +++ b/src/WebSocketServer.cpp @@ -1,4 +1,95 @@ #include +#include "plugin-macros.generated.h" + #include "WebSocketServer.h" -#include "requesthandler/RequestHandler.h" \ No newline at end of file +#include "obs-websocket.h" +#include "Config.h" +#include "requesthandler/RequestHandler.h" + +WebSocketServer::WebSocketServer() : + QObject(nullptr), + _sessionMutex(QMutex::Recursive), + _sessions() +{ + _server.get_alog().clear_channels(websocketpp::log::alevel::frame_header | websocketpp::log::alevel::frame_payload | websocketpp::log::alevel::control); + _server.init_asio(); + +#ifndef _WIN32 + _server.set_reuse_addr(true); +#endif + + _server.set_open_handler( + websocketpp::lib::bind( + &WebSocketServer::onOpen, this, websocketpp::lib::placeholders::_1 + ) + ); + _server.set_close_handler( + websocketpp::lib::bind( + &WebSocketServer::onClose, this, websocketpp::lib::placeholders::_1 + ) + ); + _server.set_message_handler( + websocketpp::lib::bind( + &WebSocketServer::onMessage, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2 + ) + ); + + blog(LOG_INFO, "test"); +} + +WebSocketServer::~WebSocketServer() +{ + Stop(); +} + +void WebSocketServer::Start() +{ + ; +} + +void WebSocketServer::Stop() +{ + ; +} + +void WebSocketServer::InvalidateSession(websocketpp::connection_hdl hdl) +{ + ; +} + +std::vector WebSocketServer::GetWebSocketSessions() +{ + std::vector webSocketSessions; + return webSocketSessions; +} + +std::string WebSocketServer::GetConnectUrl() +{ + return ""; +} + +void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData) +{ + ; +} + +WebSocketSession *WebSocketServer::GetWebSocketSession(websocketpp::connection_hdl hdl) +{ + return new WebSocketSession(); +} + +void WebSocketServer::onOpen(websocketpp::connection_hdl hdl) +{ + ; +} + +void WebSocketServer::onClose(websocketpp::connection_hdl hdl) +{ + ; +} + +void WebSocketServer::onMessage(websocketpp::connection_hdl hdl, websocketpp::server::message_ptr message) +{ + ; +} diff --git a/src/WebSocketServer.h b/src/WebSocketServer.h index 7524afbf..edc53d30 100644 --- a/src/WebSocketServer.h +++ b/src/WebSocketServer.h @@ -9,7 +9,6 @@ #include #include "WebSocketSession.h" -#include "requesthandler/RequestHandler.h" using json = nlohmann::json; @@ -65,19 +64,21 @@ class WebSocketServer : public QObject return &_threadPool; } + std::string GetConnectUrl(); + public Q_SLOTS: void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr); private: + WebSocketSession *GetWebSocketSession(websocketpp::connection_hdl hdl); + void onOpen(websocketpp::connection_hdl hdl); void onClose(websocketpp::connection_hdl hdl); - void onMessage(websocketpp::connection_hdl hdl); - - WebSocketSession *GetWebSocketSession(websocketpp::connection_hdl hdl); + void onMessage(websocketpp::connection_hdl hdl, websocketpp::server::message_ptr message); websocketpp::server _server; QThreadPool _threadPool; - QMutex _sessionMutex; std::map> _sessions; + uint16_t _serverPort; }; diff --git a/src/WebSocketSession.cpp b/src/WebSocketSession.cpp index 54ffcff5..f8ab9767 100644 --- a/src/WebSocketSession.cpp +++ b/src/WebSocketSession.cpp @@ -1,3 +1,5 @@ +#include "plugin-macros.generated.h" + #include "WebSocketSession.h" WebSocketSession::WebSocketSession() : diff --git a/src/WebSocketSession.h b/src/WebSocketSession.h index dccf3c2f..96d256b0 100644 --- a/src/WebSocketSession.h +++ b/src/WebSocketSession.h @@ -2,8 +2,6 @@ #include -#include "plugin-macros.generated.h" - class WebSocketSession { public: diff --git a/src/forms/SettingsDialog.cpp b/src/forms/SettingsDialog.cpp index d60951d7..42258dc3 100644 --- a/src/forms/SettingsDialog.cpp +++ b/src/forms/SettingsDialog.cpp @@ -4,6 +4,8 @@ #include #include +#include "../plugin-macros.generated.h" + #include "SettingsDialog.h" #include "../obs-websocket.h" #include "../Config.h" diff --git a/src/obs-websocket.cpp b/src/obs-websocket.cpp index e4c7da0a..292126e8 100644 --- a/src/obs-websocket.cpp +++ b/src/obs-websocket.cpp @@ -6,6 +6,8 @@ #include #include +#include "plugin-macros.generated.h" + #include "obs-websocket.h" #include "Config.h" #include "WebSocketServer.h" diff --git a/src/obs-websocket.h b/src/obs-websocket.h index a2a87b39..bcb9dfa3 100644 --- a/src/obs-websocket.h +++ b/src/obs-websocket.h @@ -3,8 +3,6 @@ #include #include -#include "plugin-macros.generated.h" - // Autorelease object declarations void ___source_dummy_addref(obs_source_t*); void ___sceneitem_dummy_addref(obs_sceneitem_t*); diff --git a/src/plugin-macros.generated.h b/src/plugin-macros.generated.h index 2a629770..aadae584 100644 --- a/src/plugin-macros.generated.h +++ b/src/plugin-macros.generated.h @@ -27,4 +27,4 @@ with this program. If not, see #define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__) -#endif // PLUGINNAME_H +#endif diff --git a/src/plugin-macros.h.in b/src/plugin-macros.h.in index 1c614051..bb246bb2 100644 --- a/src/plugin-macros.h.in +++ b/src/plugin-macros.h.in @@ -27,4 +27,4 @@ with this program. If not, see #define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__) -#endif // PLUGINNAME_H +#endif diff --git a/src/requesthandler/RequestHandler.cpp b/src/requesthandler/RequestHandler.cpp index e0c02c4f..20d8d0d4 100644 --- a/src/requesthandler/RequestHandler.cpp +++ b/src/requesthandler/RequestHandler.cpp @@ -1 +1,3 @@ +#include "../plugin-macros.generated.h" + #include "RequestHandler.h" diff --git a/src/requesthandler/rpc/Request.cpp b/src/requesthandler/rpc/Request.cpp index f6cdf2b0..b2b7290e 100644 --- a/src/requesthandler/rpc/Request.cpp +++ b/src/requesthandler/rpc/Request.cpp @@ -1 +1,3 @@ +#include "../../plugin-macros.generated.h" + #include "Request.h" diff --git a/src/requesthandler/rpc/RequestResult.cpp b/src/requesthandler/rpc/RequestResult.cpp index b2392843..ef11f7ad 100644 --- a/src/requesthandler/rpc/RequestResult.cpp +++ b/src/requesthandler/rpc/RequestResult.cpp @@ -1 +1,3 @@ +#include "../../plugin-macros.generated.h" + #include "RequestResult.h" diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index 3c672d4b..cd28376d 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -1,6 +1,9 @@ -#include "JsonUtils.h" +// For AutoRelease types +#include "../obs-websocket.h" -bool JsonUtils::JsonArrayIsValidObsArray(json j) +#include "Utils.h" + +bool Utils::Json::JsonArrayIsValidObsArray(json j) { for (auto it : j) { if (!it.is_object()) @@ -54,7 +57,7 @@ void obs_data_set_json_object_item(obs_data_t *d, json j) } } -obs_data_t *JsonUtils::JsonToObsData(json j) +obs_data_t *Utils::Json::JsonToObsData(json j) { obs_data_t *data = obs_data_create(); @@ -93,7 +96,7 @@ void set_json_bool(json *j, const char *name, obs_data_item_t *item) void set_json_object(json *j, const char *name, obs_data_item_t *item, bool includeDefault) { obs_data_t *obj = obs_data_item_get_obj(item); - j->emplace(name, JsonUtils::ObsDataToJson(obj, includeDefault)); + j->emplace(name, Utils::Json::ObsDataToJson(obj, includeDefault)); obs_data_release(obj); } void set_json_array(json *j, const char *name, obs_data_item_t *item, bool includeDefault) @@ -104,14 +107,14 @@ void set_json_array(json *j, const char *name, obs_data_item_t *item, bool inclu for (size_t idx = 0; idx < count; idx++) { OBSDataAutoRelease subItem = obs_data_array_item(array, idx); - json jItem = JsonUtils::ObsDataToJson(subItem, includeDefault); + json jItem = Utils::Json::ObsDataToJson(subItem, includeDefault); jArray.push_back(jItem); } j->emplace(name, jArray); } -json JsonUtils::ObsDataToJson(obs_data_t *d, bool includeDefault) +json Utils::Json::ObsDataToJson(obs_data_t *d, bool includeDefault) { json j; obs_data_item_t *item = nullptr; diff --git a/src/utils/JsonUtils.h b/src/utils/JsonUtils.h deleted file mode 100644 index 31bdfed4..00000000 --- a/src/utils/JsonUtils.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -// For AutoRelease types -#include "../obs-websocket.h" - -using json = nlohmann::json; - -namespace JsonUtils { - bool JsonArrayIsValidObsArray(json j); - obs_data_t *JsonToObsData(json j); - json ObsDataToJson(obs_data_t *d, bool includeDefault = false); -}; diff --git a/src/utils/Utils.h b/src/utils/Utils.h new file mode 100644 index 00000000..b7889bbc --- /dev/null +++ b/src/utils/Utils.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +using json = nlohmann::json; + +namespace Utils { + namespace Json { + bool JsonArrayIsValidObsArray(json j); + obs_data_t *JsonToObsData(json j); + json ObsDataToJson(obs_data_t *d, bool includeDefault = false); + }; +};