From 3f9f58d5f631e9575d040ce822bea01eebc091c9 Mon Sep 17 00:00:00 2001 From: /dev/root Date: Sun, 2 Dec 2018 18:17:45 +0100 Subject: [PATCH 1/2] [Download] Add serverlist info --- src/Components/Modules/Download.cpp | 32 +++++++++++++++++++++++++++++ src/Components/Modules/Download.hpp | 1 + src/Components/Modules/Node.cpp | 8 ++++++++ src/Components/Modules/Node.hpp | 1 + 4 files changed, 42 insertions(+) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index f347920b..bb763b1d 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -465,6 +465,37 @@ namespace Components nc->flags |= MG_F_SEND_AND_CLOSE; } + void Download::ServerlistHandler(mg_connection* nc, int ev, void* /*ev_data*/) + { + // Only handle http requests + if (ev != MG_EV_HTTP_REQUEST) return; + + std::vector servers; + + // Build server list + for (auto& node : Node::GetNodes()) + { + std::map serverInfo; + serverInfo["address"] = "127.0.0.1:28960"; + + if (node.isValid()) + { + serverInfo["address"] = node.address.getCString(); + servers.push_back(serverInfo); + } + } + + mg_printf(nc, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Connection: close\r\n" + "Access-Control-Allow-Origin: *\r\n" + "\r\n" + "%s", json11::Json(servers).dump().data()); + + nc->flags |= MG_F_SEND_AND_CLOSE; + } + void Download::MapHandler(mg_connection *nc, int ev, void* ev_data) { // Only handle http requests @@ -819,6 +850,7 @@ namespace Components mg_register_http_endpoint(nc, "/list", Download::ListHandler); mg_register_http_endpoint(nc, "/map", Download::MapHandler); mg_register_http_endpoint(nc, "/file/", Download::FileHandler); + mg_register_http_endpoint(nc, "/serverlist", Download::ServerlistHandler); mg_set_protocol_http_websocket(nc); } diff --git a/src/Components/Modules/Download.hpp b/src/Components/Modules/Download.hpp index 8feb6071..3c53a7c9 100644 --- a/src/Components/Modules/Download.hpp +++ b/src/Components/Modules/Download.hpp @@ -217,6 +217,7 @@ namespace Components static void EventHandler(mg_connection *nc, int ev, void *ev_data); static void ListHandler(mg_connection *nc, int ev, void *ev_data); static void MapHandler(mg_connection *nc, int ev, void *ev_data); + static void ServerlistHandler(mg_connection *nc, int ev, void *ev_data); static void FileHandler(mg_connection *nc, int ev, void *ev_data); static void InfoHandler(mg_connection *nc, int ev, void *ev_data); static void DownloadHandler(mg_connection *nc, int ev, void *ev_data); diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index f033d885..bb19ddee 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -153,6 +153,13 @@ namespace Components Node::Nodes.push_back(node); } + std::vector Node::GetNodes() + { + std::lock_guard _(Node::Mutex); + + return Node::Nodes; + } + void Node::RunFrame() { if (Dedicated::IsEnabled() && Dvar::Var("sv_lanOnly").get()) return; @@ -264,6 +271,7 @@ namespace Components Node::Nodes.push_back(entry); } } + void Node::SendList(Network::Address address) { Proto::Node::List list; diff --git a/src/Components/Modules/Node.hpp b/src/Components/Modules/Node.hpp index ac6ce353..760a5d8f 100644 --- a/src/Components/Modules/Node.hpp +++ b/src/Components/Modules/Node.hpp @@ -41,6 +41,7 @@ namespace Components ~Node(); static void Add(Network::Address address); + static std::vector GetNodes(); static void RunFrame(); static void Synchronize(); From 70a60517ee64faf7317a4147548becad6cdc647c Mon Sep 17 00:00:00 2001 From: /dev/root Date: Mon, 3 Dec 2018 21:48:12 +0100 Subject: [PATCH 2/2] [Download] Change json --- src/Components/Modules/Download.cpp | 6 +----- src/Components/Modules/Node.cpp | 5 +++++ src/Components/Modules/Node.hpp | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index bb763b1d..eb18ae6e 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -475,13 +475,9 @@ namespace Components // Build server list for (auto& node : Node::GetNodes()) { - std::map serverInfo; - serverInfo["address"] = "127.0.0.1:28960"; - if (node.isValid()) { - serverInfo["address"] = node.address.getCString(); - servers.push_back(serverInfo); + servers.push_back(json11::Json{ node }); } } diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index bb19ddee..75a5b458 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -50,6 +50,11 @@ namespace Components this->lastRequest.reset(); } + json11::Json Node::Entry::to_json() const + { + return this->address.getString(); + } + void Node::LoadNodeRemotePreset() { std::string nodes = Utils::Cache::GetFile("/iw4/nodes.txt"); diff --git a/src/Components/Modules/Node.hpp b/src/Components/Modules/Node.hpp index 760a5d8f..31299e4f 100644 --- a/src/Components/Modules/Node.hpp +++ b/src/Components/Modules/Node.hpp @@ -35,6 +35,7 @@ namespace Components void sendRequest(); void reset(); + json11::Json to_json() const; }; Node();