Implemented more networking
This commit is contained in:
parent
f77c9d7823
commit
ac463f0669
|
@ -60,11 +60,14 @@ executable "server"
|
||||||
path "network-server"
|
path "network-server"
|
||||||
|
|
||||||
dependency "network-shared"
|
dependency "network-shared"
|
||||||
|
dependency "iohelper"
|
||||||
|
|
||||||
threads()
|
threads()
|
||||||
|
|
||||||
executable "client"
|
executable "client"
|
||||||
path "network-client"
|
path "network-client"
|
||||||
|
|
||||||
|
dependency "network-shared"
|
||||||
|
|
||||||
run_dir "test"
|
run_dir "test"
|
||||||
run_target "test"
|
run_target "test"
|
||||||
|
|
|
@ -1,9 +1,21 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "ecs-serial.h"
|
||||||
|
#include "ecs_network_components.h"
|
||||||
|
|
||||||
|
#include "iohelper/memstream.h"
|
||||||
|
|
||||||
|
#include "packets.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
ecs::Manager manager;
|
||||||
|
generated::init();
|
||||||
|
|
||||||
// IPv4
|
// IPv4
|
||||||
int address_family = AF_INET;
|
int address_family = AF_INET;
|
||||||
// UDP
|
// UDP
|
||||||
|
@ -24,20 +36,65 @@ int main() {
|
||||||
sockaddr* to = (sockaddr*)&server_address;
|
sockaddr* to = (sockaddr*)&server_address;
|
||||||
int to_size = sizeof(server_address);
|
int to_size = sizeof(server_address);
|
||||||
|
|
||||||
int8_t buffer[1024];
|
std::array<uint8_t, 1024> buff;
|
||||||
buffer[0] = 'c';
|
// @todo Implement iomemstream
|
||||||
|
iohelper::omemstream_fixed<buff.size()> buff_stream_out(buff);
|
||||||
|
iohelper::imemstream_fixed<buff.size()> buff_stream_in(buff);
|
||||||
|
|
||||||
if (sendto(sock, buffer, 1024, 0, to, to_size) < 0) {
|
iohelper::write<uint8_t>(buff_stream_out, PACKET::CONNECT);
|
||||||
|
|
||||||
|
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
|
||||||
throw std::runtime_error("Failed to connect to server");
|
throw std::runtime_error("Failed to connect to server");
|
||||||
}
|
}
|
||||||
|
|
||||||
sockaddr_in from;
|
sockaddr_in from;
|
||||||
socklen_t from_size = sizeof(from);
|
socklen_t from_size = sizeof(from);
|
||||||
int bytes_received = recvfrom(sock, buffer, 1024, 0, (sockaddr*)&from, &from_size);
|
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, &from_size);
|
||||||
|
|
||||||
if (bytes_received < 0) {
|
if (bytes_received < 0) {
|
||||||
throw std::runtime_error("Failed to receive data!");
|
throw std::runtime_error("Failed to receive data!");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Connected to server!\n";
|
if (iohelper::read<uint8_t>(buff_stream_in) == PACKET::ACK) {
|
||||||
|
std::cout << "Connected to server!\n";
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Unexpected server response");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
buff_stream_in.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, &from_size);
|
||||||
|
|
||||||
|
if (bytes_received < 0) {
|
||||||
|
throw std::runtime_error("Failed to receive data!");
|
||||||
|
}
|
||||||
|
|
||||||
|
PACKET type = (PACKET)iohelper::read<uint8_t>(buff_stream_in);
|
||||||
|
switch (type) {
|
||||||
|
case PACKET::IDS:
|
||||||
|
std::cout << "Receiving ids\n";
|
||||||
|
ecs::serial::deserialize_ids(buff_stream_in);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PACKET::ENTITIES: {
|
||||||
|
std::cout << "Receiving entities\n";
|
||||||
|
size_t entity_count = iohelper::read_length(buff_stream_in);
|
||||||
|
for (size_t i = 0; i < entity_count; ++i) {
|
||||||
|
ecs::serial::deserialize(buff_stream_in, manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.view<Position>().for_each([](ecs::Entity* entity, Position* position) {
|
||||||
|
std::cout << entity->uuid << ' ' << position->x << ' ' << position->y << '\n';
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
std::cout << "Unknown packet type: " << (uint32_t)type << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
// dump_buffer<buff.size()>(buff);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#include "ecs-serial.h"
|
#include "ecs-serial.h"
|
||||||
#include "ecs_network_components.h"
|
#include "ecs_network_components.h"
|
||||||
|
|
||||||
|
#include "iohelper/memstream.h"
|
||||||
|
|
||||||
|
#include "packets.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
ecs::Manager manager;
|
ecs::Manager manager;
|
||||||
|
|
||||||
void on_connect(int sock, sockaddr_in from) {
|
void on_connect(int sock, sockaddr_in from) {
|
||||||
|
@ -20,40 +26,84 @@ void on_connect(int sock, sockaddr_in from) {
|
||||||
|
|
||||||
std::cout << "Client connected: " << bytes[0] << '.' << bytes[1] << '.' << bytes[2] << '.' << bytes[3] << '\n';
|
std::cout << "Client connected: " << bytes[0] << '.' << bytes[1] << '.' << bytes[2] << '.' << bytes[3] << '\n';
|
||||||
|
|
||||||
uint8_t buffer[1024];
|
std::array<uint8_t, 1024> buff;
|
||||||
buffer[0] = 'a';
|
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
||||||
if (sendto(sock, buffer, 1024, 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
iohelper::write<uint8_t>(buff_stream, PACKET::ACK);
|
||||||
|
|
||||||
|
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
||||||
throw std::runtime_error("Failed to send ack to client");
|
throw std::runtime_error("Failed to send ack to client");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dump_buffer<buff.size()>(buff);
|
||||||
|
|
||||||
// Send ids and all entities
|
// Send ids and all entities
|
||||||
// @todo We need to somehow write to the socket
|
|
||||||
// @todo Make everything thread safe
|
// @todo Make everything thread safe
|
||||||
ecs::serial::serialize_ids(std::cout);
|
buff_stream.seekp(0, std::ios::beg);
|
||||||
}
|
iohelper::write<uint8_t>(buff_stream, PACKET::IDS);
|
||||||
|
ecs::serial::serialize_ids(buff_stream);
|
||||||
// For now we will just send the entire state every tick
|
|
||||||
void on_update() {
|
std::cout << "Sending ids\n";
|
||||||
// Depending on the update type send it to all clients
|
|
||||||
// New component added -> send new id
|
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
||||||
// Component updated -> send updated component
|
throw std::runtime_error("Failed to send ids to client");
|
||||||
|
}
|
||||||
|
|
||||||
|
// dump_buffer<buff.size()>(buff);
|
||||||
|
|
||||||
|
std::cout << "Sending entities\n";
|
||||||
|
|
||||||
|
buff_stream.seekp(0, std::ios::beg);
|
||||||
|
iohelper::write<uint8_t>(buff_stream, PACKET::ENTITIES);
|
||||||
|
iohelper::write_length(buff_stream, manager.view<>().size());
|
||||||
|
for (auto [uuid, entity] : manager.view<>()) {
|
||||||
|
ecs::serial::serialize(buff_stream, entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
||||||
|
throw std::runtime_error("Failed to send ids to client");
|
||||||
|
}
|
||||||
|
|
||||||
|
// THIS IS A TEST
|
||||||
|
std::cout << "Sending update\n";
|
||||||
|
|
||||||
|
manager.view<Position>().for_each([](ecs::Entity* /* entity */, Position* position) {
|
||||||
|
if (int(position->y * 10) % 2 == 0) {
|
||||||
|
position->y *= 2;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// @todo Figure out how to delta everything
|
||||||
|
buff_stream.seekp(0, std::ios::beg);
|
||||||
|
iohelper::write<uint8_t>(buff_stream, PACKET::ENTITIES);
|
||||||
|
iohelper::write_length(buff_stream, manager.view<>().size());
|
||||||
|
for (auto [uuid, entity] : manager.view<>()) {
|
||||||
|
ecs::serial::serialize(buff_stream, entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
||||||
|
throw std::runtime_error("Failed to send ids to client");
|
||||||
|
}
|
||||||
|
// dump_buffer<buff.size()>(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is the loop that listens for messages from clients
|
||||||
void listener(int sock) {
|
void listener(int sock) {
|
||||||
// @todo Replace true with an atomic
|
std::array<uint8_t, 1024> buff;
|
||||||
while (true) {
|
iohelper::imemstream_fixed<buff.size()> buff_stream(buff);
|
||||||
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);
|
|
||||||
|
|
||||||
|
sockaddr_in from;
|
||||||
|
socklen_t from_size = sizeof(from);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
buff_stream.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, &from_size);
|
||||||
if (bytes_received < 0) {
|
if (bytes_received < 0) {
|
||||||
throw std::runtime_error("Failed to receive data!");
|
throw std::runtime_error("Failed to receive data!");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (buffer[0]) {
|
switch (iohelper::read<uint8_t>(buff_stream)) {
|
||||||
case 'c':
|
case PACKET::CONNECT:
|
||||||
on_connect(sock, from);
|
on_connect(sock, from);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -63,6 +113,17 @@ void listener(int sock) {
|
||||||
int main() {
|
int main() {
|
||||||
generated::init();
|
generated::init();
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
// We can create entities
|
||||||
|
ecs::Entity* entity = manager.create_entity();
|
||||||
|
// Then we can add components to them
|
||||||
|
entity->add_component<Position>(0.1f*i, 0.3f*i);
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.view<Position>().for_each([](ecs::Entity* entity, Position* position) {
|
||||||
|
std::cout << entity->uuid << ' ' << position->x << ' ' << position->y << '\n';
|
||||||
|
});
|
||||||
|
|
||||||
int address_family = AF_INET;
|
int address_family = AF_INET;
|
||||||
int type = SOCK_DGRAM;
|
int type = SOCK_DGRAM;
|
||||||
int protocol = 0;
|
int protocol = 0;
|
||||||
|
|
13
network-shared/include/debug.h
Normal file
13
network-shared/include/debug.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
template<int I>
|
||||||
|
void dump_buffer(std::array<uint8_t, I> &buff) {
|
||||||
|
for (uint32_t b : buff) {
|
||||||
|
std::cout << b << ' ';
|
||||||
|
}
|
||||||
|
std::cout << '\n';
|
||||||
|
}
|
||||||
|
|
10
network-shared/include/packets.h
Normal file
10
network-shared/include/packets.h
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
enum PACKET : uint8_t {
|
||||||
|
CONNECT,
|
||||||
|
ACK,
|
||||||
|
IDS,
|
||||||
|
ENTITIES
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user