additional packing helpers
parent
93c68ee3c8
commit
cf955ef3fb
|
@ -8,6 +8,7 @@
|
||||||
#include "librg.h"
|
#include "librg.h"
|
||||||
|
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
#include "packets/packet.h"
|
||||||
|
|
||||||
#define NETWORK_UPDATE_DELAY 0.100
|
#define NETWORK_UPDATE_DELAY 0.100
|
||||||
|
|
||||||
|
@ -108,6 +109,15 @@ int32_t network_client_tick() {
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_RECEIVE: {
|
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 */
|
/* handle a newly received event */
|
||||||
// librg_world_read(
|
// librg_world_read(
|
||||||
// world,
|
// world,
|
||||||
|
|
|
@ -99,5 +99,3 @@ int main(int argc, char** argv) {
|
||||||
void platform_shutdown(void) {
|
void platform_shutdown(void) {
|
||||||
is_running = false;
|
is_running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "packets/pkt_01_welcome.c"
|
|
||||||
|
|
|
@ -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) {
|
for (pkt_desc *field = desc; field->type != 0; ++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
|
||||||
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);
|
||||||
|
@ -55,3 +56,5 @@ 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"
|
|
@ -41,7 +41,7 @@ inline int32_t pkt_validate_eof_msg(cw_unpack_context *uc) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef PKT_STRUCT_PTR
|
#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
|
#endif
|
||||||
|
|
||||||
#ifndef PKT_FIELD
|
#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
|
#define PKT_END .type = CWP_NOT_AN_ITEM
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef PKT_IF
|
||||||
|
#define PKT_IF(c) if (c < 0) return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct pkt_desc {
|
typedef struct pkt_desc {
|
||||||
cwpack_item_types type;
|
cwpack_item_types type;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
|
|
|
@ -4,12 +4,7 @@
|
||||||
|
|
||||||
#define PKT_01_WELCOME_ARGS 2
|
#define PKT_01_WELCOME_ARGS 2
|
||||||
|
|
||||||
pkt_desc pkt_01_welcome_desc[3] = {
|
#ifdef SERVER
|
||||||
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) },
|
|
||||||
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_amount) },
|
|
||||||
{ PKT_END },
|
|
||||||
};
|
|
||||||
|
|
||||||
size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
|
size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
|
||||||
cw_pack_context pc = {0};
|
cw_pack_context pc = {0};
|
||||||
pkt_pack_msg(&pc, PKT_01_WELCOME_ARGS);
|
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 */
|
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) {
|
int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) {
|
||||||
cw_unpack_context uc = {0};
|
cw_unpack_context uc = {0};
|
||||||
pkt_unpack_msg(&uc, header, PKT_01_WELCOME_ARGS);
|
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) {
|
int32_t pkt_01_welcome_handler(pkt_header *header) {
|
||||||
#if 1
|
|
||||||
pkt_01_welcome table;
|
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);
|
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
Loading…
Reference in New Issue