Implemented more networking
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <array>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "ecs-serial.h"
|
||||
#include "ecs_network_components.h"
|
||||
|
||||
#include "iohelper/memstream.h"
|
||||
|
||||
#include "packets.h"
|
||||
#include "debug.h"
|
||||
|
||||
ecs::Manager manager;
|
||||
|
||||
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';
|
||||
|
||||
uint8_t buffer[1024];
|
||||
buffer[0] = 'a';
|
||||
if (sendto(sock, buffer, 1024, 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
||||
std::array<uint8_t, 1024> buff;
|
||||
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
||||
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");
|
||||
}
|
||||
|
||||
// dump_buffer<buff.size()>(buff);
|
||||
|
||||
// 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
|
||||
buff_stream.seekp(0, std::ios::beg);
|
||||
iohelper::write<uint8_t>(buff_stream, PACKET::IDS);
|
||||
ecs::serial::serialize_ids(buff_stream);
|
||||
|
||||
std::cout << "Sending ids\n";
|
||||
|
||||
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);
|
||||
|
||||
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) {
|
||||
// @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);
|
||||
std::array<uint8_t, 1024> buff;
|
||||
iohelper::imemstream_fixed<buff.size()> buff_stream(buff);
|
||||
|
||||
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) {
|
||||
throw std::runtime_error("Failed to receive data!");
|
||||
}
|
||||
|
||||
switch (buffer[0]) {
|
||||
case 'c':
|
||||
switch (iohelper::read<uint8_t>(buff_stream)) {
|
||||
case PACKET::CONNECT:
|
||||
on_connect(sock, from);
|
||||
break;
|
||||
}
|
||||
@@ -63,6 +113,17 @@ void listener(int sock) {
|
||||
int main() {
|
||||
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 type = SOCK_DGRAM;
|
||||
int protocol = 0;
|
||||
|
||||
Reference in New Issue
Block a user