From cf955ef3fbf4e01703ea7498aca6337124c4a4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 4 May 2021 01:57:19 +0200 Subject: [PATCH] additional packing helpers --- code/apps/client/source/network.c | 10 ++++++++++ code/apps/server/source/main.c | 2 -- code/common/packets/packet.c | 3 +++ code/common/packets/packet_utils.h | 6 +++++- code/common/packets/pkt_01_welcome.c | 19 +++++++++---------- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/code/apps/client/source/network.c b/code/apps/client/source/network.c index 59058f5..fbebdfd 100644 --- a/code/apps/client/source/network.c +++ b/code/apps/client/source/network.c @@ -8,6 +8,7 @@ #include "librg.h" #include "network.h" +#include "packets/packet.h" #define NETWORK_UPDATE_DELAY 0.100 @@ -108,6 +109,15 @@ int32_t network_client_tick() { } break; case ENET_EVENT_TYPE_RECEIVE: { + pkt_header header = {0}; + uint32_t ok = pkt_header_decode(&header, event.packet->data, event.packet->dataLength); + + if (ok && header.ok) { + pkt_handlers[header.id].handler(&header); + } else { + zpl_printf("[INFO] Server sent us an unsupported packet.\n"); + } + /* handle a newly received event */ // librg_world_read( // world, diff --git a/code/apps/server/source/main.c b/code/apps/server/source/main.c index 4c96890..eea0433 100644 --- a/code/apps/server/source/main.c +++ b/code/apps/server/source/main.c @@ -99,5 +99,3 @@ int main(int argc, char** argv) { void platform_shutdown(void) { is_running = false; } - -#include "packets/pkt_01_welcome.c" diff --git a/code/common/packets/packet.c b/code/common/packets/packet.c index e2f20ad..149bc6c 100644 --- a/code/common/packets/packet.c +++ b/code/common/packets/packet.c @@ -42,6 +42,7 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, for (pkt_desc *field = desc; field->type != 0; ++field) { cw_unpack_next(uc); if (uc->item.type != field->type) return -1; // unexpected field + if (blob + field->offset + field->size >= blob + blob_size) return -1; // field does not fit switch (field->type) { case CWP_ITEM_POSITIVE_INTEGER: { zpl_memcopy(blob + field->offset, (uint8_t*)uc->item.as.u64, field->size); @@ -55,3 +56,5 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, return 0; } + +#include "packets/pkt_01_welcome.c" \ No newline at end of file diff --git a/code/common/packets/packet_utils.h b/code/common/packets/packet_utils.h index e0e27d3..01cc871 100644 --- a/code/common/packets/packet_utils.h +++ b/code/common/packets/packet_utils.h @@ -41,7 +41,7 @@ inline int32_t pkt_validate_eof_msg(cw_unpack_context *uc) { #endif #ifndef PKT_STRUCT_PTR -#define PKT_STRUCT_PTR(a) (void*)a, (uint32_t)sizeof(*a) +#define PKT_STRUCT_PTR(a) (void*)(a), (uint32_t)sizeof(*(a)) #endif #ifndef PKT_FIELD @@ -52,6 +52,10 @@ inline int32_t pkt_validate_eof_msg(cw_unpack_context *uc) { #define PKT_END .type = CWP_NOT_AN_ITEM #endif +#ifndef PKT_IF +#define PKT_IF(c) if (c < 0) return -1; +#endif + typedef struct pkt_desc { cwpack_item_types type; size_t offset; diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index f1cf755..17275e7 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -4,12 +4,7 @@ #define PKT_01_WELCOME_ARGS 2 -pkt_desc pkt_01_welcome_desc[3] = { - { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) }, - { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_amount) }, - { PKT_END }, -}; - +#ifdef SERVER size_t pkt_01_welcome_encode(pkt_01_welcome *table) { cw_pack_context pc = {0}; pkt_pack_msg(&pc, PKT_01_WELCOME_ARGS); @@ -19,7 +14,12 @@ size_t pkt_01_welcome_encode(pkt_01_welcome *table) { return pc.current - pc.start; /* length */ } - +#else +pkt_desc pkt_01_welcome_desc[3] = { + { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) }, + { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_amount) }, + { PKT_END }, +}; int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) { cw_unpack_context uc = {0}; pkt_unpack_msg(&uc, header, PKT_01_WELCOME_ARGS); @@ -29,11 +29,10 @@ int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) { } int32_t pkt_01_welcome_handler(pkt_header *header) { -#if 1 pkt_01_welcome table; - pkt_01_welcome_decode(&table, header); + PKT_IF(pkt_01_welcome_decode(&table, header)); zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount); -#endif return 0; } +#endif \ No newline at end of file