simplify unpacking process

isolation_bkp/dynres
Dominik Madarász 2021-05-04 10:08:00 +02:00
parent 5b2db7cd78
commit a4ce84c5f9
5 changed files with 31 additions and 21 deletions

View File

@ -42,13 +42,13 @@ int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen) {
int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, uint32_t blob_size) {
uint8_t *blob = (uint8_t*)raw_blob;
for (pkt_desc *field = desc; field->type != 0; ++field) {
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++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);
zpl_memcopy(blob + field->offset, (uint8_t*)&uc->item.as.u64, field->size);
}break;
default: {
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);

View File

@ -25,14 +25,9 @@ typedef struct {
pkt_handler_proc *handler;
} pkt_handler;
typedef struct {
uint32_t chunk_size;
uint32_t chunk_amount;
} pkt_01_welcome;
int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen);
PKT_HANDLER_PROC(pkt_01_welcome_handler);
extern pkt_handler pkt_handlers[];
extern uint8_t pkt_buffer[];
#include "packet_list.h"

View File

@ -0,0 +1,13 @@
#pragma once
// NOTE(zaklaus): pkt data
typedef struct {
uint32_t chunk_size;
uint32_t chunk_amount;
} pkt_01_welcome;
// NOTE(zaklaus): pkt handlers
PKT_HANDLER_PROC(pkt_01_welcome_handler);

View File

@ -31,6 +31,10 @@ inline int32_t pkt_validate_eof_msg(cw_unpack_context *uc) {
return 0;
}
inline size_t pkt_pack_msg_size(cw_pack_context *pc) {
return pc->current - pc->start; // NOTE(zaklaus): length
}
#ifndef PKT_OFFSETOF
#if defined(_MSC_VER) || defined(ZPL_COMPILER_TINYC)
# define PKT_OFFSETOF(Type, element) ((size_t) & (((Type *)0)->element))
@ -63,3 +67,11 @@ typedef struct pkt_desc {
} pkt_desc;
int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, uint32_t blob_size);
inline int32_t pkt_msg_decode(pkt_header *header, pkt_desc* desc, uint32_t args, void *raw_blob, uint32_t blob_size) {
cw_unpack_context uc = {0};
pkt_unpack_msg(&uc, header, args);
pkt_unpack_struct(&uc, desc, header, raw_blob, blob_size);
return pkt_validate_eof_msg(&uc);
}

View File

@ -1,9 +1,6 @@
#include "packet.h"
#include "cwpack/cwpack.h"
#include "packet_utils.h"
#define PKT_01_WELCOME_ARGS 2
#ifdef SERVER
size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
cw_pack_context pc = {0};
@ -12,7 +9,7 @@ size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
cw_pack_unsigned(&pc, world_chunk_size());
cw_pack_unsigned(&pc, world_chunk_amount());
return pc.current - pc.start; /* length */
return pkt_pack_msg_size(&pc);
}
#else
pkt_desc pkt_01_welcome_desc[3] = {
@ -20,17 +17,10 @@ pkt_desc pkt_01_welcome_desc[3] = {
{ 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);
pkt_unpack_struct(&uc, header, PKT_STRUCT_PTR(table));
return pkt_validate_eof_msg(&uc);
}
int32_t pkt_01_welcome_handler(pkt_header *header) {
pkt_01_welcome table;
PKT_IF(pkt_01_welcome_decode(&table, header));
PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, 2, PKT_STRUCT_PTR(&table)));
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
return 0;