code: server signal handling + refactors

isolation_bkp/dynres
Dominik Madarász 2021-01-18 00:09:29 +01:00
parent 1705d97620
commit 138bd6e7fb
9 changed files with 51 additions and 21 deletions

View File

@ -23,12 +23,13 @@ add_library(client-common STATIC
source/network.c source/network.c
source/game.c source/game.c
source/main.c source/main.c
source/signal_handling.c
header/signal_handling.h
header/network.h header/network.h
header/platform.h header/platform.h
header/game.h header/game.h
../../common/signal_handling.c
../../common/signal_handling.h
) )
add_executable(eco2d-client add_executable(eco2d-client

View File

@ -7,6 +7,7 @@ add_executable(eco2d-server
source/world/blocks.c source/world/blocks.c
header/network.h header/network.h
header/platform.h
header/utils/options.h header/utils/options.h
header/world/perlin.h header/world/perlin.h
header/world/world.h header/world/world.h
@ -22,6 +23,9 @@ add_executable(eco2d-server
../../vendors/flecs/flecs.h ../../vendors/flecs/flecs.h
../../vendors/flecs/flecs_meta.c ../../vendors/flecs/flecs_meta.c
../../vendors/flecs/flecs_meta.h ../../vendors/flecs/flecs_meta.h
../../common/signal_handling.c
../../common/signal_handling.h
) )
include_directories(eco2d-server header) include_directories(eco2d-server header)

View File

@ -7,3 +7,6 @@ int32_t network_server_start(const char *host, uint16_t port);
int32_t network_server_stop(void); int32_t network_server_stop(void);
int32_t network_server_tick(void); int32_t network_server_tick(void);
void network_server_update(void *data); void network_server_update(void *data);
uint64_t network_player_create(uint16_t peer_id);
void network_player_destroy(uint64_t ent_id);

View File

@ -0,0 +1,3 @@
#pragma once
void platform_shutdown();

View File

@ -5,6 +5,7 @@
#include "network.h" #include "network.h"
#include "world/world.h" #include "world/world.h"
#include "utils/options.h" #include "utils/options.h"
#include "signal_handling.h"
#define DEFAULT_WORLD_SEED 302097 #define DEFAULT_WORLD_SEED 302097
#define DEFAULT_BLOCK_SIZE 64 /* amount of units within a block (single axis) */ #define DEFAULT_BLOCK_SIZE 64 /* amount of units within a block (single axis) */
@ -18,6 +19,8 @@
} \ } \
} while (0) } while (0)
zpl_global zpl_b32 is_running = true;
int main(int argc, char** argv) { int main(int argc, char** argv) {
zpl_opts opts={0}; zpl_opts opts={0};
zpl_opts_init(&opts, zpl_heap(), argv[0]); zpl_opts_init(&opts, zpl_heap(), argv[0]);
@ -55,6 +58,8 @@ int main(int argc, char** argv) {
return 0; return 0;
} }
sighandler_register();
zpl_printf("[INFO] Generating world of size: %d x %d\n", world_size, world_size); zpl_printf("[INFO] Generating world of size: %d x %d\n", world_size, world_size);
IF(world_init(seed, block_size, chunk_size, world_size)); IF(world_init(seed, block_size, chunk_size, world_size));
@ -62,16 +67,22 @@ int main(int argc, char** argv) {
IF(network_init()); IF(network_init());
IF(network_server_start("0.0.0.0", 27000)); IF(network_server_start("0.0.0.0", 27000));
while (true) { while (is_running) {
network_server_tick(); network_server_tick();
world_update(); world_update();
} }
zpl_printf("Bye!\n");
IF(network_server_stop()); IF(network_server_stop());
IF(network_destroy()); IF(network_destroy());
IF(world_destroy()); IF(world_destroy());
sighandler_unregister();
return 0; return 0;
} }
void platform_shutdown(void) {
is_running = false;
}
#include "packets/pkt_01_welcome.c" #include "packets/pkt_01_welcome.c"

View File

@ -27,7 +27,6 @@ int32_t network_init(void) {
int32_t network_destroy(void) { int32_t network_destroy(void) {
enet_deinitialize(); enet_deinitialize();
return 0; return 0;
} }
@ -54,6 +53,7 @@ int32_t network_server_start(const char *host, uint16_t port) {
} }
int32_t network_server_stop(void) { int32_t network_server_stop(void) {
zpl_printf("[INFO] Shutting down the ENet server...\n");
zpl_timer_stop(&nettimer); zpl_timer_stop(&nettimer);
enet_host_destroy(server); enet_host_destroy(server);
server = NULL; server = NULL;
@ -61,34 +61,20 @@ int32_t network_server_stop(void) {
} }
int32_t network_server_tick(void) { int32_t network_server_tick(void) {
ECS_IMPORT(world_ecs(), Common);
ECS_IMPORT(world_ecs(), Net);
ENetEvent event = {0}; ENetEvent event = {0};
while (enet_host_service(server, &event, 1) > 0) { while (enet_host_service(server, &event, 1) > 0) {
switch (event.type) { switch (event.type) {
case ENET_EVENT_TYPE_CONNECT: { case ENET_EVENT_TYPE_CONNECT: {
zpl_printf("[INFO] A new user %d connected.\n", event.peer->incomingPeerID); zpl_printf("[INFO] A new user %d connected.\n", event.peer->incomingPeerID);
uint16_t peer_id = event.peer->incomingPeerID; uint16_t peer_id = event.peer->incomingPeerID;
uint64_t ent_id = network_player_create(peer_id);
ecs_entity_t e = ecs_new(world_ecs(), 0); event.peer->data = (void*)((uint32_t)ent_id);
ecs_set(world_ecs(), e, Position, {0, 0});
ecs_set(world_ecs(), e, NetClient, {peer_id});
event.peer->data = (void*)((uint32_t)e);
librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, peer_id);
librg_entity_chunk_set(world_tracker(), e, 1);
librg_entity_radius_set(world_tracker(), e, 2); /* 2 chunk radius visibility */
librg_entity_userdata_set(world_tracker(), e, event.peer); /* save ptr to peer */
} break; } break;
case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT:
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: { case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: {
zpl_printf("[INFO] A user %d disconnected.\n", event.peer->incomingPeerID); zpl_printf("[INFO] A user %d disconnected.\n", event.peer->incomingPeerID);
ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data); ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data);
librg_entity_untrack(world_tracker(), e); network_player_destroy(e);
ecs_delete(world_ecs(), e);
} break; } break;
case ENET_EVENT_TYPE_RECEIVE: { case ENET_EVENT_TYPE_RECEIVE: {
@ -139,3 +125,24 @@ void network_server_update(void *data) {
// enet_peer_send(currentPeer, 0, packet); // enet_peer_send(currentPeer, 0, packet);
// } // }
} }
uint64_t network_player_create(uint16_t peer_id) {
ECS_IMPORT(world_ecs(), Common);
ECS_IMPORT(world_ecs(), Net);
ecs_entity_t e = ecs_new(world_ecs(), 0);
ecs_set(world_ecs(), e, Position, {0, 0});
ecs_set(world_ecs(), e, NetClient, {peer_id});
librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, peer_id);
librg_entity_chunk_set(world_tracker(), e, 1);
librg_entity_radius_set(world_tracker(), e, 2); /* 2 chunk radius visibility */
return (uint64_t)e;
}
void network_player_destroy(uint64_t ent_id) {
librg_entity_untrack(world_tracker(), ent_id);
ecs_delete(world_ecs(), ent_id);
}

View File

@ -108,6 +108,7 @@ int32_t world_destroy(void) {
ecs_fini(world.ecs); ecs_fini(world.ecs);
zpl_mfree(world.data); zpl_mfree(world.data);
zpl_memset(&world, 0, sizeof(world)); zpl_memset(&world, 0, sizeof(world));
zpl_printf("[INFO] World was destroyed.\n");
return WORLD_ERROR_NONE; return WORLD_ERROR_NONE;
} }