Compare commits
No commits in common. "748b9c76ef105fecbca9b432fcee30e2fbdbcc5a" and "ac463f06692b358b3bfc8adee49c58769ecca096" have entirely different histories.
748b9c76ef
...
ac463f0669
10
.flintlock
10
.flintlock
|
@ -4,13 +4,3 @@ base_url = https://downloads.mtgames.nl/release/flint
|
||||||
|
|
||||||
[flint.py-plugins]
|
[flint.py-plugins]
|
||||||
|
|
||||||
[git-vulkan-headers]
|
|
||||||
url = https://github.com/KhronosGroup/Vulkan-Headers
|
|
||||||
revision = master
|
|
||||||
commit = 42ad3f90faec009b9435383ee89910846d6a91ba
|
|
||||||
|
|
||||||
[git-glfw]
|
|
||||||
url = https://github.com/glfw/glfw
|
|
||||||
revision = 3.3-stable
|
|
||||||
commit = b1309dd42a72c8f7cd58a6f75329c4328679aed2
|
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,6 @@ namespace ecs::lua {
|
||||||
// @todo Allow to construct with given uuid
|
// @todo Allow to construct with given uuid
|
||||||
return thiz->create_entity();
|
return thiz->create_entity();
|
||||||
},
|
},
|
||||||
"remove_entity", &Manager::remove_entity,
|
|
||||||
"has_entity", [] (Manager* thiz, std::string uuid) {
|
"has_entity", [] (Manager* thiz, std::string uuid) {
|
||||||
// @todo Check if valid
|
// @todo Check if valid
|
||||||
return thiz->has_entity(uuids::uuid::from_string(uuid).value());
|
return thiz->has_entity(uuids::uuid::from_string(uuid).value());
|
||||||
|
|
|
@ -198,8 +198,6 @@ namespace ecs {
|
||||||
Entity* create_entity(uuids::uuid uuid);
|
Entity* create_entity(uuids::uuid uuid);
|
||||||
Entity* create_entity();
|
Entity* create_entity();
|
||||||
|
|
||||||
void remove_entity(ecs::Entity* entity);
|
|
||||||
|
|
||||||
bool has_entity(uuids::uuid uuid) {
|
bool has_entity(uuids::uuid uuid) {
|
||||||
// @todo c++20 has .contains()
|
// @todo c++20 has .contains()
|
||||||
return _entities.count(uuid);
|
return _entities.count(uuid);
|
||||||
|
|
|
@ -77,16 +77,6 @@ namespace ecs {
|
||||||
return create_entity(uuid);
|
return create_entity(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Manager::remove_entity(ecs::Entity* entity) {
|
|
||||||
auto it = _entities.find(entity->uuid);
|
|
||||||
if (it == _entities.end()) {
|
|
||||||
throw std::runtime_error("Entity with uuid does not exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
delete it->second;
|
|
||||||
_entities.erase(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
Entity* Manager::create_entity(uuids::uuid uuid) {
|
Entity* Manager::create_entity(uuids::uuid uuid) {
|
||||||
Entity* entity = new Entity(uuid);
|
Entity* entity = new Entity(uuid);
|
||||||
_entities.insert({uuid, entity});
|
_entities.insert({uuid, entity});
|
||||||
|
|
29
flint.lua
29
flint.lua
|
@ -48,32 +48,6 @@ executable "test"
|
||||||
hook(step.PRE_BUILD, codegen, "test/include", "components.h")
|
hook(step.PRE_BUILD, codegen, "test/include", "components.h")
|
||||||
include(config.paths.build .. "/generated/")
|
include(config.paths.build .. "/generated/")
|
||||||
|
|
||||||
git("https://github.com/glfw/glfw", "3.3-stable", "glfw")
|
|
||||||
lib "glfw"
|
|
||||||
include "@glfw@/include"
|
|
||||||
|
|
||||||
src "@glfw@/src/{context,init,input,monitor,window}.c"
|
|
||||||
src "@glfw@/src/egl_context.c"
|
|
||||||
if config.platform.target == "linux" then
|
|
||||||
src "@glfw@/src/glx_context.c"
|
|
||||||
src "@glfw@/src/x11_{init,monitor,window}.c"
|
|
||||||
src "@glfw@/src/posix_{time,thread}.c"
|
|
||||||
src "@glfw@/src/linux_joystick.c"
|
|
||||||
src "@glfw@/src/xkb_unicode.c"
|
|
||||||
src "@glfw@/src/osmesa_context.c"
|
|
||||||
|
|
||||||
define "_GLFW_X11"
|
|
||||||
-- @todo This is needed for strdup to work
|
|
||||||
define "_POSIX_C_SOURCE=200809L"
|
|
||||||
|
|
||||||
link("dl", "X11", "Xrandr", "Xinerama", "Xcursor", "GL", "GLU")
|
|
||||||
elseif config.platform.target == "windows" then
|
|
||||||
end
|
|
||||||
|
|
||||||
lang "c11"
|
|
||||||
|
|
||||||
warnings(false)
|
|
||||||
|
|
||||||
lib "network-shared"
|
lib "network-shared"
|
||||||
path "network-shared"
|
path "network-shared"
|
||||||
|
|
||||||
|
@ -94,9 +68,6 @@ executable "client"
|
||||||
path "network-client"
|
path "network-client"
|
||||||
|
|
||||||
dependency "network-shared"
|
dependency "network-shared"
|
||||||
dependency "glfw"
|
|
||||||
|
|
||||||
threads()
|
|
||||||
|
|
||||||
run_dir "test"
|
run_dir "test"
|
||||||
run_target "test"
|
run_target "test"
|
||||||
|
|
|
@ -12,51 +12,7 @@
|
||||||
#include "packets.h"
|
#include "packets.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
#include <queue>
|
|
||||||
#include <mutex>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
|
|
||||||
// template <typename T>
|
|
||||||
// class ThreadQueue {
|
|
||||||
// public:
|
|
||||||
// void push(const T& value) {
|
|
||||||
// std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
// _queue.push(value);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// void pop() {
|
|
||||||
// std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
// _queue.pop();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// void front() {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private:
|
|
||||||
// std::queue<T> _queue;
|
|
||||||
// std::mutex _mutex;
|
|
||||||
// };
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::queue<std::array<uint8_t, 1024>> thread_queue;
|
|
||||||
|
|
||||||
GLFWwindow* window;
|
|
||||||
|
|
||||||
if (!glfwInit()) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
window = glfwCreateWindow(640, 480, "Network Client", nullptr, nullptr);
|
|
||||||
if (!window) {
|
|
||||||
glfwTerminate();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
|
|
||||||
ecs::Manager manager;
|
ecs::Manager manager;
|
||||||
generated::init();
|
generated::init();
|
||||||
|
|
||||||
|
@ -72,14 +28,6 @@ int main() {
|
||||||
throw std::runtime_error("Failed to create socket!");
|
throw std::runtime_error("Failed to create socket!");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timeval tv;
|
|
||||||
tv.tv_sec = 0;
|
|
||||||
tv.tv_usec = 500000;
|
|
||||||
|
|
||||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
|
|
||||||
throw std::runtime_error("Failed to set timeout");
|
|
||||||
}
|
|
||||||
|
|
||||||
sockaddr_in server_address;
|
sockaddr_in server_address;
|
||||||
server_address.sin_family = AF_INET;
|
server_address.sin_family = AF_INET;
|
||||||
server_address.sin_port = htons(9999);
|
server_address.sin_port = htons(9999);
|
||||||
|
@ -95,8 +43,6 @@ int main() {
|
||||||
|
|
||||||
iohelper::write<uint8_t>(buff_stream_out, PACKET::CONNECT);
|
iohelper::write<uint8_t>(buff_stream_out, PACKET::CONNECT);
|
||||||
|
|
||||||
// @todo Resend connect request until we have an ack (or reach timeout)
|
|
||||||
// @todo Same for requesting initial ids and entities
|
|
||||||
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
|
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");
|
||||||
}
|
}
|
||||||
|
@ -115,111 +61,40 @@ int main() {
|
||||||
throw std::runtime_error("Unexpected server response");
|
throw std::runtime_error("Unexpected server response");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool connected = true;
|
while (true) {
|
||||||
uint8_t timeout = TIMEOUT;
|
buff_stream_in.seekg(0, std::ios::beg);
|
||||||
std::thread receive([&thread_queue, sock, &from, &from_size, &window, &connected, &timeout] {
|
|
||||||
while (!glfwWindowShouldClose(window) && connected) {
|
|
||||||
std::array<uint8_t, 1024> buff;
|
|
||||||
|
|
||||||
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 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) {
|
||||||
timeout--;
|
throw std::runtime_error("Failed to receive data!");
|
||||||
if (timeout == 0) {
|
|
||||||
std::cout << "Connection to server lost!\n";
|
|
||||||
connected = false;
|
|
||||||
} else if (timeout < TIMEOUT/3) {
|
|
||||||
std::cout << "Server connection interrupted\n";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
timeout = TIMEOUT;
|
|
||||||
thread_queue.push(std::move(buff));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window) && connected) {
|
|
||||||
glfwSwapBuffers(window);
|
|
||||||
glfwPollEvents();
|
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
|
||||||
buff_stream_out.seekp(0, std::ios::beg);
|
|
||||||
iohelper::write<uint8_t>(buff_stream_out, PACKET::EVENT);
|
|
||||||
iohelper::write<std::string>(buff_stream_out, "up");
|
|
||||||
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
|
|
||||||
throw std::runtime_error("Failed to connect to server");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
PACKET type = (PACKET)iohelper::read<uint8_t>(buff_stream_in);
|
||||||
buff_stream_out.seekp(0, std::ios::beg);
|
switch (type) {
|
||||||
iohelper::write<uint8_t>(buff_stream_out, PACKET::EVENT);
|
case PACKET::IDS:
|
||||||
iohelper::write<std::string>(buff_stream_out, "down");
|
std::cout << "Receiving ids\n";
|
||||||
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
|
ecs::serial::deserialize_ids(buff_stream_in);
|
||||||
throw std::runtime_error("Failed to connect to server");
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!thread_queue.empty()) {
|
case PACKET::ENTITIES: {
|
||||||
std::array<uint8_t, 1024> buff = thread_queue.front();
|
std::cout << "Receiving entities\n";
|
||||||
iohelper::imemstream_fixed<buff.size()> buff_stream_in(buff);
|
size_t entity_count = iohelper::read_length(buff_stream_in);
|
||||||
buff_stream_in.seekg(0, std::ios::beg);
|
for (size_t i = 0; i < entity_count; ++i) {
|
||||||
|
ecs::serial::deserialize(buff_stream_in, manager);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case PACKET::PING: {
|
manager.view<Position>().for_each([](ecs::Entity* entity, Position* position) {
|
||||||
buff_stream_out.seekp(0, std::ios::beg);
|
std::cout << entity->uuid << ' ' << position->x << ' ' << position->y << '\n';
|
||||||
iohelper::write<uint8_t>(buff_stream_out, PACKET::PING);
|
});
|
||||||
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
|
|
||||||
throw std::runtime_error("Failed to connect to server");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
break;
|
||||||
std::cout << "Unknown packet type: " << (uint32_t)type << '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump_buffer<buff.size()>(buff);
|
default:
|
||||||
std::cout << "PACKET TYPE: " << type << '\n';
|
std::cout << "Unknown packet type: " << (uint32_t)type << '\n';
|
||||||
thread_queue.pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dump_buffer<buff.size()>(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Disconnecting\n";
|
|
||||||
|
|
||||||
buff_stream_out.seekp(0, std::ios::beg);
|
|
||||||
iohelper::write<uint8_t>(buff_stream_out, PACKET::DISCONNECT);
|
|
||||||
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
|
|
||||||
throw std::runtime_error("Failed to connect to server");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "Waiting for thread to join\n";
|
|
||||||
|
|
||||||
shutdown(sock, SHUT_RDWR);
|
|
||||||
receive.join();
|
|
||||||
|
|
||||||
std::cout << "Shutting down\n";
|
|
||||||
|
|
||||||
glfwTerminate();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <unordered_set>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
@ -15,34 +13,9 @@
|
||||||
#include "packets.h"
|
#include "packets.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool operator==(const sockaddr_in& lhs, const sockaddr_in& rhs) {
|
|
||||||
return lhs.sin_addr.s_addr == rhs.sin_addr.s_addr && lhs.sin_port == rhs.sin_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace std {
|
|
||||||
template <>
|
|
||||||
struct hash<sockaddr_in> {
|
|
||||||
size_t operator()(const sockaddr_in& from) const {
|
|
||||||
size_t addr_hash = hash<unsigned long>()(from.sin_addr.s_addr);
|
|
||||||
size_t port_hash = hash<unsigned short>()(from.sin_port);
|
|
||||||
|
|
||||||
addr_hash ^= port_hash + 0x9e3779b9 + (addr_hash << 6) + (addr_hash >> 2);
|
|
||||||
return addr_hash;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Client {
|
|
||||||
uint8_t timeout = TIMEOUT;
|
|
||||||
ecs::Entity* player = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
ecs::Manager manager;
|
ecs::Manager manager;
|
||||||
std::unordered_map<sockaddr_in, Client> clients = {};
|
|
||||||
|
|
||||||
std::string get_ip(sockaddr_in from) {
|
void on_connect(int sock, sockaddr_in from) {
|
||||||
uint32_t ip = from.sin_addr.s_addr;
|
uint32_t ip = from.sin_addr.s_addr;
|
||||||
|
|
||||||
uint32_t bytes[4];
|
uint32_t bytes[4];
|
||||||
|
@ -51,49 +24,7 @@ std::string get_ip(sockaddr_in from) {
|
||||||
bytes[2] = (ip >> 16) & 0xff;
|
bytes[2] = (ip >> 16) & 0xff;
|
||||||
bytes[3] = (ip >> 24) & 0xff;
|
bytes[3] = (ip >> 24) & 0xff;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::cout << "Client connected: " << bytes[0] << '.' << bytes[1] << '.' << bytes[2] << '.' << bytes[3] << '\n';
|
||||||
|
|
||||||
ss << bytes[0] << '.' << bytes[1] << '.' << bytes[2] << '.' << bytes[3];
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_entities(int sock, sockaddr_in from) {
|
|
||||||
std::array<uint8_t, 1024> buff;
|
|
||||||
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_ids(int sock, sockaddr_in from) {
|
|
||||||
std::array<uint8_t, 1024> buff;
|
|
||||||
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_connect(int sock, sockaddr_in from) {
|
|
||||||
std::cout << "Client connected: " << std::dec << get_ip(from) << ':' << from.sin_port << '\n';
|
|
||||||
|
|
||||||
ecs::Entity* player = manager.create_entity();
|
|
||||||
player->add_component<Position>(0, 0);
|
|
||||||
|
|
||||||
Client client = {TIMEOUT, player};
|
|
||||||
clients.insert({from, client});
|
|
||||||
|
|
||||||
std::array<uint8_t, 1024> buff;
|
std::array<uint8_t, 1024> buff;
|
||||||
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
||||||
|
@ -103,36 +34,56 @@ void on_connect(int sock, sockaddr_in from) {
|
||||||
throw std::runtime_error("Failed to send ack to client");
|
throw std::runtime_error("Failed to send ack to client");
|
||||||
}
|
}
|
||||||
|
|
||||||
send_ids(sock, from);
|
// dump_buffer<buff.size()>(buff);
|
||||||
send_entities(sock, from);
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_ping(int sock) {
|
// Send ids and all entities
|
||||||
for (auto i = clients.begin(), last = clients.end(); i != last; ) {
|
// @todo Make everything thread safe
|
||||||
auto& from = i->first;
|
buff_stream.seekp(0, std::ios::beg);
|
||||||
auto& timeout = i->second.timeout;
|
iohelper::write<uint8_t>(buff_stream, PACKET::IDS);
|
||||||
|
ecs::serial::serialize_ids(buff_stream);
|
||||||
|
|
||||||
timeout--;
|
std::cout << "Sending ids\n";
|
||||||
|
|
||||||
if (timeout < TIMEOUT/2) {
|
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
||||||
std::array<uint8_t, 1024> buff;
|
throw std::runtime_error("Failed to send ids to client");
|
||||||
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
|
|
||||||
iohelper::write<uint8_t>(buff_stream, PACKET::PING);
|
|
||||||
|
|
||||||
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
|
|
||||||
throw std::runtime_error("Failed to send ping to client");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timeout == 0) {
|
|
||||||
std::cout << "Client timed-out: " << std::dec << get_ip(from) << ':' << from.sin_port << '\n';
|
|
||||||
|
|
||||||
manager.remove_entity(i->second.player);
|
|
||||||
i = clients.erase(i);
|
|
||||||
} else {
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// This is the loop that listens for messages from clients
|
||||||
|
@ -151,84 +102,28 @@ void listener(int sock) {
|
||||||
throw std::runtime_error("Failed to receive data!");
|
throw std::runtime_error("Failed to receive data!");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it = clients.find(from);
|
|
||||||
|
|
||||||
if (it != clients.end()) {
|
|
||||||
it->second.timeout = TIMEOUT;
|
|
||||||
} else {
|
|
||||||
std::cout << "Unknown client\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (iohelper::read<uint8_t>(buff_stream)) {
|
switch (iohelper::read<uint8_t>(buff_stream)) {
|
||||||
case PACKET::CONNECT:
|
case PACKET::CONNECT:
|
||||||
on_connect(sock, from);
|
on_connect(sock, from);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PACKET::DISCONNECT: {
|
|
||||||
auto it = clients.find(from);
|
|
||||||
if (it == clients.end()) {
|
|
||||||
std::cout << "Unknown client tried to disconnect\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::cout << "Client disconnected: " << std::dec << get_ip(from) << ':' << from.sin_port << '\n';
|
|
||||||
|
|
||||||
manager.remove_entity(it->second.player);
|
|
||||||
it = clients.erase(it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PACKET::IDS:
|
|
||||||
send_ids(sock, from);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PACKET::ENTITIES:
|
|
||||||
send_entities(sock, from);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PACKET::EVENT: {
|
|
||||||
std::string event = iohelper::read<std::string>(buff_stream);
|
|
||||||
auto player = it->second.player;
|
|
||||||
if (!player) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!event.compare("up")) {
|
|
||||||
std::cout << "Moving up\n";
|
|
||||||
player->get_component<Position>()->x += 0.1f;
|
|
||||||
} else if (!event.compare("down")) {
|
|
||||||
std::cout << "Moving down\n";
|
|
||||||
player->get_component<Position>()->x -= 0.1f;
|
|
||||||
}
|
|
||||||
for (auto [from, timeout] : clients) {
|
|
||||||
send_entities(sock, from);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Maybe calculate the ping time here?
|
|
||||||
case PACKET::PING: {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
std::cout << "Unknown packet\n";
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
/// Setup ECS
|
|
||||||
generated::init();
|
generated::init();
|
||||||
|
|
||||||
// for (int i = 0; i < 10; ++i) {
|
for (int i = 0; i < 10; ++i) {
|
||||||
// // We can create entities
|
// We can create entities
|
||||||
// ecs::Entity* entity = manager.create_entity();
|
ecs::Entity* entity = manager.create_entity();
|
||||||
// // Then we can add components to them
|
// Then we can add components to them
|
||||||
// entity->add_component<Position>(0.1f*i, 0.3f*i);
|
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';
|
||||||
|
});
|
||||||
|
|
||||||
/// Setup socket
|
|
||||||
int address_family = AF_INET;
|
int address_family = AF_INET;
|
||||||
int type = SOCK_DGRAM;
|
int type = SOCK_DGRAM;
|
||||||
int protocol = 0;
|
int protocol = 0;
|
||||||
|
@ -248,23 +143,13 @@ int main() {
|
||||||
throw std::runtime_error("Failed to bind to port!");
|
throw std::runtime_error("Failed to bind to port!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start listener
|
std::thread lthread(std::bind(listener, sock));
|
||||||
std::thread listen(std::bind(listener, sock));
|
|
||||||
|
|
||||||
// @todo Add ping thread
|
|
||||||
|
|
||||||
std::cout << "Server started!\n";
|
std::cout << "Server started!\n";
|
||||||
|
|
||||||
/// Run server logic
|
|
||||||
while (true) {
|
while (true) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
send_ping(sock);
|
|
||||||
|
|
||||||
manager.view<Position>().for_each([](ecs::Entity* entity, Position* position) {
|
|
||||||
std::cout << entity->uuid << ' ' << position->x << ' ' << position->y << '\n';
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Waint for threads to join
|
lthread.join();
|
||||||
listen.join();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,9 @@
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#define TIMEOUT 20
|
|
||||||
|
|
||||||
enum PACKET : uint8_t {
|
enum PACKET : uint8_t {
|
||||||
CONNECT,
|
CONNECT,
|
||||||
DISCONNECT,
|
|
||||||
PING,
|
|
||||||
ACK,
|
ACK,
|
||||||
IDS,
|
IDS,
|
||||||
ENTITIES,
|
ENTITIES
|
||||||
EVENT
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user