small progress on packing

isolation_bkp/dynres
Dominik Madarász 2021-05-04 00:44:39 +02:00
parent 738b612798
commit b135b62a2b
9 changed files with 17269 additions and 16377 deletions

View File

@ -17,3 +17,6 @@ int32_t world_update(void);
uint32_t world_buf(uint8_t const **ptr, uint32_t *width);
ecs_world_t * world_ecs(void);
librg_world * world_tracker(void);
uint16_t world_chunk_size(void);
uint16_t world_chunk_amount(void);

View File

@ -84,12 +84,14 @@ int32_t network_server_tick(void) {
case ENET_EVENT_TYPE_RECEIVE: {
pkt_header header = {0};
pkt_header_decode(&header, event.packet->data, event.packet->dataLength);
uint32_t ok = pkt_header_decode(&header, event.packet->data, event.packet->dataLength);
if (header.ok) {
if (ok && header.ok) {
pkt_handlers[header.id].handler(&header);
} else {
// error happened within top level packet flow
zpl_printf("[INFO] User %d sent us a malformed packet.\n", event.peer->incomingPeerID);
ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data);
network_client_destroy(e);
}

View File

@ -135,4 +135,12 @@ librg_world * world_tracker() {
return world.tracker;
}
uint16_t world_chunk_size(void) {
return world.chunk_size;
}
uint16_t world_chunk_amount(void) {
return world.world_size;
}
#include "world_gen.c"

View File

@ -100,7 +100,7 @@ static void world_fill_mountain(uint32_t x, uint32_t y) {
}
#define RAND_RANGE(x,y) (x + (uint32_t)rand()%(y-(x)))
#define RAND_RANGE(x,y) (x + (int)rand()%(y-(x)))
int32_t world_gen() {
// TODO: perform world gen

View File

@ -1,28 +1,26 @@
#include "packet.h"
#include "packet_utils.h"
#include "cwpack/cwpack.h"
#define PKT_HEADER_ELEMENTS 2
pkt_handler pkt_handlers[] = {
{.id = MSG_ID_01_WELCOME, .handler = /*pkt_01_welcome_handler*/ NULL},
{.id = MSG_ID_01_WELCOME, .handler = pkt_01_welcome_handler},
};
uint8_t pkt_buffer[PKT_BUFSIZ];
int32_t pkt_header_encode(pkt_header *table) {
return 0;
}
int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen) {
cw_unpack_context uc = {0};
cw_unpack_context_init(&uc, data, datalen, 0);
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_ARRAY || uc.item.as.array.size != PKT_HEADER_ELEMENTS) {
return -1;//todo: error
}
pkt_unpack_msg_raw(&uc, data, datalen, PKT_HEADER_ELEMENTS);
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER || uc.item.as.u64 > UINT16_MAX) {
return -1; // invalid packet id id
return -1; // invalid packet id
}
uint16_t pkt_id = (uint16_t)uc.item.as.u64;
@ -36,9 +34,5 @@ int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen) {
table->datalen = packed_size;
table->ok = 1;
if (uc.return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere
cw_unpack_next(&uc);
if (uc.return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be?
return 0;
return pkt_validate_eof_msg(&uc);
}

View File

@ -1,6 +1,8 @@
#pragma once
#include "system.h"
#define PKT_BUFSIZ 4096
typedef enum {
MSG_ID_01_WELCOME,
MSG_ID_LIBRG_UPDATE,
@ -33,3 +35,4 @@ 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[];

View File

@ -0,0 +1,31 @@
#pragma once
#include "packet.h"
#include "cwpack/cwpack.h"
inline void pkt_pack_msg(cw_pack_context *pc, uint32_t args) {
cw_pack_context_init(pc, pkt_buffer, PKT_BUFSIZ, 0);
cw_pack_array_size(pc, args);
}
inline int32_t pkt_unpack_msg(cw_unpack_context *uc, pkt_header *header, uint32_t args) {
cw_unpack_context_init(uc, header->data, header->datalen, 0);
cw_unpack_next(uc);
if (uc->item.type != CWP_ITEM_ARRAY || uc->item.as.array.size != args) {
return -1;//todo: error
}
return 0;
}
inline int32_t pkt_unpack_msg_raw(cw_unpack_context *uc, uint8_t *data, uint32_t datalen, uint32_t args) {
pkt_header header = {.data = data, .datalen = datalen};
return pkt_unpack_msg(uc, &header, args);
}
inline int32_t pkt_validate_eof_msg(cw_unpack_context *uc) {
if (uc->return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere
cw_unpack_next(uc);
if (uc->return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be?
return 0;
}

View File

@ -1,57 +1,42 @@
// #include "packet.h"
#include "packet.h"
#include "cwpack/cwpack.h"
#include "packet_utils.h"
// PACKET_GENERATE_ENCODE(1, 2, )
//
//#include "packet.h"
//#include "cwpack/cwpack.h"
//
//#define PKT_01_WELCOME_ID 1
//#define PKT_01_WELCOME_ARGS 2
//
//
//size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
//cw_pack_context pc = {0};
//cw_pack_context_init(&pc, buffer, 20, 0);
//cw_pack_array_size(&pc, 1 + PKT_01_WELCOME_ARGS);
//cw_pack_signed(&pc, PKT_01_WELCOME_ID);
//
//cw_pack_unsigned(&pc, chunk_size);
//cw_pack_unsigned(&pc, chunk_amount);
//
//return pc.current - pc.start; /* length */
//}
//
//int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) {
//cw_unpack_context uc = {0};
//cw_unpack_context_init(&uc, header->data, header->datalen, 0);
//
//cw_unpack_next(&uc);
//if (uc.item.type != CWP_ITEM_ARRAY || uc.item.as.array.size != PKT_01_WELCOME_ARGS) {
//return -1;//todo: error
//}
//
//cw_unpack_next(&uc);
//if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size
//table->chunk_size = uc.item.as.u64;
//
//cw_unpack_next(&uc);
//if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount
//table->chunk_amount = uc.item.as.u64;
//
//if (uc.return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere
//cw_unpack_next(&uc);
//if (uc.return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be?
//
//return 0;
//}
//
//int32_t pkt_01_handler(pkt_header *header) {
//pkt_01_welcome table;
//pkt_01_welcome_decode(&table, header);
//
//zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
//
// do STUFF
//return 0;
//}
//
#define PKT_01_WELCOME_ARGS 2
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 pc.current - pc.start; /* length */
}
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);
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size
table->chunk_size = (uint16_t)uc.item.as.u64;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount
table->chunk_amount = (uint16_t)uc.item.as.u64;
return pkt_validate_eof_msg(&uc);;
}
int32_t pkt_01_welcome_handler(pkt_header *header) {
#if 0
pkt_01_welcome table;
pkt_01_welcome_decode(&table, header);
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
do STUFF
#endif
return 0;
}

8376
code/vendors/zpl.h vendored

File diff suppressed because it is too large Load Diff