code: server signal handling + refactors
parent
1705d97620
commit
138bd6e7fb
|
@ -23,12 +23,13 @@ add_library(client-common STATIC
|
|||
source/network.c
|
||||
source/game.c
|
||||
source/main.c
|
||||
source/signal_handling.c
|
||||
|
||||
header/signal_handling.h
|
||||
header/network.h
|
||||
header/platform.h
|
||||
header/game.h
|
||||
|
||||
../../common/signal_handling.c
|
||||
../../common/signal_handling.h
|
||||
)
|
||||
|
||||
add_executable(eco2d-client
|
||||
|
|
|
@ -7,6 +7,7 @@ add_executable(eco2d-server
|
|||
source/world/blocks.c
|
||||
|
||||
header/network.h
|
||||
header/platform.h
|
||||
header/utils/options.h
|
||||
header/world/perlin.h
|
||||
header/world/world.h
|
||||
|
@ -22,6 +23,9 @@ add_executable(eco2d-server
|
|||
../../vendors/flecs/flecs.h
|
||||
../../vendors/flecs/flecs_meta.c
|
||||
../../vendors/flecs/flecs_meta.h
|
||||
|
||||
../../common/signal_handling.c
|
||||
../../common/signal_handling.h
|
||||
)
|
||||
|
||||
include_directories(eco2d-server header)
|
||||
|
|
|
@ -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_tick(void);
|
||||
void network_server_update(void *data);
|
||||
|
||||
uint64_t network_player_create(uint16_t peer_id);
|
||||
void network_player_destroy(uint64_t ent_id);
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void platform_shutdown();
|
|
@ -5,6 +5,7 @@
|
|||
#include "network.h"
|
||||
#include "world/world.h"
|
||||
#include "utils/options.h"
|
||||
#include "signal_handling.h"
|
||||
|
||||
#define DEFAULT_WORLD_SEED 302097
|
||||
#define DEFAULT_BLOCK_SIZE 64 /* amount of units within a block (single axis) */
|
||||
|
@ -18,6 +19,8 @@
|
|||
} \
|
||||
} while (0)
|
||||
|
||||
zpl_global zpl_b32 is_running = true;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
zpl_opts opts={0};
|
||||
zpl_opts_init(&opts, zpl_heap(), argv[0]);
|
||||
|
@ -55,6 +58,8 @@ int main(int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
sighandler_register();
|
||||
|
||||
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));
|
||||
|
||||
|
@ -62,16 +67,22 @@ int main(int argc, char** argv) {
|
|||
IF(network_init());
|
||||
IF(network_server_start("0.0.0.0", 27000));
|
||||
|
||||
while (true) {
|
||||
while (is_running) {
|
||||
network_server_tick();
|
||||
world_update();
|
||||
}
|
||||
|
||||
zpl_printf("Bye!\n");
|
||||
IF(network_server_stop());
|
||||
IF(network_destroy());
|
||||
IF(world_destroy());
|
||||
sighandler_unregister();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void platform_shutdown(void) {
|
||||
is_running = false;
|
||||
}
|
||||
|
||||
#include "packets/pkt_01_welcome.c"
|
||||
|
|
|
@ -27,7 +27,6 @@ int32_t network_init(void) {
|
|||
|
||||
int32_t network_destroy(void) {
|
||||
enet_deinitialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -54,6 +53,7 @@ int32_t network_server_start(const char *host, uint16_t port) {
|
|||
}
|
||||
|
||||
int32_t network_server_stop(void) {
|
||||
zpl_printf("[INFO] Shutting down the ENet server...\n");
|
||||
zpl_timer_stop(&nettimer);
|
||||
enet_host_destroy(server);
|
||||
server = NULL;
|
||||
|
@ -61,34 +61,20 @@ int32_t network_server_stop(void) {
|
|||
}
|
||||
|
||||
int32_t network_server_tick(void) {
|
||||
ECS_IMPORT(world_ecs(), Common);
|
||||
ECS_IMPORT(world_ecs(), Net);
|
||||
|
||||
ENetEvent event = {0};
|
||||
while (enet_host_service(server, &event, 1) > 0) {
|
||||
switch (event.type) {
|
||||
case ENET_EVENT_TYPE_CONNECT: {
|
||||
zpl_printf("[INFO] A new user %d connected.\n", event.peer->incomingPeerID);
|
||||
uint16_t peer_id = event.peer->incomingPeerID;
|
||||
|
||||
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});
|
||||
|
||||
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 */
|
||||
uint64_t ent_id = network_player_create(peer_id);
|
||||
event.peer->data = (void*)((uint32_t)ent_id);
|
||||
} break;
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: {
|
||||
zpl_printf("[INFO] A user %d disconnected.\n", event.peer->incomingPeerID);
|
||||
ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data);
|
||||
librg_entity_untrack(world_tracker(), e);
|
||||
ecs_delete(world_ecs(), e);
|
||||
network_player_destroy(e);
|
||||
} break;
|
||||
|
||||
case ENET_EVENT_TYPE_RECEIVE: {
|
||||
|
@ -139,3 +125,24 @@ void network_server_update(void *data) {
|
|||
// 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);
|
||||
}
|
||||
|
|
|
@ -108,6 +108,7 @@ int32_t world_destroy(void) {
|
|||
ecs_fini(world.ecs);
|
||||
zpl_mfree(world.data);
|
||||
zpl_memset(&world, 0, sizeof(world));
|
||||
zpl_printf("[INFO] World was destroyed.\n");
|
||||
return WORLD_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue