additional packing helpers

isolation_bkp/dynres
Dominik Madarász 2021-05-04 01:57:19 +02:00
parent 93c68ee3c8
commit cf955ef3fb
5 changed files with 27 additions and 13 deletions

View File

@ -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,

View File

@ -99,5 +99,3 @@ int main(int argc, char** argv) {
void platform_shutdown(void) {
is_running = false;
}
#include "packets/pkt_01_welcome.c"

View File

@ -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"

View File

@ -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;

View File

@ -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