diff --git a/src/openrct2-ui/windows/ServerList.cpp b/src/openrct2-ui/windows/ServerList.cpp index f7f2b4f930..d5993e9c6d 100644 --- a/src/openrct2-ui/windows/ServerList.cpp +++ b/src/openrct2-ui/windows/ServerList.cpp @@ -33,6 +33,8 @@ #include #include +using namespace OpenRCT2::Network; + #define WWIDTH_MIN 500 #define WHEIGHT_MIN 300 #define WWIDTH_MAX 1200 @@ -136,7 +138,7 @@ static void sort_servers(); static void join_server(std::string address); static void fetch_servers(); #ifndef DISABLE_HTTP -static void fetch_servers_callback(http::Response & response); +static void fetch_servers_callback(Http::Response & response); #endif static bool is_version_valid(const std::string &version); @@ -633,12 +635,12 @@ static void fetch_servers() sort_servers(); } - http::Request request; + Http::Request request; request.url = masterServerUrl; - request.method = http::Method::GET; + request.method = Http::Method::GET; request.header["Accept"] = "application/json"; status_text = STR_SERVER_LIST_CONNECTING; - http::DoAsync(request, fetch_servers_callback); + Http::DoAsync(request, fetch_servers_callback); #endif } @@ -655,9 +657,9 @@ static uint32 get_total_player_count() }); } -static void fetch_servers_callback(http::Response & response) +static void fetch_servers_callback(Http::Response & response) { - if (response.status != http::Status::OK) + if (response.status != Http::Status::OK) { status_text = STR_SERVER_LIST_NO_CONNECTION; window_invalidate_by_class(WC_SERVER_LIST); diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 92afa6fc87..010a1b8e2a 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -104,7 +104,7 @@ namespace OpenRCT2 #endif StdInOutConsole _stdInOutConsole; #ifndef DISABLE_HTTP - http::HTTP _http; + Network::Http::Http _http; #endif // Game states @@ -720,7 +720,7 @@ namespace OpenRCT2 #ifndef DISABLE_HTTP // Download park and open it using its temporary filename void * data; - size_t dataSize = http::download_park(gOpenRCT2StartupActionPath, &data); + size_t dataSize = Network::Http::DownloadPark(gOpenRCT2StartupActionPath, &data); if (dataSize == 0) { title_load(); diff --git a/src/openrct2/network/Http.cpp b/src/openrct2/network/Http.cpp index c44747e410..78ac80ba10 100644 --- a/src/openrct2/network/Http.cpp +++ b/src/openrct2/network/Http.cpp @@ -33,15 +33,15 @@ #define OPENRCT2_USER_AGENT "OpenRCT2/" OPENRCT2_VERSION -namespace http +namespace OpenRCT2::Network::Http { -HTTP::HTTP() +Http::Http() { curl_global_init(CURL_GLOBAL_DEFAULT); } -HTTP::~HTTP() +Http::~Http() { curl_global_cleanup(); } @@ -178,7 +178,7 @@ void DoAsync(const Request & req, std::function fn) Response res; try { - res = http::Do(req); + res = Do(req); } catch (std::exception & e) { @@ -190,7 +190,7 @@ void DoAsync(const Request & req, std::function fn) thread.detach(); } -size_t download_park(const char * url, void ** outData) +size_t DownloadPark(const char * url, void ** outData) { // Download park to buffer in memory Request request; @@ -225,6 +225,6 @@ size_t download_park(const char * url, void ** outData) return dataSize; } -} // namespace http +} // namespace OpenRCT2::Network #endif diff --git a/src/openrct2/network/Http.h b/src/openrct2/network/Http.h index b92b12f231..06ba37d605 100644 --- a/src/openrct2/network/Http.h +++ b/src/openrct2/network/Http.h @@ -14,8 +14,7 @@ *****************************************************************************/ #pragma endregion -#ifndef OPENRCT2_NETWORK_HTTP_H_ -#define OPENRCT2_NETWORK_HTTP_H_ +#pragma once #ifndef DISABLE_HTTP @@ -25,7 +24,7 @@ #include "../common.h" -namespace http +namespace OpenRCT2::Network::Http { enum class Status @@ -44,24 +43,24 @@ struct Response { Status status; std::string content_type; - std::string body; - std::map header; - std::string error; + std::string body = ""; + std::map header = {}; + std::string error = ""; }; struct Request { std::string url; - std::map header; - Method method; - std::string body; - bool forceIPv4; + std::map header = {}; + Method method = Method::GET; + std::string body = ""; + bool forceIPv4 = false; }; -struct HTTP +struct Http { - HTTP(); - ~HTTP(); + Http(); + ~Http(); }; Response Do(const Request & req); @@ -74,10 +73,8 @@ void DoAsync(const Request & req, std::function fn); * @param outData The data returned. * @returns The size of the data or 0 if the download failed. */ -size_t download_park(const char * url, void ** outData); +size_t DownloadPark(const char * url, void ** outData); } /* namespace http */ #endif // DISABLE_HTTP - -#endif // OPENRCT2_NETWORK_HTTP_H diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index b6e6f7b561..40189a690a 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -33,6 +33,8 @@ #include "../world/Park.h" #include "Http.h" +using namespace OpenRCT2::Network; + #ifndef DISABLE_HTTP enum MASTER_SERVER_STATUS @@ -103,9 +105,9 @@ private: _lastAdvertiseTime = platform_get_ticks(); // Send the registration request - http::Request request; + Http::Request request; request.url = GetMasterServerUrl(); - request.method = http::Method::POST; + request.method = Http::Method::POST; request.forceIPv4 = forceIPv4; json_t *body = json_object(); @@ -114,8 +116,8 @@ private: request.body = json_dumps(body, JSON_COMPACT); request.header["Content-Type"] = "application/json"; - http::DoAsync(request, [&](http::Response response) -> void { - if (response.status != http::Status::OK) + Http::DoAsync(request, [&](Http::Response response) -> void { + if (response.status != Http::Status::OK) { Console::WriteLine("Unable to connect to master server"); return; @@ -130,17 +132,17 @@ private: void SendHeartbeat() { - http::Request request; + Http::Request request; request.url = GetMasterServerUrl(); - request.method = http::Method::POST; + request.method = Http::Method::POST; json_t * body = GetHeartbeatJson(); request.body = json_dumps(body, JSON_COMPACT); request.header["Content-Type"] = "application/json"; _lastHeartbeatTime = platform_get_ticks(); - http::DoAsync(request, [&](http::Response response) -> void { - if (response.status != http::Status::OK) + Http::DoAsync(request, [&](Http::Response response) -> void { + if (response.status != Http::Status::OK) { Console::WriteLine("Unable to connect to master server"); return; diff --git a/src/openrct2/network/Twitch.cpp b/src/openrct2/network/Twitch.cpp index 83d1da3aee..a823286527 100644 --- a/src/openrct2/network/Twitch.cpp +++ b/src/openrct2/network/Twitch.cpp @@ -50,6 +50,7 @@ #include "twitch.h" using namespace OpenRCT2; +using namespace OpenRCT2::Network; bool gTwitchEnable = false; @@ -112,7 +113,7 @@ namespace Twitch static bool _twitchIdle = true; static uint32 _twitchLastPulseTick = 0; static sint32 _twitchLastPulseOperation = 1; - static http::Response _twitchJsonResponse; + static Http::Response _twitchJsonResponse; static void Join(); static void Leave(); @@ -208,14 +209,14 @@ namespace Twitch _twitchState = TWITCH_STATE_JOINING; _twitchIdle = false; - http::Request request; + Http::Request request; request.url = url; - request.method = http::Method::GET; + request.method = Http::Method::GET; - http::DoAsync(request, [](http::Response res) { + Http::DoAsync(request, [](Http::Response res) { std::shared_ptr _(nullptr, [&](...) { _twitchIdle = true; }); - if (res.status != http::Status::OK) + if (res.status != Http::Status::OK) { _twitchState = TWITCH_STATE_LEFT; GetContext()->WriteLine("Unable to connect to twitch channel."); @@ -284,10 +285,10 @@ namespace Twitch _twitchState = TWITCH_STATE_WAITING; _twitchIdle = false; - http::DoAsync({ url }, [](http::Response res) { + Http::DoAsync({ url }, [](Http::Response res) { std::shared_ptr _(nullptr, [&](...) { _twitchIdle = true; }); - if (res.status != http::Status::OK) + if (res.status != Http::Status::OK) { _twitchState = TWITCH_STATE_JOINED; return; @@ -316,10 +317,10 @@ namespace Twitch _twitchState = TWITCH_STATE_WAITING; _twitchIdle = false; - http::DoAsync({ url }, [](http::Response res) { + Http::DoAsync({ url }, [](Http::Response res) { std::shared_ptr _(nullptr, [&](...) { _twitchIdle = true; }); - if (res.status != http::Status::OK) + if (res.status != Http::Status::OK) { _twitchState = TWITCH_STATE_JOINED; return;