From 2094d416c8b236a40afccfab63a85cbbd112dea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 4 May 2021 23:12:32 +0200 Subject: [PATCH] add pkt_pack_desc_args --- code/common/packets/packet_utils.h | 8 +++++++- code/common/packets/pkt_01_welcome.c | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/code/common/packets/packet_utils.h b/code/common/packets/packet_utils.h index 7a92f4d..bdf0374 100644 --- a/code/common/packets/packet_utils.h +++ b/code/common/packets/packet_utils.h @@ -99,4 +99,10 @@ inline int32_t pkt_msg_decode(pkt_header *header, pkt_desc* desc, uint32_t args, PKT_IF(pkt_unpack_struct(&uc, desc, raw_blob, blob_size)); return pkt_validate_eof_msg(&uc); -} \ No newline at end of file +} + +inline size_t pkt_pack_desc_args(pkt_desc *desc) { + size_t cnt = 0; + for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field, ++cnt) {} + return cnt; +} diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index a3e474d..bff09cc 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -1,9 +1,8 @@ #include "pkt_01_welcome.h" +#include "packet_utils.h" #include "world.h" #include "game.h" -#define PKT_01_WELCOME_ARGC 3 - pkt_desc pkt_01_welcome_desc[] = { { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, block_size) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) }, @@ -13,14 +12,14 @@ pkt_desc pkt_01_welcome_desc[] = { size_t pkt_01_welcome_encode(pkt_01_welcome *table) { cw_pack_context pc = {0}; - pkt_pack_msg(&pc, PKT_01_WELCOME_ARGC); + pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_01_welcome_desc)); pkt_pack_struct(&pc, pkt_01_welcome_desc, PKT_STRUCT_PTR(table)); return pkt_pack_msg_size(&pc); } int32_t pkt_01_welcome_handler(pkt_header *header) { pkt_01_welcome table; - PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, PKT_01_WELCOME_ARGC, PKT_STRUCT_PTR(&table))); + PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, pkt_pack_desc_args(pkt_01_welcome_desc), PKT_STRUCT_PTR(&table))); zpl_printf("we received: block_size: %d, chunk_size: %d and world_size: %d\n", table.block_size, table.chunk_size, table.world_size);