diff --git a/src/openrct2-ui/WindowManager.cpp b/src/openrct2-ui/WindowManager.cpp index 8c8e497466..d897f0f641 100644 --- a/src/openrct2-ui/WindowManager.cpp +++ b/src/openrct2-ui/WindowManager.cpp @@ -100,10 +100,12 @@ public: return window_save_prompt_open(); case WC_SCENERY: return window_scenery_open(); +#ifndef DISABLE_NETWORK case WC_SERVER_LIST: return window_server_list_open(); case WC_SERVER_START: return window_server_start_open(); +#endif case WC_KEYBOARD_SHORTCUT_LIST: return window_shortcut_keys_open(); case WC_STAFF_LIST: diff --git a/src/openrct2-ui/windows/ServerList.cpp b/src/openrct2-ui/windows/ServerList.cpp index 7976f4bac3..fa285efcb9 100644 --- a/src/openrct2-ui/windows/ServerList.cpp +++ b/src/openrct2-ui/windows/ServerList.cpp @@ -7,35 +7,33 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#ifndef DISABLE_NETWORK -#ifndef DISABLE_HTTP -using namespace OpenRCT2::Network; -#endif +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include -#define WWIDTH_MIN 500 -#define WHEIGHT_MIN 300 -#define WWIDTH_MAX 1200 -#define WHEIGHT_MAX 800 -#define ITEM_HEIGHT (3 + 9 + 3) +# define WWIDTH_MIN 500 +# define WHEIGHT_MIN 300 +# define WWIDTH_MAX 1200 +# define WHEIGHT_MAX 800 +# define ITEM_HEIGHT (3 + 9 + 3) static char _playerName[32 + 1]; static ServerList _serverList; @@ -613,3 +611,5 @@ static void server_list_fetch_servers_check(rct_window* w) } } } + +#endif diff --git a/src/openrct2-ui/windows/ServerStart.cpp b/src/openrct2-ui/windows/ServerStart.cpp index 5fc1d49cd0..631ea62f60 100644 --- a/src/openrct2-ui/windows/ServerStart.cpp +++ b/src/openrct2-ui/windows/ServerStart.cpp @@ -7,18 +7,20 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../interface/Theme.h" +#ifndef DISABLE_NETWORK -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +# include "../interface/Theme.h" + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include static char _port[7]; static char _name[65]; @@ -345,3 +347,5 @@ static void window_server_start_paint(rct_window* w, rct_drawpixelinfo* dpi) gfx_draw_string_left(dpi, STR_PASSWORD, nullptr, w->colours[1], w->x + 6, w->y + w->widgets[WIDX_PASSWORD_INPUT].top); gfx_draw_string_left(dpi, STR_MAX_PLAYERS, nullptr, w->colours[1], w->x + 6, w->y + w->widgets[WIDX_MAXPLAYERS].top); } + +#endif diff --git a/src/openrct2-ui/windows/Window.h b/src/openrct2-ui/windows/Window.h index 8c8d770e81..76c45cb3f4 100644 --- a/src/openrct2-ui/windows/Window.h +++ b/src/openrct2-ui/windows/Window.h @@ -43,8 +43,10 @@ rct_window* window_news_open(); rct_window* window_news_options_open(); rct_window* window_options_open(); rct_window* window_save_prompt_open(); +#ifndef DISABLE_NETWORK rct_window* window_server_list_open(); rct_window* window_server_start_open(); +#endif rct_window* window_shortcut_change_open(int32_t selected_key); rct_window* window_shortcut_keys_open(); rct_window* window_staff_list_open(); diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index 6e5504d8c8..bf9c921c51 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -32,10 +32,6 @@ # include # include -# ifndef DISABLE_HTTP - -using namespace OpenRCT2::Network; - enum MASTER_SERVER_STATUS { MASTER_SERVER_STATUS_OK = 200, @@ -52,7 +48,12 @@ class NetworkServerAdvertiser final : public INetworkServerAdvertiser private: uint16_t _port; + std::unique_ptr _lanListener; + uint32_t _lastListenTime{}; + ADVERTISE_STATUS _status = ADVERTISE_STATUS::UNREGISTERED; + +# ifndef DISABLE_HTTP uint32_t _lastAdvertiseTime = 0; uint32_t _lastHeartbeatTime = 0; @@ -64,16 +65,16 @@ private: // See https://github.com/OpenRCT2/OpenRCT2/issues/6277 and 4953 bool _forceIPv4 = false; - - std::unique_ptr _lanListener; - uint32_t _lastListenTime{}; +# endif public: explicit NetworkServerAdvertiser(uint16_t port) { _port = port; - _key = GenerateAdvertiseKey(); _lanListener = CreateUdpSocket(); +# ifndef DISABLE_HTTP + _key = GenerateAdvertiseKey(); +# endif } ADVERTISE_STATUS GetStatus() const override @@ -84,10 +85,12 @@ public: void Update() override { UpdateLAN(); +# ifndef DISABLE_HTTP if (gConfigNetwork.advertise) { UpdateWAN(); } +# endif } private: @@ -106,24 +109,34 @@ private: size_t recievedBytes{}; std::unique_ptr endpoint; auto p = _lanListener->ReceiveData(buffer, sizeof(buffer) - 1, &recievedBytes, &endpoint); - if (p == NETWORK_READPACKET_SUCCESS && String::Equals(buffer, NETWORK_LAN_BROADCAST_MSG)) + if (p == NETWORK_READPACKET_SUCCESS) { std::string sender = endpoint->GetHostname(); log_verbose("Received %zu bytes from %s on LAN broadcast port", recievedBytes, sender.c_str()); - - auto body = GetBroadcastJson(); - auto bodyDump = json_dumps(body, JSON_COMPACT); - size_t sendLen = strlen(bodyDump) + 1; - log_verbose("Sending %zu bytes back to %s", sendLen, sender.c_str()); - _lanListener->SendData(*endpoint, bodyDump, sendLen); - free(bodyDump); - json_decref(body); + if (String::Equals(buffer, NETWORK_LAN_BROADCAST_MSG)) + { + auto body = GetBroadcastJson(); + auto bodyDump = json_dumps(body, JSON_COMPACT); + size_t sendLen = strlen(bodyDump) + 1; + log_verbose("Sending %zu bytes back to %s", sendLen, sender.c_str()); + _lanListener->SendData(*endpoint, bodyDump, sendLen); + free(bodyDump); + json_decref(body); + } } } _lastListenTime = ticks; } } + json_t* GetBroadcastJson() + { + auto root = network_get_server_info_as_json(); + json_object_set(root, "port", json_integer(_port)); + return root; + } + +# ifndef DISABLE_HTTP void UpdateWAN() { switch (_status) @@ -148,6 +161,8 @@ private: void SendRegistration(bool forceIPv4) { + using namespace OpenRCT2::Network; + _lastAdvertiseTime = platform_get_ticks(); // Send the registration request @@ -181,6 +196,8 @@ private: void SendHeartbeat() { + using namespace OpenRCT2::Network; + Http::Request request; request.url = GetMasterServerUrl(); request.method = Http::Method::PUT; @@ -284,13 +301,6 @@ private: return root; } - json_t* GetBroadcastJson() - { - auto root = network_get_server_info_as_json(); - json_object_set(root, "port", json_integer(_port)); - return root; - } - static std::string GenerateAdvertiseKey() { // Generate a string of 16 random hex characters (64-integer key as a hex formatted string) @@ -316,6 +326,7 @@ private: } return result; } +# endif }; std::unique_ptr CreateServerAdvertiser(uint16_t port) @@ -323,23 +334,4 @@ std::unique_ptr CreateServerAdvertiser(uint16_t port) return std::make_unique(port); } -# else // DISABLE_HTTP - -class DummyNetworkServerAdvertiser final : public INetworkServerAdvertiser -{ -public: - virtual ADVERTISE_STATUS GetStatus() const override - { - return ADVERTISE_STATUS::DISABLED; - }; - virtual void Update() override{}; -}; - -std::unique_ptr CreateServerAdvertiser(uint16_t port) -{ - return std::make_unique(); -} - -# endif // DISABLE_HTTP - #endif // DISABLE_NETWORK diff --git a/src/openrct2/network/ServerList.cpp b/src/openrct2/network/ServerList.cpp index cf2dd67161..46f181ae3c 100644 --- a/src/openrct2/network/ServerList.cpp +++ b/src/openrct2/network/ServerList.cpp @@ -7,26 +7,27 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "ServerList.h" +#ifndef DISABLE_NETWORK -#include "../Context.h" -#include "../PlatformEnvironment.h" -#include "../config/Config.h" -#include "../core/FileStream.hpp" -#include "../core/Json.hpp" -#include "../core/Memory.hpp" -#include "../core/Path.hpp" -#include "../core/String.hpp" -#include "../network/Http.h" -#include "../platform/platform.h" -#include "Socket.h" -#include "network.h" +# include "ServerList.h" -#include -#include +# include "../Context.h" +# include "../PlatformEnvironment.h" +# include "../config/Config.h" +# include "../core/FileStream.hpp" +# include "../core/Json.hpp" +# include "../core/Memory.hpp" +# include "../core/Path.hpp" +# include "../core/String.hpp" +# include "../network/Http.h" +# include "../platform/platform.h" +# include "Socket.h" +# include "network.h" + +# include +# include using namespace OpenRCT2; -using namespace OpenRCT2::Network; int32_t ServerListEntry::CompareTo(const ServerListEntry& other) const { @@ -243,7 +244,7 @@ std::future> ServerList::FetchLocalServerListAsync( std::string_view msg = NETWORK_LAN_BROADCAST_MSG; auto udpSocket = CreateUdpSocket(); - log_verbose("Broadcasting %zu bytes to the LAN (%s)", msg.size(), broadcastAddress); + log_verbose("Broadcasting %zu bytes to the LAN (%s)", msg.size(), broadcastAddress.c_str()); auto len = udpSocket->SendData(broadcastAddress, NETWORK_LAN_BROADCAST_PORT, msg.data(), msg.size()); if (len != msg.size()) { @@ -318,9 +319,11 @@ std::future> ServerList::FetchLocalServerListAsync( std::future> ServerList::FetchOnlineServerListAsync() { -#ifdef DISABLE_HTTP +# ifdef DISABLE_HTTP return std::async(std::launch::deferred, [] { return std::vector(); }); -#else +# else + using namespace OpenRCT2::Network; + auto p = std::make_shared>>(); auto f = p->get_future(); @@ -386,7 +389,7 @@ std::future> ServerList::FetchOnlineServerListAsync json_decref(root); }); return f; -#endif +# endif } uint32_t ServerList::GetTotalPlayerCount() const @@ -395,3 +398,5 @@ uint32_t ServerList::GetTotalPlayerCount() const return acc + entry.players; }); } + +#endif