1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-16 11:33:03 +01:00

Rename Http namespace, add default initialisers.

This commit is contained in:
Aaron van Geffen
2018-06-11 11:18:41 +02:00
parent 62c25d88fb
commit f1d4e5b596
6 changed files with 49 additions and 47 deletions

View File

@@ -33,6 +33,8 @@
#include <openrct2/interface/Colour.h>
#include <openrct2/drawing/Drawing.h>
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);

View File

@@ -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();

View File

@@ -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<void(Response & res)> 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<void(Response & res)> 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

View File

@@ -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<std::string, std::string> header;
std::string error;
std::string body = "";
std::map<std::string, std::string> header = {};
std::string error = "";
};
struct Request
{
std::string url;
std::map<std::string, std::string> header;
Method method;
std::string body;
bool forceIPv4;
std::map<std::string, std::string> 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<void(Response & res)> 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

View File

@@ -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;

View File

@@ -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<void> _(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<void> _(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<void> _(nullptr, [&](...) { _twitchIdle = true; });
if (res.status != http::Status::OK)
if (res.status != Http::Status::OK)
{
_twitchState = TWITCH_STATE_JOINED;
return;