From 2dace4e0e57df5506a31687b44a48fa94c9f9071 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 15 Oct 2023 18:57:47 +0200 Subject: Add error checking, local testing, default convar handler --- CMakeLists.txt | 2 ++ src/CMakeLists.txt | 4 ++++ src/handler.cpp | 36 +++++++++++++++++++++++++----------- src/http_server.h | 6 ++++-- src/internal/convarproxy.h | 9 ++++++--- src/local/CMakeLists.txt | 16 ++++++++++++++++ src/local/main.cpp | 12 ++++++++++++ src/rpc_server.h | 2 ++ 8 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 src/local/CMakeLists.txt create mode 100644 src/local/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c6cf527..c1ed934 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ if(NOT CMAKE_BUILD_TYPE) "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE) endif() +option(BUILD_LOCAL "Build Plugin as a local Executable for testing" OFF) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a68c423..0cf8952 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,3 +34,7 @@ plugin_thunderstore(SouthRPC "${PROJECT_SOURCE_DIR}/README.md" "${PROJECT_SOURCE_DIR}/icon.png" ) + +if (BUILD_LOCAL) + add_subdirectory(local) +endif() diff --git a/src/handler.cpp b/src/handler.cpp index f82573c..56aea76 100644 --- a/src/handler.cpp +++ b/src/handler.cpp @@ -28,18 +28,25 @@ ServerHandler::ServerHandler(Plugin* plugin): this->init = true; - /* - @TODO + /* execute_squirrel */ this->register_callback( - "list_methods", - [=](rapidjson::Value& params) -> rapidjson::Value + "methods", + [this](rapidjson::MemoryPoolAllocator<>& allocator, rapidjson::Value& params) -> rapidjson::Value { - rapidjson::MemoryPoolAllocator<>& allocator = this->body.GetAllocator(); + rapidjson::Value method_list; + method_list.SetArray(); - return rapidjson::Value(1); + for (auto const& [key, val] : this->methods) + { + rapidjson::Value method_name; + method_name.SetString(key.c_str(), key.size(), allocator); + + method_list.PushBack(method_name, allocator); + } + + return method_list; } ); - */ } @@ -78,12 +85,19 @@ void ServerHandler::run() spdlog::info("Running Handler"); // The engine won't have loaded convar data until after the entry - //Sleep(SLEEP_DURATION); + spdlog::info("Waiting for engine to initialize"); + Sleep(SLEEP_DURATION); - //int port = this->Convar_Port->GetInt(); - int port = 26503; + int port = this->Convar_Port->GetInt(); RPCServer* server = new RPCServer(INADDR_ANY, port); - Sleep(SLEEP_DURATION); + + if (server->get_http_server().get_socket() == -1) + { + spdlog::error("HTTP Server failed to start"); + return; + } + + spdlog::info("Launched server on port {}", port); while (this->running) { diff --git a/src/http_server.h b/src/http_server.h index 0c4dafe..6175295 100644 --- a/src/http_server.h +++ b/src/http_server.h @@ -25,12 +25,12 @@ class HTTPRequest { HTTPRequest(SOCKET socket); ~HTTPRequest(); + std::string get_body() { return this->body; } + void parse_headers(std::string raw); void set_body(std::string body) { this->body = body; } size_t content_length(); - std::string get_body() { return this->body; } - void respond(std::string status_code, header_map response_headers, std::string response_body); void close(); }; @@ -45,6 +45,8 @@ class HTTPServer { HTTPServer(unsigned long addr, unsigned short port); ~HTTPServer(); + SOCKET get_socket() { return this->sock; } + void close(); HTTPRequest* receive_request(); }; diff --git a/src/internal/convarproxy.h b/src/internal/convarproxy.h index 08aea5a..3291bd8 100644 --- a/src/internal/convarproxy.h +++ b/src/internal/convarproxy.h @@ -62,7 +62,8 @@ public: } const char* GetString() const { - assert(this->ptr); + if (!this->ptr) + return this->pszDefaultValue; return this->ptr->m_Value.m_pszString; } @@ -72,13 +73,15 @@ public: } int GetInt() const { - assert(this->ptr); + if (!this->ptr) + return atoi(this->pszDefaultValue); return this->ptr->m_Value.m_nValue; } float GetFloat() const { - assert(this->ptr); + if (!this->ptr) + return atof(this->pszDefaultValue); return this->ptr->m_Value.m_fValue; } diff --git a/src/local/CMakeLists.txt b/src/local/CMakeLists.txt new file mode 100644 index 0000000..8e35f2b --- /dev/null +++ b/src/local/CMakeLists.txt @@ -0,0 +1,16 @@ + +find_package(spdlog REQUIRED) +find_package(NorthstarPluginABI REQUIRED) + +add_executable(LocalRPC + $ + "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp" +) +target_include_directories(LocalRPC PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..") +target_link_libraries(LocalRPC rapidjson_header) +target_link_libraries(LocalRPC ws2_32) +target_precompile_headers(LocalRPC PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../ns_plugin.h") + +target_link_libraries(LocalRPC spdlog::spdlog_header_only) +target_include_directories(LocalRPC PRIVATE ${NS_DLL_DIR}) +target_include_directories(LocalRPC PRIVATE ${NS_PLUG_DIR}) \ No newline at end of file diff --git a/src/local/main.cpp b/src/local/main.cpp new file mode 100644 index 0000000..aa1b990 --- /dev/null +++ b/src/local/main.cpp @@ -0,0 +1,12 @@ +#include "ns_plugin.h" +#include "plugin.h" +#include "handler.h" + +int main() +{ + spdlog::info("Main"); + + Plugin plugin(nullptr, nullptr); + + plugin.server->run(); +} \ No newline at end of file diff --git a/src/rpc_server.h b/src/rpc_server.h index afa72fe..edfc15b 100644 --- a/src/rpc_server.h +++ b/src/rpc_server.h @@ -40,6 +40,8 @@ class RPCServer { public: RPCServer(unsigned long addr, unsigned short port); + HTTPServer& get_http_server() { return this->http_server; } + RPCRequest* receive_request(); }; -- cgit v1.2.3