add network.http_get, network.http_get_binary

This commit is contained in:
MihailRis 2024-11-22 07:30:38 +03:00
parent 186078a8d5
commit b23318a06c
6 changed files with 62 additions and 2 deletions

View File

@ -30,6 +30,7 @@
#include "logic/EngineController.hpp"
#include "logic/CommandsInterpreter.hpp"
#include "logic/scripting/scripting.hpp"
#include "network/Network.hpp"
#include "util/listutil.hpp"
#include "util/platform.hpp"
#include "window/Camera.hpp"
@ -72,7 +73,8 @@ static std::unique_ptr<ImageData> load_icon(const fs::path& resdir) {
Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, EnginePaths* paths)
: settings(settings), settingsHandler(settingsHandler), paths(paths),
interpreter(std::make_unique<cmd::CommandsInterpreter>())
interpreter(std::make_unique<cmd::CommandsInterpreter>()),
network(network::Network::create(settings.network))
{
paths->prepare();
loadSettings();
@ -191,6 +193,7 @@ void Engine::mainloop() {
: settings.display.framerate.get()
);
network->update();
processPostRunnables();
Window::swapBuffers();
@ -235,6 +238,7 @@ Engine::~Engine() {
gui.reset();
logger.info() << "gui finished";
audio::close();
network.reset();
scripting::close();
logger.info() << "scripting finished";
Window::terminate();
@ -482,3 +486,7 @@ void Engine::postRunnable(const runnable& callback) {
SettingsHandler& Engine::getSettingsHandler() {
return settingsHandler;
}
network::Network& Engine::getNetwork() {
return *network;
}

View File

@ -37,6 +37,10 @@ namespace cmd {
class CommandsInterpreter;
}
namespace network {
class Network;
}
class initialize_error : public std::runtime_error {
public:
initialize_error(const std::string& message) : std::runtime_error(message) {}
@ -56,6 +60,7 @@ class Engine : public util::ObjectsKeeper {
std::recursive_mutex postRunnablesMutex;
std::unique_ptr<EngineController> controller;
std::unique_ptr<cmd::CommandsInterpreter> interpreter;
std::unique_ptr<network::Network> network;
std::vector<std::string> basePacks;
uint64_t frame = 0;
@ -148,4 +153,6 @@ public:
PacksManager createPacksManager(const fs::path& worldFolder);
SettingsHandler& getSettingsHandler();
network::Network& getNetwork();
};

View File

@ -32,6 +32,7 @@ extern const luaL_Reg inventorylib[];
extern const luaL_Reg itemlib[];
extern const luaL_Reg jsonlib[];
extern const luaL_Reg mat4lib[];
extern const luaL_Reg networklib[];
extern const luaL_Reg packlib[];
extern const luaL_Reg particleslib[]; // gfx.particles
extern const luaL_Reg playerlib[];

View File

@ -0,0 +1,43 @@
#include "api_lua.hpp"
#include "engine.hpp"
#include "network/Network.hpp"
using namespace scripting;
static int l_http_get(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda(L);
engine->getNetwork().httpGet(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
});
return 0;
}
static int l_http_get_binary(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda(L);
engine->getNetwork().httpGet(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
);
engine->postRunnable([=]() {
onResponse({buffer});
});
});
return 0;
}
const luaL_Reg networklib[] = {
{"http_get", lua::wrap<l_http_get>},
{"http_get_binary", lua::wrap<l_http_get_binary>},
{NULL, NULL}
};

View File

@ -65,6 +65,7 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "player", playerlib);
openlib(L, "network", networklib);
openlib(L, "entities", entitylib);
openlib(L, "cameras", cameralib);

View File

@ -16,7 +16,7 @@ TEST(curltest, curltest) {
auto view = std::string_view(data.data(), data.size());
auto value = json::parse(view);
std::cout << value << std::endl;
}
}, [](auto){}
);
if (false) {
auto socket = network->connect("localhost", 8000);