packets: rework encode func

isolation_bkp/dynres
Dominik Madarász 2022-07-31 11:21:27 +02:00
parent df9c45468a
commit 6acd8d202a
7 changed files with 13 additions and 28 deletions

View File

@ -137,4 +137,12 @@ static inline uint32_t pkt_pack_desc_args(pkt_desc *desc) {
return cnt; return cnt;
} }
static inline size_t pkt_table_encode(pkt_desc *desc, void *raw_blob, uint32_t blob_size) {
assert(desc && raw_blob && blob_size > 0);
cw_pack_context pc = {0};
pkt_pack_msg(&pc, pkt_pack_desc_args(desc));
pkt_pack_struct(&pc, desc, raw_blob, blob_size);
return pkt_pack_msg_size(&pc);
}
void pkt_dump_struct(pkt_desc *desc, void* raw_blob, uint32_t blob_size); void pkt_dump_struct(pkt_desc *desc, void* raw_blob, uint32_t blob_size);

View File

@ -16,16 +16,9 @@ pkt_desc pkt_00_init_desc[] = {
{ PKT_END }, { PKT_END },
}; };
size_t pkt_00_init_encode(pkt_00_init *table) {
cw_pack_context pc = {0};
pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_00_init_desc));
pkt_pack_struct(&pc, pkt_00_init_desc, PKT_STRUCT_PTR(table));
return pkt_pack_msg_size(&pc);
}
size_t pkt_00_init_send(uint16_t view_id) { size_t pkt_00_init_send(uint16_t view_id) {
pkt_00_init table = {.view_id = view_id }; pkt_00_init table = {.view_id = view_id };
return pkt_world_write(MSG_ID_00_INIT, pkt_00_init_encode(&table), 1, view_id, NULL, 1); return pkt_world_write(MSG_ID_00_INIT, pkt_table_encode(pkt_00_init_desc, PKT_STRUCT_PTR(&table)), 1, view_id, NULL, 1);
} }
int32_t pkt_00_init_handler(pkt_header *header) { int32_t pkt_00_init_handler(pkt_header *header) {

View File

@ -6,7 +6,6 @@ typedef struct {
uint16_t view_id; uint16_t view_id;
} pkt_00_init; } pkt_00_init;
size_t pkt_00_init_encode(pkt_00_init *table);
size_t pkt_00_init_send(uint16_t view_id); size_t pkt_00_init_send(uint16_t view_id);
extern pkt_desc pkt_00_init_desc[]; extern pkt_desc pkt_00_init_desc[];

View File

@ -13,12 +13,6 @@ pkt_desc pkt_01_welcome_desc[] = {
{ PKT_END }, { PKT_END },
}; };
size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
cw_pack_context pc = {0};
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);
}
size_t pkt_01_welcome_send(uint32_t seed, size_t pkt_01_welcome_send(uint32_t seed,
uint64_t peer_id, uint64_t peer_id,
uint16_t view_id, uint16_t view_id,
@ -26,15 +20,15 @@ size_t pkt_01_welcome_send(uint32_t seed,
uint16_t chunk_size, uint16_t chunk_size,
uint16_t world_size) { uint16_t world_size) {
pkt_01_welcome table = {.seed = seed, .ent_id = ent_id, .chunk_size = chunk_size, .world_size = world_size}; pkt_01_welcome table = {.seed = seed, .ent_id = ent_id, .chunk_size = chunk_size, .world_size = world_size};
return pkt_world_write(MSG_ID_01_WELCOME, pkt_01_welcome_encode(&table), 1, view_id, (void*)peer_id, 0); return pkt_world_write(MSG_ID_01_WELCOME, pkt_table_encode(pkt_01_welcome_desc, PKT_STRUCT_PTR(&table)), 1, view_id, (void*)peer_id, 0);
} }
int32_t pkt_01_welcome_handler(pkt_header *header) { int32_t pkt_01_welcome_handler(pkt_header *header) {
pkt_01_welcome table; pkt_01_welcome table;
PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, pkt_pack_desc_args(pkt_01_welcome_desc), 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)));
world_view *view = game_world_view_get(header->view_id); world_view *view = game_world_view_get(header->view_id);
zpl_printf("[INFO] initializing read-only world view ...\n"); zpl_printf("[INFO] initializing read-only world view ...\n");
world_view_init(view, table.seed, table.ent_id, table.chunk_size, table.world_size); world_view_init(view, table.seed, table.ent_id, table.chunk_size, table.world_size);
game_world_view_set_active(view); game_world_view_set_active(view);

View File

@ -15,7 +15,6 @@ size_t pkt_01_welcome_send(uint32_t seed,
uint64_t ent_id, uint64_t ent_id,
uint16_t chunk_size, uint16_t chunk_size,
uint16_t world_size); uint16_t world_size);
size_t pkt_01_welcome_encode(pkt_01_welcome *table);
extern pkt_desc pkt_01_welcome_desc[]; extern pkt_desc pkt_01_welcome_desc[];
PKT_HANDLER_PROC(pkt_01_welcome_handler); PKT_HANDLER_PROC(pkt_01_welcome_handler);

View File

@ -27,14 +27,7 @@ pkt_desc pkt_send_keystate_desc[] = {
size_t pkt_send_keystate_send(uint16_t view_id, size_t pkt_send_keystate_send(uint16_t view_id,
game_keystate_data *data) { game_keystate_data *data) {
return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_send_keystate_encode(data), 1, view_id, NULL, 1); return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_table_encode(pkt_send_keystate_desc, PKT_STRUCT_PTR(data)), 1, view_id, NULL, 1);
}
size_t pkt_send_keystate_encode(pkt_send_keystate *table) {
cw_pack_context pc = {0};
pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc));
pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table));
return pkt_pack_msg_size(&pc);
} }
int32_t pkt_send_keystate_handler(pkt_header *header) { int32_t pkt_send_keystate_handler(pkt_header *header) {

View File

@ -26,7 +26,6 @@ typedef pkt_send_keystate game_keystate_data;
size_t pkt_send_keystate_send(uint16_t view_id, size_t pkt_send_keystate_send(uint16_t view_id,
game_keystate_data *data); game_keystate_data *data);
size_t pkt_send_keystate_encode(pkt_send_keystate *table);
extern pkt_desc pkt_send_keystate_desc[]; extern pkt_desc pkt_send_keystate_desc[];
PKT_HANDLER_PROC(pkt_send_keystate_handler); PKT_HANDLER_PROC(pkt_send_keystate_handler);