From f77c9d7823b751802aaef4f1c1e01029efbe5907 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 28 Jun 2019 03:18:54 +0200 Subject: [PATCH] Started work on networked test for ecs --- flint.lua | 25 +++++- network-client/src/client.cpp | 43 ++++++++++ network-server/src/server.cpp | 94 +++++++++++++++++++++ network-shared/include/network_components.h | 10 +++ 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 network-client/src/client.cpp create mode 100644 network-server/src/server.cpp create mode 100644 network-shared/include/network_components.h diff --git a/flint.lua b/flint.lua index f0f201e..320c6b0 100644 --- a/flint.lua +++ b/flint.lua @@ -31,10 +31,10 @@ lib "ecs-serial" dependency("ecs", "iohelper") -function codegen(file) +function codegen(path, file) local handle = io.popen("mkdir " .. config.paths.build .. "/generated") handle:close() - local command = "python test.py test/include/" .. file .. " > " .. config.paths.build .. "/generated/ecs_" .. file + local command = "python test.py " .. path .. "/" .. file .. " > " .. config.paths.build .. "/generated/ecs_" .. file handle = io.popen(command) handle:close() end @@ -45,9 +45,26 @@ executable "test" dependency "ecs-lua" dependency "ecs-serial" - hook(step.PRE_BUILD, codegen, "components.h") - + hook(step.PRE_BUILD, codegen, "test/include", "components.h") include(config.paths.build .. "/generated/") +lib "network-shared" + path "network-shared" + + dependency "ecs-serial" + + hook(step.PRE_BUILD, codegen, "network-shared/include", "network_components.h") + include(config.paths.build .. "/generated/") + +executable "server" + path "network-server" + + dependency "network-shared" + + threads() + +executable "client" + path "network-client" + run_dir "test" run_target "test" diff --git a/network-client/src/client.cpp b/network-client/src/client.cpp new file mode 100644 index 0000000..bd55d8d --- /dev/null +++ b/network-client/src/client.cpp @@ -0,0 +1,43 @@ +#include + +#include +#include + +int main() { + // IPv4 + int address_family = AF_INET; + // UDP + int type = SOCK_DGRAM; + int protocol = 0; + + int sock = socket(address_family, type, protocol); + + if (sock < 0) { + throw std::runtime_error("Failed to create socket!"); + } + + sockaddr_in server_address; + server_address.sin_family = AF_INET; + server_address.sin_port = htons(9999); + server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); + + sockaddr* to = (sockaddr*)&server_address; + int to_size = sizeof(server_address); + + int8_t buffer[1024]; + buffer[0] = 'c'; + + if (sendto(sock, buffer, 1024, 0, to, to_size) < 0) { + throw std::runtime_error("Failed to connect to server"); + } + + sockaddr_in from; + socklen_t from_size = sizeof(from); + int bytes_received = recvfrom(sock, buffer, 1024, 0, (sockaddr*)&from, &from_size); + + if (bytes_received < 0) { + throw std::runtime_error("Failed to receive data!"); + } + + std::cout << "Connected to server!\n"; +} diff --git a/network-server/src/server.cpp b/network-server/src/server.cpp new file mode 100644 index 0000000..a257148 --- /dev/null +++ b/network-server/src/server.cpp @@ -0,0 +1,94 @@ +#include +#include +#include + +#include + +#include "ecs-serial.h" +#include "ecs_network_components.h" + +ecs::Manager manager; + +void on_connect(int sock, sockaddr_in from) { + uint32_t ip = from.sin_addr.s_addr; + + uint32_t bytes[4]; + bytes[0] = ip & 0xff; + bytes[1] = (ip >> 8) & 0xff; + bytes[2] = (ip >> 16) & 0xff; + bytes[3] = (ip >> 24) & 0xff; + + std::cout << "Client connected: " << bytes[0] << '.' << bytes[1] << '.' << bytes[2] << '.' << bytes[3] << '\n'; + + uint8_t buffer[1024]; + buffer[0] = 'a'; + if (sendto(sock, buffer, 1024, 0, (sockaddr*)&from, sizeof(from)) < 0) { + throw std::runtime_error("Failed to send ack to client"); + } + + // Send ids and all entities + // @todo We need to somehow write to the socket + // @todo Make everything thread safe + ecs::serial::serialize_ids(std::cout); +} + +// For now we will just send the entire state every tick +void on_update() { + // Depending on the update type send it to all clients + // New component added -> send new id + // Component updated -> send updated component +} + +void listener(int sock) { + // @todo Replace true with an atomic + while (true) { + int8_t buffer[1024]; + // Wait on initial connection + sockaddr_in from; + socklen_t from_size = sizeof(from); + int bytes_received = recvfrom(sock, buffer, 1024, 0, (sockaddr*)&from, &from_size); + + if (bytes_received < 0) { + throw std::runtime_error("Failed to receive data!"); + } + + switch (buffer[0]) { + case 'c': + on_connect(sock, from); + break; + } + } +} + +int main() { + generated::init(); + + int address_family = AF_INET; + int type = SOCK_DGRAM; + int protocol = 0; + + int sock = socket(address_family, type, protocol); + + if (sock < 0) { + throw std::runtime_error("Failed to create socket!"); + } + + sockaddr_in local_address; + local_address.sin_family = AF_INET; + local_address.sin_port = htons(9999); + local_address.sin_addr.s_addr = INADDR_ANY; + + if (bind(sock, (sockaddr*)&local_address, sizeof(local_address)) < 0) { + throw std::runtime_error("Failed to bind to port!"); + } + + std::thread lthread(std::bind(listener, sock)); + + std::cout << "Server started!\n"; + + while (true) { + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } + + lthread.join(); +} diff --git a/network-shared/include/network_components.h b/network-shared/include/network_components.h new file mode 100644 index 0000000..ed6fb61 --- /dev/null +++ b/network-shared/include/network_components.h @@ -0,0 +1,10 @@ +#pragma once +#include "ecs.h" + +struct Position : ecs::Component { + Position(float _x, float _y) : x(_x), y(_y) {} + Position() {} + + float x = 0; + float y = 0; +};