write data packer + add playground app
parent
a4ce84c5f9
commit
44ade35dd0
|
@ -19,3 +19,4 @@ add_subdirectory(code/vendors)
|
||||||
|
|
||||||
add_subdirectory(code/apps/client)
|
add_subdirectory(code/apps/client)
|
||||||
add_subdirectory(code/apps/server)
|
add_subdirectory(code/apps/server)
|
||||||
|
add_subdirectory(code/apps/playground)
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
add_executable(playground
|
||||||
|
source/main.c
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(header)
|
||||||
|
target_link_libraries(playground eco2d-common cwpack)
|
||||||
|
link_system_libs(playground)
|
|
@ -0,0 +1,9 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
uint64_t world_chunk_size(void) {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t world_chunk_amount(void) {
|
||||||
|
return 8;
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
#define ZPL_IMPL
|
||||||
|
#define ZPL_NANO
|
||||||
|
#include <zpl.h>
|
||||||
|
|
||||||
|
#include "mock.h"
|
||||||
|
#include "packets/packet.h"
|
||||||
|
|
||||||
|
int32_t mock_pkt_decode(pkt_desc *desc, uint32_t args, size_t msg_size, void *data, uint32_t size) {
|
||||||
|
pkt_header header = {
|
||||||
|
.data = pkt_buffer,
|
||||||
|
.datalen = msg_size
|
||||||
|
};
|
||||||
|
return pkt_msg_decode(&header, desc, args, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
pkt_01_welcome test_data = {
|
||||||
|
.chunk_size = world_chunk_size(),
|
||||||
|
.chunk_amount = world_chunk_amount()
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i += 1){
|
||||||
|
test_data.numbers[i] = i*2;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t msg_size = pkt_01_welcome_encode(&test_data);
|
||||||
|
pkt_01_welcome resp = {0};
|
||||||
|
if (mock_pkt_decode(pkt_01_welcome_desc, 3, msg_size, PKT_STRUCT_PTR(&resp))) {
|
||||||
|
zpl_printf("[err] oopsie!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
zpl_printf("size %d, amt %d, num[1] %d struct %d msg %d\n", resp.chunk_size, resp.chunk_amount, resp.numbers[1], sizeof(pkt_01_welcome), msg_size);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ pkt_handler pkt_handlers[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
uint8_t pkt_buffer[PKT_BUFSIZ];
|
uint8_t pkt_buffer[PKT_BUFSIZ];
|
||||||
|
uint8_t pkt_pack_buffer[PKT_BUFSIZ];
|
||||||
|
|
||||||
int32_t pkt_header_encode(pkt_header *table) {
|
int32_t pkt_header_encode(pkt_header *table) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -45,11 +46,15 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob,
|
||||||
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
|
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
|
||||||
cw_unpack_next(uc);
|
cw_unpack_next(uc);
|
||||||
if (uc->item.type != field->type) return -1; // unexpected field
|
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
|
if (blob + field->offset + field->size > blob + blob_size) return -1; // field does not fit
|
||||||
switch (field->type) {
|
switch (field->type) {
|
||||||
case CWP_ITEM_POSITIVE_INTEGER: {
|
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;
|
}break;
|
||||||
|
case CWP_ITEM_BIN: {
|
||||||
|
if (uc->item.as.bin.length != field->size) return -1; // bin size mismatch
|
||||||
|
zpl_memcopy(blob + field->offset, (uint8_t*)uc->item.as.bin.start, uc->item.as.bin.length);
|
||||||
|
}break;
|
||||||
default: {
|
default: {
|
||||||
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
|
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
|
||||||
return -1; // unsupported field
|
return -1; // unsupported field
|
||||||
|
@ -60,4 +65,25 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "packets/pkt_01_welcome.c"
|
int32_t pkt_pack_struct(cw_pack_context *pc, pkt_desc *desc, void *raw_blob, uint32_t blob_size) {
|
||||||
|
uint8_t *blob = (uint8_t*)raw_blob;
|
||||||
|
zpl_zero_item(pkt_pack_buffer);
|
||||||
|
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
|
||||||
|
switch (field->type) {
|
||||||
|
case CWP_ITEM_BIN: {
|
||||||
|
cw_pack_bin(pc, blob + field->offset, field->size);
|
||||||
|
}break;
|
||||||
|
case CWP_ITEM_POSITIVE_INTEGER: {
|
||||||
|
uint64_t num;
|
||||||
|
zpl_memcopy(&num, blob + field->offset, field->size);
|
||||||
|
cw_pack_unsigned(pc, num);
|
||||||
|
}break;
|
||||||
|
default: {
|
||||||
|
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
|
||||||
|
return -1; // unsupported field
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -29,5 +29,6 @@ int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen);
|
||||||
|
|
||||||
extern pkt_handler pkt_handlers[];
|
extern pkt_handler pkt_handlers[];
|
||||||
extern uint8_t pkt_buffer[];
|
extern uint8_t pkt_buffer[];
|
||||||
|
extern uint8_t pkt_pack_buffer[];
|
||||||
|
|
||||||
#include "packet_list.h"
|
#include "packet_list.h"
|
|
@ -1,12 +1,20 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "packet_utils.h"
|
||||||
|
#include "packet.h"
|
||||||
|
|
||||||
// NOTE(zaklaus): pkt data
|
// NOTE(zaklaus): pkt data
|
||||||
|
|
||||||
|
#define PKT_01_NUMBERS_SIZ 32
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t chunk_size;
|
uint32_t chunk_size;
|
||||||
uint32_t chunk_amount;
|
uint32_t chunk_amount;
|
||||||
|
uint16_t numbers[PKT_01_NUMBERS_SIZ];
|
||||||
} pkt_01_welcome;
|
} pkt_01_welcome;
|
||||||
|
|
||||||
|
size_t pkt_01_welcome_encode(pkt_01_welcome *table);
|
||||||
|
extern pkt_desc pkt_01_welcome_desc[];
|
||||||
|
|
||||||
// NOTE(zaklaus): pkt handlers
|
// NOTE(zaklaus): pkt handlers
|
||||||
|
|
||||||
PKT_HANDLER_PROC(pkt_01_welcome_handler);
|
PKT_HANDLER_PROC(pkt_01_welcome_handler);
|
||||||
|
|
|
@ -49,7 +49,11 @@ inline size_t pkt_pack_msg_size(cw_pack_context *pc) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef PKT_FIELD
|
#ifndef PKT_FIELD
|
||||||
#define PKT_FIELD(k, t, a) .type = k, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a)
|
#define PKT_FIELD(k, t, a) .type = k, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef PKT_ARRAY
|
||||||
|
#define PKT_ARRAY(t, a) .type = CWP_ITEM_BIN, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a[0])
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef PKT_END
|
#ifndef PKT_END
|
||||||
|
@ -64,14 +68,15 @@ typedef struct pkt_desc {
|
||||||
cwpack_item_types type;
|
cwpack_item_types type;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
size_t it_size;
|
||||||
} pkt_desc;
|
} pkt_desc;
|
||||||
|
|
||||||
int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, uint32_t blob_size);
|
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) {
|
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};
|
cw_unpack_context uc = {0};
|
||||||
pkt_unpack_msg(&uc, header, args);
|
PKT_IF(pkt_unpack_msg(&uc, header, args));
|
||||||
pkt_unpack_struct(&uc, desc, header, raw_blob, blob_size);
|
PKT_IF(pkt_unpack_struct(&uc, desc, raw_blob, blob_size));
|
||||||
|
|
||||||
return pkt_validate_eof_msg(&uc);
|
return pkt_validate_eof_msg(&uc);
|
||||||
}
|
}
|
|
@ -1,28 +1,24 @@
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "packet_utils.h"
|
#include "packet_utils.h"
|
||||||
|
|
||||||
#ifdef SERVER
|
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_ARGS);
|
|
||||||
|
|
||||||
cw_pack_unsigned(&pc, world_chunk_size());
|
|
||||||
cw_pack_unsigned(&pc, world_chunk_amount());
|
|
||||||
|
|
||||||
return pkt_pack_msg_size(&pc);
|
|
||||||
}
|
|
||||||
#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_size) },
|
||||||
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_amount) },
|
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_amount) },
|
||||||
|
{ PKT_ARRAY(pkt_01_welcome, numbers) },
|
||||||
{ PKT_END },
|
{ PKT_END },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
|
||||||
|
cw_pack_context pc = {0};
|
||||||
|
pkt_pack_msg(&pc, 3);
|
||||||
|
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) {
|
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, 2, PKT_STRUCT_PTR(&table)));
|
PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, 3, PKT_STRUCT_PTR(&table)));
|
||||||
|
|
||||||
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
|
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#pragma once
|
||||||
|
|
Loading…
Reference in New Issue