From 1928f0a531888c16f36a114230f243f924f1b5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 21 Aug 2023 20:07:24 +0200 Subject: [PATCH] reuse IDs on reconnect --- engine/joint/v4k.h | 27 +++++++++++++++++++++++++-- engine/split/v4k_netsync.c | 27 +++++++++++++++++++++++++-- engine/v4k.c | 27 +++++++++++++++++++++++++-- engine/v4k.html | 2 +- 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index 300efc4..cc3259f 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -338587,10 +338587,16 @@ static void enet_init() { } } +struct peer_node_t { + int64_t id; + struct peer_node_t *next; +}; + static ENetHost *Server; static map(ENetPeer *, int64_t) clients; static map(int64_t, ENetPeer *) peers; static int64_t next_client_id = 1; // assumes ID 0 is server +static struct peer_node_t *next_free_id = NULL; enum { MSG_INIT, MSG_BUF, MSG_RPC, MSG_RPC_RESP }; bool server_bind(int max_clients, int port) { @@ -338612,6 +338618,14 @@ void server_drop_client(int64_t handle) { static void server_drop_client_peer(ENetPeer *peer) { + struct peer_node_t *node = C_CAST(struct peer_node_t *, CALLOC(sizeof(struct peer_node_t), 1)); + node->id = *(int64_t *)map_find(clients, peer); + if (!next_free_id) { + next_free_id = node; + } else { + node->next = next_free_id; + next_free_id = node; + } map_erase(peers, *(int64_t *)map_find(clients, peer)); map_erase(clients, peer); } @@ -338894,13 +338908,22 @@ char** server_poll(unsigned timeout_ms) { event.peer->data = STRDUP(ip); /* TEMP */ /* ensure we have free slot for client */ - if (map_count(clients) >= network_get(NETWORK_CAPACITY)) { + if (map_count(clients) >= network_get(NETWORK_CAPACITY)-1) { msg = stringf("%d %s", 1, va("%s", "Server is at maximum capacity, disconnecting the peer (::%s:%u)...", ip, event.peer->address.port)); enet_peer_disconnect_now(event.peer, 1); break; } - int64_t client_id = next_client_id++; + int64_t client_id = -1; + + if (next_free_id) { + struct peer_node_t *node = next_free_id; + client_id = next_free_id->id; + next_free_id = next_free_id->next; + FREE(node); + } + else client_id = next_client_id++; + map_find_or_add(clients, event.peer, client_id); map_find_or_add(peers, client_id, event.peer); network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)+1); diff --git a/engine/split/v4k_netsync.c b/engine/split/v4k_netsync.c index bc3a43b..816b68f 100644 --- a/engine/split/v4k_netsync.c +++ b/engine/split/v4k_netsync.c @@ -125,10 +125,16 @@ static void enet_init() { } } +struct peer_node_t { + int64_t id; + struct peer_node_t *next; +}; + static ENetHost *Server; static map(ENetPeer *, int64_t) clients; static map(int64_t, ENetPeer *) peers; static int64_t next_client_id = 1; // assumes ID 0 is server +static struct peer_node_t *next_free_id = NULL; enum { MSG_INIT, MSG_BUF, MSG_RPC, MSG_RPC_RESP }; bool server_bind(int max_clients, int port) { @@ -150,6 +156,14 @@ void server_drop_client(int64_t handle) { static void server_drop_client_peer(ENetPeer *peer) { + struct peer_node_t *node = C_CAST(struct peer_node_t *, CALLOC(sizeof(struct peer_node_t), 1)); + node->id = *(int64_t *)map_find(clients, peer); + if (!next_free_id) { + next_free_id = node; + } else { + node->next = next_free_id; + next_free_id = node; + } map_erase(peers, *(int64_t *)map_find(clients, peer)); map_erase(clients, peer); } @@ -432,13 +446,22 @@ char** server_poll(unsigned timeout_ms) { event.peer->data = STRDUP(ip); /* TEMP */ /* ensure we have free slot for client */ - if (map_count(clients) >= network_get(NETWORK_CAPACITY)) { + if (map_count(clients) >= network_get(NETWORK_CAPACITY)-1) { msg = stringf("%d %s", 1, va("%s", "Server is at maximum capacity, disconnecting the peer (::%s:%u)...", ip, event.peer->address.port)); enet_peer_disconnect_now(event.peer, 1); break; } - int64_t client_id = next_client_id++; + int64_t client_id = -1; + + if (next_free_id) { + struct peer_node_t *node = next_free_id; + client_id = next_free_id->id; + next_free_id = next_free_id->next; + FREE(node); + } + else client_id = next_client_id++; + map_find_or_add(clients, event.peer, client_id); map_find_or_add(peers, client_id, event.peer); network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)+1); diff --git a/engine/v4k.c b/engine/v4k.c index 3f0842a..303cfcc 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -9602,10 +9602,16 @@ static void enet_init() { } } +struct peer_node_t { + int64_t id; + struct peer_node_t *next; +}; + static ENetHost *Server; static map(ENetPeer *, int64_t) clients; static map(int64_t, ENetPeer *) peers; static int64_t next_client_id = 1; // assumes ID 0 is server +static struct peer_node_t *next_free_id = NULL; enum { MSG_INIT, MSG_BUF, MSG_RPC, MSG_RPC_RESP }; bool server_bind(int max_clients, int port) { @@ -9627,6 +9633,14 @@ void server_drop_client(int64_t handle) { static void server_drop_client_peer(ENetPeer *peer) { + struct peer_node_t *node = C_CAST(struct peer_node_t *, CALLOC(sizeof(struct peer_node_t), 1)); + node->id = *(int64_t *)map_find(clients, peer); + if (!next_free_id) { + next_free_id = node; + } else { + node->next = next_free_id; + next_free_id = node; + } map_erase(peers, *(int64_t *)map_find(clients, peer)); map_erase(clients, peer); } @@ -9909,13 +9923,22 @@ char** server_poll(unsigned timeout_ms) { event.peer->data = STRDUP(ip); /* TEMP */ /* ensure we have free slot for client */ - if (map_count(clients) >= network_get(NETWORK_CAPACITY)) { + if (map_count(clients) >= network_get(NETWORK_CAPACITY)-1) { msg = stringf("%d %s", 1, va("%s", "Server is at maximum capacity, disconnecting the peer (::%s:%u)...", ip, event.peer->address.port)); enet_peer_disconnect_now(event.peer, 1); break; } - int64_t client_id = next_client_id++; + int64_t client_id = -1; + + if (next_free_id) { + struct peer_node_t *node = next_free_id; + client_id = next_free_id->id; + next_free_id = next_free_id->next; + FREE(node); + } + else client_id = next_client_id++; + map_find_or_add(clients, event.peer, client_id); map_find_or_add(peers, client_id, event.peer); network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)+1); diff --git a/engine/v4k.html b/engine/v4k.html index 3d75cda..ff65aed 100644 --- a/engine/v4k.html +++ b/engine/v4k.html @@ -596,7 +596,7 @@ details > summary::-webkit-details-marker { |Version: | 2023.7 | |:--------------|:------------| |Branch: | main | -|Commit: | 57 | +|Commit: | 58 | # [V·4·K 2023.7 ](https://dev.v4.games/zaklaus/v4k)