add Network.httpGet (WIP)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
#include "Network.hpp"
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
using namespace network;
|
||||
|
||||
static debug::Logger logger("network");
|
||||
|
||||
size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
|
||||
auto& buffer = *reinterpret_cast<std::vector<char>*>(userdata);
|
||||
size_t psize = buffer.size();
|
||||
buffer.resize(psize + size * nmemb);
|
||||
std::memcpy(buffer.data() + psize, ptr, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
class CurlHttp : public Http {
|
||||
CURL* curl;
|
||||
public:
|
||||
CurlHttp(CURL* curl) : curl(curl) {
|
||||
}
|
||||
|
||||
virtual ~CurlHttp() {
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
|
||||
void get(const std::string& url, const OnResponse& callback) override {
|
||||
std::vector<char> buffer;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
callback(res, std::move(buffer));
|
||||
}
|
||||
|
||||
static std::unique_ptr<CurlHttp> create() {
|
||||
if (auto curl = curl_easy_init()) {
|
||||
return std::make_unique<CurlHttp>(curl);
|
||||
}
|
||||
throw std::runtime_error("could not initialzie cURL");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Network::Network(std::unique_ptr<Http> http) : http(std::move(http)) {
|
||||
}
|
||||
|
||||
Network::~Network() = default;
|
||||
|
||||
void Network::httpGet(const std::string& url, const OnResponse& callback) {
|
||||
http->get(url, callback);
|
||||
}
|
||||
|
||||
std::unique_ptr<Network> Network::create(const NetworkSettings& settings) {
|
||||
auto http = CurlHttp::create();
|
||||
return std::make_unique<Network>(std::move(http));
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "typedefs.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
|
||||
namespace network {
|
||||
using OnResponse = std::function<void(int, std::vector<char>)>;
|
||||
|
||||
class Http {
|
||||
public:
|
||||
virtual ~Http() {}
|
||||
|
||||
virtual void get(const std::string& url, const OnResponse& callback) = 0;
|
||||
};
|
||||
|
||||
class Network {
|
||||
std::unique_ptr<Http> http;
|
||||
public:
|
||||
Network(std::unique_ptr<Http> http);
|
||||
~Network();
|
||||
|
||||
void httpGet(const std::string& url, const OnResponse& callback);
|
||||
|
||||
static std::unique_ptr<Network> create(const NetworkSettings& settings);
|
||||
};
|
||||
}
|
||||
@@ -81,6 +81,9 @@ struct UiSettings {
|
||||
IntegerSetting worldPreviewSize {64, 1, 512};
|
||||
};
|
||||
|
||||
struct NetworkSettings {
|
||||
};
|
||||
|
||||
struct EngineSettings {
|
||||
AudioSettings audio;
|
||||
DisplaySettings display;
|
||||
@@ -89,4 +92,5 @@ struct EngineSettings {
|
||||
GraphicsSettings graphics;
|
||||
DebugSettings debug;
|
||||
UiSettings ui;
|
||||
NetworkSettings network;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user