add Network.httpGet (WIP)
This commit is contained in:
parent
a5d87c9fc4
commit
46dfdac998
60
src/network/Network.cpp
Normal file
60
src/network/Network.cpp
Normal file
@ -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));
|
||||||
|
}
|
||||||
31
src/network/Network.hpp
Normal file
31
src/network/Network.hpp
Normal file
@ -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};
|
IntegerSetting worldPreviewSize {64, 1, 512};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct NetworkSettings {
|
||||||
|
};
|
||||||
|
|
||||||
struct EngineSettings {
|
struct EngineSettings {
|
||||||
AudioSettings audio;
|
AudioSettings audio;
|
||||||
DisplaySettings display;
|
DisplaySettings display;
|
||||||
@ -89,4 +92,5 @@ struct EngineSettings {
|
|||||||
GraphicsSettings graphics;
|
GraphicsSettings graphics;
|
||||||
DebugSettings debug;
|
DebugSettings debug;
|
||||||
UiSettings ui;
|
UiSettings ui;
|
||||||
|
NetworkSettings network;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
#include <gtest/gtest.h>
|
|
||||||
|
|
||||||
#include <curl/curl.h>
|
|
||||||
|
|
||||||
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
|
|
||||||
return size * nmemb;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(curltest, curltest) {
|
|
||||||
if (CURL* curl = curl_easy_init()) {
|
|
||||||
CURLcode res;
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, "https://github.com");
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
|
||||||
res = curl_easy_perform(curl);
|
|
||||||
std::cout << curl_easy_strerror(res) << std::endl;
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
test/network/curltest.cpp
Normal file
18
test/network/curltest.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "network/Network.hpp"
|
||||||
|
#include "coders/json.hpp"
|
||||||
|
|
||||||
|
TEST(curltest, curltest) {
|
||||||
|
NetworkSettings settings {};
|
||||||
|
auto network = network::Network::create(settings);
|
||||||
|
network->httpGet(
|
||||||
|
"https://raw.githubusercontent.com/MihailRis/VoxelEngine-Cpp/refs/"
|
||||||
|
"heads/curl/res/content/base/blocks/lamp.json",
|
||||||
|
[=](int code, std::vector<char> data) {
|
||||||
|
auto v = std::string_view(data.data(), data.size());
|
||||||
|
auto value = json::parse(v);
|
||||||
|
std::cout << value << std::endl;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user