From 4969ab42d91abf291ac43c2d4873bd970beacbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 21 Aug 2023 14:58:12 +0200 Subject: [PATCH] rpc support --- engine/bind/v4k.lua | 2 ++ engine/joint/v4k.h | 23 +++++++++++++++-------- engine/split/v4k_netsync.c | 19 +++++++++++-------- engine/split/v4k_netsync.h | 4 ++++ engine/v4k.c | 19 +++++++++++-------- engine/v4k.h | 4 ++++ engine/v4k.html | 6 ++++-- 7 files changed, 51 insertions(+), 26 deletions(-) diff --git a/engine/bind/v4k.lua b/engine/bind/v4k.lua index e738d4e..f914d03 100644 --- a/engine/bind/v4k.lua +++ b/engine/bind/v4k.lua @@ -2111,6 +2111,8 @@ NETWORK_EVENT_CONNECT, NETWORK_EVENT_DISCONNECT, NETWORK_EVENT_RECEIVE, NETWORK_EVENT_DISCONNECT_TIMEOUT, +NETWORK_EVENT_RPC = 10, +NETWORK_EVENT_RPC_RESP, }; int network_event(const char *msg, int *errcode, char **errstr); enum { NETWORK_RANK = 0 }; diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index e9452d7..88a5ca2 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -15934,6 +15934,10 @@ enum { NETWORK_EVENT_DISCONNECT, NETWORK_EVENT_RECEIVE, NETWORK_EVENT_DISCONNECT_TIMEOUT, + + /* offset from internal networking events */ + NETWORK_EVENT_RPC = 10, + NETWORK_EVENT_RPC_RESP, }; /* errcode and errstr are optional arguments, pass NULL to ignore them, @@ -338942,7 +338946,7 @@ void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank) { } static -int enet_event_to_netsync(ENetEventType ev) { +int enet_event_to_netsync(int ev) { switch (ev) { case ENET_EVENT_TYPE_CONNECT: return NETWORK_EVENT_CONNECT; case ENET_EVENT_TYPE_DISCONNECT: return NETWORK_EVENT_DISCONNECT; @@ -338950,7 +338954,8 @@ int enet_event_to_netsync(ENetEventType ev) { case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: return NETWORK_EVENT_DISCONNECT_TIMEOUT; } - return -1; + /* passthrough for our own events */ + return ev; } char** network_sync(unsigned timeout_ms) { @@ -339080,19 +339085,21 @@ char** network_sync(unsigned timeout_ms) { } } break; case MSG_RPC: { + event.type = NETWORK_EVENT_RPC; unsigned id = *(uint32_t*)ptr; ptr += 4; char *cmdline = ptr; char *resp = rpc(id, cmdline); - char *resp_msg = MALLOC(strlen(resp) + 5); + char *resp_msg = MALLOC(strlen(resp) + 6); *(uint32_t*)&resp_msg[0] = MSG_RPC_RESP; memcpy(&resp_msg[4], resp, strlen(resp)+1); - ENetPacket *packet = enet_packet_create(resp_msg, strlen(resp) + 4, ENET_PACKET_FLAG_RELIABLE); + ENetPacket *packet = enet_packet_create(resp_msg, strlen(resp) + 5, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, packet); + msg = stringf("%d %s", 0, va("req:%s res:%s", cmdline, resp)); FREE(resp_msg); } break; case MSG_RPC_RESP: { - // @todo: react on response? - msg = ptr; + event.type = NETWORK_EVENT_RPC_RESP; + msg = stringf("%d %s", 0, va("%s", ptr)); } break; default: // PRINTF("!Receiving unk %d sz %d from peer ::%s:%u\n", mid, sz, ip, event.peer->address.port); @@ -339149,7 +339156,7 @@ void network_rpc(const char *signature, void *function) { void network_rpc_send_to(int64_t rank, unsigned id, const char *cmdline) { assert(network_get(NETWORK_RANK) == 0); /* must be a host */ - unsigned sz = strlen(cmdline) + 8; + unsigned sz = strlen(cmdline) + 9; char *msg = MALLOC(sz); *(uint32_t*)&msg[0] = MSG_RPC; *(uint32_t*)&msg[4] = id; @@ -339159,7 +339166,7 @@ void network_rpc_send_to(int64_t rank, unsigned id, const char *cmdline) { } void network_rpc_send(unsigned id, const char *cmdline) { - unsigned sz = strlen(cmdline) + 8; + unsigned sz = strlen(cmdline) + 9; char *msg = MALLOC(sz); *(uint32_t*)&msg[0] = MSG_RPC; *(uint32_t*)&msg[4] = id; diff --git a/engine/split/v4k_netsync.c b/engine/split/v4k_netsync.c index e2713db..aeab660 100644 --- a/engine/split/v4k_netsync.c +++ b/engine/split/v4k_netsync.c @@ -485,7 +485,7 @@ void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank) { } static -int enet_event_to_netsync(ENetEventType ev) { +int enet_event_to_netsync(int ev) { switch (ev) { case ENET_EVENT_TYPE_CONNECT: return NETWORK_EVENT_CONNECT; case ENET_EVENT_TYPE_DISCONNECT: return NETWORK_EVENT_DISCONNECT; @@ -493,7 +493,8 @@ int enet_event_to_netsync(ENetEventType ev) { case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: return NETWORK_EVENT_DISCONNECT_TIMEOUT; } - return -1; + /* passthrough for our own events */ + return ev; } char** network_sync(unsigned timeout_ms) { @@ -623,19 +624,21 @@ char** network_sync(unsigned timeout_ms) { } } break; case MSG_RPC: { + event.type = NETWORK_EVENT_RPC; unsigned id = *(uint32_t*)ptr; ptr += 4; char *cmdline = ptr; char *resp = rpc(id, cmdline); - char *resp_msg = MALLOC(strlen(resp) + 5); + char *resp_msg = MALLOC(strlen(resp) + 6); *(uint32_t*)&resp_msg[0] = MSG_RPC_RESP; memcpy(&resp_msg[4], resp, strlen(resp)+1); - ENetPacket *packet = enet_packet_create(resp_msg, strlen(resp) + 4, ENET_PACKET_FLAG_RELIABLE); + ENetPacket *packet = enet_packet_create(resp_msg, strlen(resp) + 5, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, packet); + msg = stringf("%d %s", 0, va("req:%s res:%s", cmdline, resp)); FREE(resp_msg); } break; case MSG_RPC_RESP: { - // @todo: react on response? - msg = ptr; + event.type = NETWORK_EVENT_RPC_RESP; + msg = stringf("%d %s", 0, va("%s", ptr)); } break; default: // PRINTF("!Receiving unk %d sz %d from peer ::%s:%u\n", mid, sz, ip, event.peer->address.port); @@ -692,7 +695,7 @@ void network_rpc(const char *signature, void *function) { void network_rpc_send_to(int64_t rank, unsigned id, const char *cmdline) { assert(network_get(NETWORK_RANK) == 0); /* must be a host */ - unsigned sz = strlen(cmdline) + 8; + unsigned sz = strlen(cmdline) + 9; char *msg = MALLOC(sz); *(uint32_t*)&msg[0] = MSG_RPC; *(uint32_t*)&msg[4] = id; @@ -702,7 +705,7 @@ void network_rpc_send_to(int64_t rank, unsigned id, const char *cmdline) { } void network_rpc_send(unsigned id, const char *cmdline) { - unsigned sz = strlen(cmdline) + 8; + unsigned sz = strlen(cmdline) + 9; char *msg = MALLOC(sz); *(uint32_t*)&msg[0] = MSG_RPC; *(uint32_t*)&msg[4] = id; diff --git a/engine/split/v4k_netsync.h b/engine/split/v4k_netsync.h index 538eb38..4b311bd 100644 --- a/engine/split/v4k_netsync.h +++ b/engine/split/v4k_netsync.h @@ -31,6 +31,10 @@ enum { NETWORK_EVENT_DISCONNECT, NETWORK_EVENT_RECEIVE, NETWORK_EVENT_DISCONNECT_TIMEOUT, + + /* offset from internal networking events */ + NETWORK_EVENT_RPC = 10, + NETWORK_EVENT_RPC_RESP, }; /* errcode and errstr are optional arguments, pass NULL to ignore them, diff --git a/engine/v4k.c b/engine/v4k.c index 30acfa0..724b7c4 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -9962,7 +9962,7 @@ void* network_buffer(void *ptr, unsigned sz, uint64_t flags, int64_t rank) { } static -int enet_event_to_netsync(ENetEventType ev) { +int enet_event_to_netsync(int ev) { switch (ev) { case ENET_EVENT_TYPE_CONNECT: return NETWORK_EVENT_CONNECT; case ENET_EVENT_TYPE_DISCONNECT: return NETWORK_EVENT_DISCONNECT; @@ -9970,7 +9970,8 @@ int enet_event_to_netsync(ENetEventType ev) { case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: return NETWORK_EVENT_DISCONNECT_TIMEOUT; } - return -1; + /* passthrough for our own events */ + return ev; } char** network_sync(unsigned timeout_ms) { @@ -10100,19 +10101,21 @@ char** network_sync(unsigned timeout_ms) { } } break; case MSG_RPC: { + event.type = NETWORK_EVENT_RPC; unsigned id = *(uint32_t*)ptr; ptr += 4; char *cmdline = ptr; char *resp = rpc(id, cmdline); - char *resp_msg = MALLOC(strlen(resp) + 5); + char *resp_msg = MALLOC(strlen(resp) + 6); *(uint32_t*)&resp_msg[0] = MSG_RPC_RESP; memcpy(&resp_msg[4], resp, strlen(resp)+1); - ENetPacket *packet = enet_packet_create(resp_msg, strlen(resp) + 4, ENET_PACKET_FLAG_RELIABLE); + ENetPacket *packet = enet_packet_create(resp_msg, strlen(resp) + 5, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, packet); + msg = stringf("%d %s", 0, va("req:%s res:%s", cmdline, resp)); FREE(resp_msg); } break; case MSG_RPC_RESP: { - // @todo: react on response? - msg = ptr; + event.type = NETWORK_EVENT_RPC_RESP; + msg = stringf("%d %s", 0, va("%s", ptr)); } break; default: // PRINTF("!Receiving unk %d sz %d from peer ::%s:%u\n", mid, sz, ip, event.peer->address.port); @@ -10169,7 +10172,7 @@ void network_rpc(const char *signature, void *function) { void network_rpc_send_to(int64_t rank, unsigned id, const char *cmdline) { assert(network_get(NETWORK_RANK) == 0); /* must be a host */ - unsigned sz = strlen(cmdline) + 8; + unsigned sz = strlen(cmdline) + 9; char *msg = MALLOC(sz); *(uint32_t*)&msg[0] = MSG_RPC; *(uint32_t*)&msg[4] = id; @@ -10179,7 +10182,7 @@ void network_rpc_send_to(int64_t rank, unsigned id, const char *cmdline) { } void network_rpc_send(unsigned id, const char *cmdline) { - unsigned sz = strlen(cmdline) + 8; + unsigned sz = strlen(cmdline) + 9; char *msg = MALLOC(sz); *(uint32_t*)&msg[0] = MSG_RPC; *(uint32_t*)&msg[4] = id; diff --git a/engine/v4k.h b/engine/v4k.h index b4e0fd5..73ef6d8 100644 --- a/engine/v4k.h +++ b/engine/v4k.h @@ -2017,6 +2017,10 @@ enum { NETWORK_EVENT_DISCONNECT, NETWORK_EVENT_RECEIVE, NETWORK_EVENT_DISCONNECT_TIMEOUT, + + /* offset from internal networking events */ + NETWORK_EVENT_RPC = 10, + NETWORK_EVENT_RPC_RESP, }; /* errcode and errstr are optional arguments, pass NULL to ignore them, diff --git a/engine/v4k.html b/engine/v4k.html index a94a7df..1e651f4 100644 --- a/engine/v4k.html +++ b/engine/v4k.html @@ -596,7 +596,7 @@ details > summary::-webkit-details-marker { |Version: | 2023.7 | |:--------------|:------------| |Branch: | main | -|Commit: | 53 | +|Commit: | 54 | # [V·4·K 2023.7 ](https://dev.v4.games/zaklaus/v4k) @@ -6557,7 +6557,9 @@ enum { NETWORK_EVENT_CONNECT, NETWORK_EVENT_DISCONNECT, NETWORK_EVENT_RECEIVE, - NETWORK_EVENT_DISCONNECT_TIMEOUT + NETWORK_EVENT_DISCONNECT_TIMEOUT, + NETWORK_EVENT_RPC = 10, + NETWORK_EVENT_RPC_RESP }; ~~~~~~