diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2023-10-15 18:57:47 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2023-10-15 18:57:47 +0200 |
commit | 2dace4e0e57df5506a31687b44a48fa94c9f9071 (patch) | |
tree | ee64a4c5d875ce43b15835c8790492109583ef80 /src | |
parent | 3583254d59ecc7ca5534c7759224a806de1ff5a8 (diff) | |
download | SouthRPC-2dace4e0e57df5506a31687b44a48fa94c9f9071.tar.gz SouthRPC-2dace4e0e57df5506a31687b44a48fa94c9f9071.zip |
Add error checking, local testing, default convar handler
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/handler.cpp | 36 | ||||
-rw-r--r-- | src/http_server.h | 6 | ||||
-rw-r--r-- | src/internal/convarproxy.h | 9 | ||||
-rw-r--r-- | src/local/CMakeLists.txt | 16 | ||||
-rw-r--r-- | src/local/main.cpp | 12 | ||||
-rw-r--r-- | src/rpc_server.h | 2 |
7 files changed, 69 insertions, 16 deletions
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 + $<TARGET_OBJECTS:SouthRPC> + "${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(); }; |