Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network #369

Merged
merged 65 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
cb925d0
add libcurl
MihailRis Nov 10, 2024
a5d87c9
update appimage workflow
MihailRis Nov 10, 2024
46dfdac
add Network.httpGet (WIP)
MihailRis Nov 11, 2024
d7389c2
add Network.getTotalUpload, getTotalDownload
MihailRis Nov 11, 2024
4328c83
add httpGet error handling
MihailRis Nov 11, 2024
dc84fe1
add Network.connect (WIP)
MihailRis Nov 11, 2024
40ba770
update Socket.send, recv
MihailRis Nov 11, 2024
14b769f
Merge branch 'main' into curl
MihailRis Nov 15, 2024
cc603f5
update workflows
MihailRis Nov 15, 2024
ee0b745
update .github/workflows/cmake.yml
MihailRis Nov 15, 2024
4aacd13
update Socket interface
MihailRis Nov 15, 2024
0a3daad
fix
MihailRis Nov 15, 2024
81e8e30
Revert "update workflows"
MihailRis Nov 15, 2024
8160ebb
Merge branch 'main' into curl
MihailRis Nov 21, 2024
5092cff
update workflows
MihailRis Nov 21, 2024
0fec17a
fix file.write_bytes
MihailRis Nov 22, 2024
186078a
make httpGet non-blocking
MihailRis Nov 22, 2024
b23318a
add network.http_get, network.http_get_binary
MihailRis Nov 22, 2024
988adce
add missing include
MihailRis Nov 22, 2024
e2b4239
fix
MihailRis Nov 22, 2024
a222311
Merge branch 'main' into curl
MihailRis Nov 22, 2024
772fafb
Merge branch 'main' into curl
MihailRis Nov 23, 2024
5e063d9
refactor Network
MihailRis Nov 23, 2024
db5dada
Merge branch 'main' into curl
MihailRis Nov 23, 2024
90155dd
rename functions
MihailRis Nov 24, 2024
e8b674c
Merge branch 'main' into curl
MihailRis Nov 26, 2024
18bdce5
make socket non-blocking
MihailRis Nov 26, 2024
8e0a8d6
fix
MihailRis Nov 26, 2024
edd4170
minor refactor
MihailRis Nov 26, 2024
5e0d5b6
turn on failing test to check an error message
MihailRis Nov 26, 2024
419699b
update error handling code
MihailRis Nov 26, 2024
b999c06
fix
MihailRis Nov 26, 2024
13bdfef
fix
MihailRis Nov 26, 2024
353046b
turn off extra workflows to minimize spam
MihailRis Nov 26, 2024
f5c4a22
again
MihailRis Nov 26, 2024
992cfef
update error message
MihailRis Nov 26, 2024
85d9401
catch EINPROGRESS and WSAEWOULDBLOCK
MihailRis Nov 26, 2024
55de484
remove non-blocking mode
MihailRis Nov 26, 2024
aa7fa6b
add network.__connect
MihailRis Nov 26, 2024
5e4949f
revert workflows
MihailRis Nov 26, 2024
dbc2541
Merge branch 'main' into curl
MihailRis Nov 26, 2024
3933bac
remove double world.lua run in 'core'
MihailRis Nov 27, 2024
fb0f4bf
complete simple connection implementation
MihailRis Nov 27, 2024
32ba027
replace getContentPacks with getAllContentPacks in global events emit…
MihailRis Nov 27, 2024
233bc31
add network.__close
MihailRis Nov 27, 2024
a72d36f
add simple inefficient server socket implementation (WIP)
MihailRis Nov 27, 2024
9e8addb
fix msvc build
MihailRis Nov 27, 2024
1b04a2a
fix msvc build again
MihailRis Nov 27, 2024
847ef23
add network.__closeserver
MihailRis Nov 27, 2024
34974c4
add network.tcp_connect and .tcp_open
MihailRis Nov 27, 2024
951d2fd
add socket:is_alive, :is_connected, serversocket:is_open
MihailRis Nov 27, 2024
af5902f
fix network.tcp_connect
MihailRis Nov 27, 2024
495dd5f
add localhost 'network' library test
MihailRis Nov 27, 2024
449604d
Merge branch 'main' into curl
MihailRis Nov 27, 2024
7e0c95d
prevent uncaught exception in thread
MihailRis Nov 27, 2024
6af6eda
refactor Network
MihailRis Nov 27, 2024
43a838b
fix WSAEAFNOSUPPORT (10047)
MihailRis Nov 27, 2024
79e311d
fix curl cleanup
MihailRis Nov 27, 2024
6b8ee77
fix server shutdown with winsocks
MihailRis Nov 27, 2024
3b3407a
remove unused function
MihailRis Nov 27, 2024
637dfa6
add 'null' support to json parser
MihailRis Nov 27, 2024
6e90568
add util::escape 'escapeUnicode' option
MihailRis Nov 27, 2024
9b73e78
Merge branch 'main' into curl
MihailRis Nov 27, 2024
4662826
add network.get_total_upload, .get_total_download
MihailRis Nov 28, 2024
7e566a5
remove test script
MihailRis Nov 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/appimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ jobs:
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev libluajit-5.1-dev libvorbis-dev cmake squashfs-tools
sudo apt-get install -y build-essential libglfw3-dev libglfw3 libglew-dev \
libglm-dev libpng-dev libopenal-dev libluajit-5.1-dev libvorbis-dev libcurl4-openssl-dev cmake squashfs-tools
# fix luajit paths
sudo ln -s /usr/lib/x86_64-linux-gnu/libluajit-5.1.a /usr/lib/x86_64-linux-gnu/liblua5.1.a
sudo ln -s /usr/include/luajit-2.1 /usr/include/lua
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
# make && make install INSTALL_INC=/usr/include/lua
run: |
sudo apt-get update
sudo apt-get install libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev libluajit-5.1-dev libvorbis-dev libgtest-dev
sudo apt-get install libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev libluajit-5.1-dev libvorbis-dev libgtest-dev libcurl4-openssl-dev
# fix luajit paths
sudo ln -s /usr/lib/x86_64-linux-gnu/libluajit-5.1.a /usr/lib/x86_64-linux-gnu/liblua-5.1.a
sudo ln -s /usr/include/luajit-2.1 /usr/include/lua
Expand Down
28 changes: 28 additions & 0 deletions res/scripts/classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,31 @@ cameras.get = function(name)
wrappers[name] = wrapper
return wrapper
end


local Socket = {__index={
send=function(self, ...) return network.__send(self.id, ...) end,
recv=function(self, ...) return network.__recv(self.id, ...) end,
close=function(self) return network.__close(self.id) end,
is_alive=function(self) return network.__is_alive(self.id) end,
is_connected=function(self) return network.__is_connected(self.id) end,
}}

network.tcp_connect = function(address, port, callback)
local socket = setmetatable({id=0}, Socket)
socket.id = network.__connect(address, port, function(id)
callback(socket)
end)
return socket
end

local ServerSocket = {__index={
close=function(self) return network.__closeserver(self.id) end,
is_open=function(self) return network.__is_serveropen(self.id) end,
}}

network.tcp_open = function(port, handler)
return setmetatable({id=network.__open(port, function(id)
handler(setmetatable({id=id}, Socket))
end)}, ServerSocket)
end
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ find_package(GLEW REQUIRED)
find_package(OpenAL REQUIRED)
find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(CURL REQUIRED)
if (NOT APPLE)
find_package(EnTT REQUIRED)
endif()
Expand Down Expand Up @@ -61,5 +62,6 @@ if(UNIX)
endif()

include_directories(${LUA_INCLUDE_DIR})
include_directories(${CURL_INCLUDE_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ZLIB::ZLIB PNG::PNG ${VORBISLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS})
target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ZLIB::ZLIB PNG::PNG CURL::libcurl ${VORBISLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS})
4 changes: 3 additions & 1 deletion src/coders/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void stringifyValue(
break;
}
case value_type::string:
ss << util::escape(value.asString());
ss << util::escape(value.asString(), !nice);
break;
case value_type::number:
ss << std::setprecision(15) << value.asNumber();
Expand Down Expand Up @@ -241,6 +241,8 @@ dv::value Parser::parseValue() {
return INFINITY;
} else if (literal == "nan") {
return NAN;
} else if (literal == "null") {
return nullptr;
}
throw error("invalid keyword " + literal);
}
Expand Down
10 changes: 9 additions & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -191,6 +193,7 @@ void Engine::mainloop() {
: settings.display.framerate.get()
);

network->update();
processPostRunnables();

Window::swapBuffers();
Expand Down Expand Up @@ -235,6 +238,7 @@ Engine::~Engine() {
gui.reset();
logger.info() << "gui finished";
audio::close();
network.reset();
scripting::close();
logger.info() << "scripting finished";
Window::terminate();
Expand Down Expand Up @@ -485,3 +489,7 @@ void Engine::postRunnable(const runnable& callback) {
SettingsHandler& Engine::getSettingsHandler() {
return settingsHandler;
}

network::Network& Engine::getNetwork() {
return *network;
}
7 changes: 7 additions & 0 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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) {}
Expand All @@ -55,6 +59,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;
Expand Down Expand Up @@ -147,4 +152,6 @@ class Engine : public util::ObjectsKeeper {
PacksManager createPacksManager(const fs::path& worldFolder);

SettingsHandler& getSettingsHandler();

network::Network& getNetwork();
};
1 change: 1 addition & 0 deletions src/logic/scripting/lua/libs/api_lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
60 changes: 23 additions & 37 deletions src/logic/scripting/lua/libs/libfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,24 @@ static int l_read_bytes(lua::State* L) {
);
}

static int read_bytes_from_table(
static void read_bytes_from_table(
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) {
if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected");
} else {
lua::pushnil(L);
while (lua::next(L, tableIndex - 1) != 0) {
size_t size = lua::objlen(L, tableIndex);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, tableIndex);
const int byte = lua::tointeger(L, -1);
lua::pop(L);
if (byte < 0 || byte > 255) {
throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'"
);
}
bytes.push_back(byte);
lua::pop(L);
}
return 1;
}
}

Expand All @@ -181,14 +181,10 @@ static int l_write_bytes(lua::State* L) {
}

std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes);
if (result != 1) {
return result;
} else {
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
);
}
read_bytes_from_table(L, 2, bytes);
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
);
}

static int l_list_all_res(lua::State* L, const std::string& path) {
Expand Down Expand Up @@ -227,39 +223,29 @@ static int l_list(lua::State* L) {
static int l_gzip_compress(lua::State* L) {
std::vector<ubyte> bytes;

int result = read_bytes_from_table(L, -1, bytes);

if (result != 1) {
return result;
} else {
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);
read_bytes_from_table(L, 1, bytes);
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);

for (size_t i = 0; i < compressed_bytes.size(); i++) {
lua::pushinteger(L, compressed_bytes.data()[i]);
lua::rawseti(L, i + 1, newTable);
}
return 1;
for (size_t i = 0; i < compressed_bytes.size(); i++) {
lua::pushinteger(L, compressed_bytes.data()[i]);
lua::rawseti(L, i + 1, newTable);
}
return 1;
}

static int l_gzip_decompress(lua::State* L) {
std::vector<ubyte> bytes;

int result = read_bytes_from_table(L, -1, bytes);
read_bytes_from_table(L, 1, bytes);
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);

if (result != 1) {
return result;
} else {
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);

for (size_t i = 0; i < decompressed_bytes.size(); i++) {
lua::pushinteger(L, decompressed_bytes.data()[i]);
lua::rawseti(L, i + 1, newTable);
}
return 1;
for (size_t i = 0; i < decompressed_bytes.size(); i++) {
lua::pushinteger(L, decompressed_bytes.data()[i]);
lua::rawseti(L, i + 1, newTable);
}
return 1;
}

static int l_read_combined_list(lua::State* L) {
Expand Down
Loading
Loading