add network event handling

main
Dominik Madarász 2023-08-21 14:24:47 +02:00
parent 4bc37fc6be
commit d5496d4ddb
9 changed files with 214 additions and 34 deletions

View File

@ -62,7 +62,12 @@ int main() {
while( window_swap() && !input(KEY_ESC) ) {
// network sync
char **event = network_sync(0); // timeout_ms:0
while(*event) printf( "network event: %s\n", *event++ );
while(*event) {
int code;
char *msg;
int ev = network_event(*event++, &code, &msg);
printf( "network event id: %d err: %d msg: %s\n", ev, code, msg );
}
self_id = network_get(NETWORK_RANK);
if (network_get(NETWORK_LIVE) == 0) {

View File

@ -2106,10 +2106,18 @@ enum { NETWORK_SEND = 2, NETWORK_RECV = 4 };
enum { NETWORK_UNRELIABLE = 8, NETWORK_UNORDERED = 16 };
void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank);
char** network_sync(unsigned timeout_ms);
enum {
NETWORK_EVENT_CONNECT,
NETWORK_EVENT_DISCONNECT,
NETWORK_EVENT_RECEIVE,
NETWORK_EVENT_DISCONNECT_TIMEOUT,
};
int network_event(const char *msg, int *errcode, char **errstr);
enum { NETWORK_RANK = 0 };
enum { NETWORK_PING = 1 };
enum { NETWORK_PORT = 2, NETWORK_IP, NETWORK_LIVE };
enum { NETWORK_SEND_MS = 4 };
enum { NETWORK_USERID = 5, NETWORK_COUNT };
int64_t network_get(uint64_t key);
int64_t network_put(uint64_t key, int64_t value);
void network_rpc(const char *signature, void *function);

View File

@ -15929,11 +15929,22 @@ enum { NETWORK_UNRELIABLE = 8, NETWORK_UNORDERED = 16/*, NETWORK_PRIORITY = 32*/
API void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank); // configures a shared/networked buffer
API char** network_sync(unsigned timeout_ms); // syncs all buffers & returns null-terminated list of network events
enum {
NETWORK_EVENT_CONNECT,
NETWORK_EVENT_DISCONNECT,
NETWORK_EVENT_RECEIVE,
NETWORK_EVENT_DISCONNECT_TIMEOUT,
};
/* errcode and errstr are optional arguments, pass NULL to ignore them,
errstr is filled by va() */
API int network_event(const char *msg, int *errcode, char **errstr);
enum { NETWORK_RANK = 0 }; // [0..N] where 0 is server
enum { NETWORK_PING = 1 }; // NETWORK_BANDWIDTH, NETWORK_QUALITY };
enum { NETWORK_PORT = 2, NETWORK_IP, NETWORK_LIVE };
enum { NETWORK_SEND_MS = 4 };
//enum { NETWORK_USERID, NETWORK_SALT, NETWORK_COUNT/*N users*/ /*...*/,
enum { NETWORK_USERID = 5, /*NETWORK_SALT,*/ NETWORK_COUNT/*N users*/ /*...*/ };
API int64_t network_get(uint64_t key);
API int64_t network_put(uint64_t key, int64_t value);
@ -338862,6 +338873,7 @@ void network_create(const char *ip, const char *port_, unsigned flags) {
// network_put(NETWORK_IP, 0x7F000001); // 127.0.0.1
network_put(NETWORK_PORT, port);
network_put(NETWORK_LIVE, -1);
network_put(NETWORK_COUNT, 0);
if( !(flags&NETWORK_CONNECT) || flags&NETWORK_BIND ) {
// server, else client
@ -338929,6 +338941,18 @@ void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank) {
return ptr;
}
static
int enet_event_to_netsync(ENetEventType ev) {
switch (ev) {
case ENET_EVENT_TYPE_CONNECT: return NETWORK_EVENT_CONNECT;
case ENET_EVENT_TYPE_DISCONNECT: return NETWORK_EVENT_DISCONNECT;
case ENET_EVENT_TYPE_RECEIVE: return NETWORK_EVENT_RECEIVE;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: return NETWORK_EVENT_DISCONNECT_TIMEOUT;
}
return -1;
}
char** network_sync(unsigned timeout_ms) {
array_clear(events);
@ -338975,13 +338999,13 @@ char** network_sync(unsigned timeout_ms) {
break;
case ENET_EVENT_TYPE_CONNECT:;
msg = stringf( "A new client connected from ::%s:%u", ip, event.peer->address.port );
msg = stringf( "%d %s", 0, va("A new client connected from ::%s:%u", ip, event.peer->address.port ));
/* Store any relevant client information here. */
event.peer->data = "Client information";
event.peer->data = STRDUP(ip); /* TEMP */
/* ensure we have free slot for client */
if (map_count(clients) >= MAX_CLIENTS) {
msg = stringf("%s\n", "Server is at maximum capacity, disconnecting the peer...");
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;
}
@ -338989,6 +339013,7 @@ char** network_sync(unsigned timeout_ms) {
int64_t 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);
break;
case ENET_EVENT_TYPE_RECEIVE:;
@ -339078,29 +339103,46 @@ char** network_sync(unsigned timeout_ms) {
break;
case ENET_EVENT_TYPE_DISCONNECT:
msg = stringf( "%s disconnected", (char *)event.peer->data );
msg = stringf( "%d %s", 0, va("%s disconnected", (char *)event.peer->data));
/* Reset the peer's client information. */
FREE(event.peer->data);
event.peer->data = NULL;
if (is_server) server_drop_client_peer(event.peer);
if (is_server) {
server_drop_client_peer(event.peer);
network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)-1);
}
else {network_put(NETWORK_RANK, -1); network_put(NETWORK_LIVE, 0);}
break;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
msg = stringf( "%s timeout", (char *)event.peer->data );
msg = stringf( "%d %s", 0, va("%s timeout", (char *)event.peer->data));
FREE(event.peer->data);
event.peer->data = NULL;
if (is_server) server_drop_client_peer(event.peer);
if (is_server) {
server_drop_client_peer(event.peer);
network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)-1);
}
else {network_put(NETWORK_RANK, -1); network_put(NETWORK_LIVE, 0);}
break;
}
if(msg) array_push(events, stringf("%d %s", event.type, msg));
// if(msg) server_broadcast(msg);
if(msg) array_push(events, stringf("%d %s", enet_event_to_netsync(event.type), msg));
}
array_push(events, NULL);
return events;
}
int network_event(const char *msg, int *errcode, char **errstr) {
int evid = -1;
int err = 0;
char errbuf[128] = {0};
sscanf(msg, "%d %d %127[^\r\n]", &evid, &err, errbuf);
if (errcode) *errcode = err;
if (errstr) *errstr = va("%s", errbuf);
return evid;
}
void network_rpc(const char *signature, void *function) {
rpc_insert(signature, function);
}

View File

@ -416,6 +416,7 @@ void network_create(const char *ip, const char *port_, unsigned flags) {
// network_put(NETWORK_IP, 0x7F000001); // 127.0.0.1
network_put(NETWORK_PORT, port);
network_put(NETWORK_LIVE, -1);
network_put(NETWORK_COUNT, 0);
if( !(flags&NETWORK_CONNECT) || flags&NETWORK_BIND ) {
// server, else client
@ -483,6 +484,18 @@ void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank) {
return ptr;
}
static
int enet_event_to_netsync(ENetEventType ev) {
switch (ev) {
case ENET_EVENT_TYPE_CONNECT: return NETWORK_EVENT_CONNECT;
case ENET_EVENT_TYPE_DISCONNECT: return NETWORK_EVENT_DISCONNECT;
case ENET_EVENT_TYPE_RECEIVE: return NETWORK_EVENT_RECEIVE;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: return NETWORK_EVENT_DISCONNECT_TIMEOUT;
}
return -1;
}
char** network_sync(unsigned timeout_ms) {
array_clear(events);
@ -529,13 +542,13 @@ char** network_sync(unsigned timeout_ms) {
break;
case ENET_EVENT_TYPE_CONNECT:;
msg = stringf( "A new client connected from ::%s:%u", ip, event.peer->address.port );
msg = stringf( "%d %s", 0, va("A new client connected from ::%s:%u", ip, event.peer->address.port ));
/* Store any relevant client information here. */
event.peer->data = "Client information";
event.peer->data = STRDUP(ip); /* TEMP */
/* ensure we have free slot for client */
if (map_count(clients) >= MAX_CLIENTS) {
msg = stringf("%s\n", "Server is at maximum capacity, disconnecting the peer...");
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;
}
@ -543,6 +556,7 @@ char** network_sync(unsigned timeout_ms) {
int64_t 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);
break;
case ENET_EVENT_TYPE_RECEIVE:;
@ -632,29 +646,46 @@ char** network_sync(unsigned timeout_ms) {
break;
case ENET_EVENT_TYPE_DISCONNECT:
msg = stringf( "%s disconnected", (char *)event.peer->data );
msg = stringf( "%d %s", 0, va("%s disconnected", (char *)event.peer->data));
/* Reset the peer's client information. */
FREE(event.peer->data);
event.peer->data = NULL;
if (is_server) server_drop_client_peer(event.peer);
if (is_server) {
server_drop_client_peer(event.peer);
network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)-1);
}
else {network_put(NETWORK_RANK, -1); network_put(NETWORK_LIVE, 0);}
break;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
msg = stringf( "%s timeout", (char *)event.peer->data );
msg = stringf( "%d %s", 0, va("%s timeout", (char *)event.peer->data));
FREE(event.peer->data);
event.peer->data = NULL;
if (is_server) server_drop_client_peer(event.peer);
if (is_server) {
server_drop_client_peer(event.peer);
network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)-1);
}
else {network_put(NETWORK_RANK, -1); network_put(NETWORK_LIVE, 0);}
break;
}
if(msg) array_push(events, stringf("%d %s", event.type, msg));
// if(msg) server_broadcast(msg);
if(msg) array_push(events, stringf("%d %s", enet_event_to_netsync(event.type), msg));
}
array_push(events, NULL);
return events;
}
int network_event(const char *msg, int *errcode, char **errstr) {
int evid = -1;
int err = 0;
char errbuf[128] = {0};
sscanf(msg, "%d %d %127[^\r\n]", &evid, &err, errbuf);
if (errcode) *errcode = err;
if (errstr) *errstr = va("%s", errbuf);
return evid;
}
void network_rpc(const char *signature, void *function) {
rpc_insert(signature, function);
}

View File

@ -26,11 +26,22 @@ enum { NETWORK_UNRELIABLE = 8, NETWORK_UNORDERED = 16/*, NETWORK_PRIORITY = 32*/
API void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank); // configures a shared/networked buffer
API char** network_sync(unsigned timeout_ms); // syncs all buffers & returns null-terminated list of network events
enum {
NETWORK_EVENT_CONNECT,
NETWORK_EVENT_DISCONNECT,
NETWORK_EVENT_RECEIVE,
NETWORK_EVENT_DISCONNECT_TIMEOUT,
};
/* errcode and errstr are optional arguments, pass NULL to ignore them,
errstr is filled by va() */
API int network_event(const char *msg, int *errcode, char **errstr);
enum { NETWORK_RANK = 0 }; // [0..N] where 0 is server
enum { NETWORK_PING = 1 }; // NETWORK_BANDWIDTH, NETWORK_QUALITY };
enum { NETWORK_PORT = 2, NETWORK_IP, NETWORK_LIVE };
enum { NETWORK_SEND_MS = 4 };
//enum { NETWORK_USERID, NETWORK_SALT, NETWORK_COUNT/*N users*/ /*...*/,
enum { NETWORK_USERID = 5, /*NETWORK_SALT,*/ NETWORK_COUNT/*N users*/ /*...*/ };
API int64_t network_get(uint64_t key);
API int64_t network_put(uint64_t key, int64_t value);

View File

@ -9893,6 +9893,7 @@ void network_create(const char *ip, const char *port_, unsigned flags) {
// network_put(NETWORK_IP, 0x7F000001); // 127.0.0.1
network_put(NETWORK_PORT, port);
network_put(NETWORK_LIVE, -1);
network_put(NETWORK_COUNT, 0);
if( !(flags&NETWORK_CONNECT) || flags&NETWORK_BIND ) {
// server, else client
@ -9960,6 +9961,18 @@ void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank) {
return ptr;
}
static
int enet_event_to_netsync(ENetEventType ev) {
switch (ev) {
case ENET_EVENT_TYPE_CONNECT: return NETWORK_EVENT_CONNECT;
case ENET_EVENT_TYPE_DISCONNECT: return NETWORK_EVENT_DISCONNECT;
case ENET_EVENT_TYPE_RECEIVE: return NETWORK_EVENT_RECEIVE;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: return NETWORK_EVENT_DISCONNECT_TIMEOUT;
}
return -1;
}
char** network_sync(unsigned timeout_ms) {
array_clear(events);
@ -10006,13 +10019,13 @@ char** network_sync(unsigned timeout_ms) {
break;
case ENET_EVENT_TYPE_CONNECT:;
msg = stringf( "A new client connected from ::%s:%u", ip, event.peer->address.port );
msg = stringf( "%d %s", 0, va("A new client connected from ::%s:%u", ip, event.peer->address.port ));
/* Store any relevant client information here. */
event.peer->data = "Client information";
event.peer->data = STRDUP(ip); /* TEMP */
/* ensure we have free slot for client */
if (map_count(clients) >= MAX_CLIENTS) {
msg = stringf("%s\n", "Server is at maximum capacity, disconnecting the peer...");
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;
}
@ -10020,6 +10033,7 @@ char** network_sync(unsigned timeout_ms) {
int64_t 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);
break;
case ENET_EVENT_TYPE_RECEIVE:;
@ -10109,29 +10123,46 @@ char** network_sync(unsigned timeout_ms) {
break;
case ENET_EVENT_TYPE_DISCONNECT:
msg = stringf( "%s disconnected", (char *)event.peer->data );
msg = stringf( "%d %s", 0, va("%s disconnected", (char *)event.peer->data));
/* Reset the peer's client information. */
FREE(event.peer->data);
event.peer->data = NULL;
if (is_server) server_drop_client_peer(event.peer);
if (is_server) {
server_drop_client_peer(event.peer);
network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)-1);
}
else {network_put(NETWORK_RANK, -1); network_put(NETWORK_LIVE, 0);}
break;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
msg = stringf( "%s timeout", (char *)event.peer->data );
msg = stringf( "%d %s", 0, va("%s timeout", (char *)event.peer->data));
FREE(event.peer->data);
event.peer->data = NULL;
if (is_server) server_drop_client_peer(event.peer);
if (is_server) {
server_drop_client_peer(event.peer);
network_put(NETWORK_COUNT, network_get(NETWORK_COUNT)-1);
}
else {network_put(NETWORK_RANK, -1); network_put(NETWORK_LIVE, 0);}
break;
}
if(msg) array_push(events, stringf("%d %s", event.type, msg));
// if(msg) server_broadcast(msg);
if(msg) array_push(events, stringf("%d %s", enet_event_to_netsync(event.type), msg));
}
array_push(events, NULL);
return events;
}
int network_event(const char *msg, int *errcode, char **errstr) {
int evid = -1;
int err = 0;
char errbuf[128] = {0};
sscanf(msg, "%d %d %127[^\r\n]", &evid, &err, errbuf);
if (errcode) *errcode = err;
if (errstr) *errstr = va("%s", errbuf);
return evid;
}
void network_rpc(const char *signature, void *function) {
rpc_insert(signature, function);
}

View File

@ -2012,11 +2012,22 @@ enum { NETWORK_UNRELIABLE = 8, NETWORK_UNORDERED = 16/*, NETWORK_PRIORITY = 32*/
API void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank); // configures a shared/networked buffer
API char** network_sync(unsigned timeout_ms); // syncs all buffers & returns null-terminated list of network events
enum {
NETWORK_EVENT_CONNECT,
NETWORK_EVENT_DISCONNECT,
NETWORK_EVENT_RECEIVE,
NETWORK_EVENT_DISCONNECT_TIMEOUT,
};
/* errcode and errstr are optional arguments, pass NULL to ignore them,
errstr is filled by va() */
API int network_event(const char *msg, int *errcode, char **errstr);
enum { NETWORK_RANK = 0 }; // [0..N] where 0 is server
enum { NETWORK_PING = 1 }; // NETWORK_BANDWIDTH, NETWORK_QUALITY };
enum { NETWORK_PORT = 2, NETWORK_IP, NETWORK_LIVE };
enum { NETWORK_SEND_MS = 4 };
//enum { NETWORK_USERID, NETWORK_SALT, NETWORK_COUNT/*N users*/ /*...*/,
enum { NETWORK_USERID = 5, /*NETWORK_SALT,*/ NETWORK_COUNT/*N users*/ /*...*/ };
API int64_t network_get(uint64_t key);
API int64_t network_put(uint64_t key, int64_t value);

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
; this is where you specify and configure the V4K pipeline.
; this is where you specify and configure the FWK pipeline.
; tweak the pipeline and add new importers just by editing this file.
; there is no flow control in this script file: lines are parsed and evaluated, from top to bottom.