Started work on networked test for ecs
This commit is contained in:
parent
e43dbea6b1
commit
f77c9d7823
25
flint.lua
25
flint.lua
|
@ -31,10 +31,10 @@ lib "ecs-serial"
|
||||||
|
|
||||||
dependency("ecs", "iohelper")
|
dependency("ecs", "iohelper")
|
||||||
|
|
||||||
function codegen(file)
|
function codegen(path, file)
|
||||||
local handle = io.popen("mkdir " .. config.paths.build .. "/generated")
|
local handle = io.popen("mkdir " .. config.paths.build .. "/generated")
|
||||||
handle:close()
|
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 = io.popen(command)
|
||||||
handle:close()
|
handle:close()
|
||||||
end
|
end
|
||||||
|
@ -45,9 +45,26 @@ executable "test"
|
||||||
dependency "ecs-lua"
|
dependency "ecs-lua"
|
||||||
dependency "ecs-serial"
|
dependency "ecs-serial"
|
||||||
|
|
||||||
hook(step.PRE_BUILD, codegen, "components.h")
|
hook(step.PRE_BUILD, codegen, "test/include", "components.h")
|
||||||
|
|
||||||
include(config.paths.build .. "/generated/")
|
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_dir "test"
|
||||||
run_target "test"
|
run_target "test"
|
||||||
|
|
43
network-client/src/client.cpp
Normal file
43
network-client/src/client.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
94
network-server/src/server.cpp
Normal file
94
network-server/src/server.cpp
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#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();
|
||||||
|
}
|
10
network-shared/include/network_components.h
Normal file
10
network-shared/include/network_components.h
Normal file
|
@ -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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user