eco2d/code/common/packets/packet.c

39 lines
958 B
C
Raw Normal View History

2021-01-26 19:16:08 +00:00
#include "packet.h"
2021-05-03 22:44:39 +00:00
#include "packet_utils.h"
2021-05-03 19:53:28 +00:00
#include "cwpack/cwpack.h"
2021-01-26 19:16:08 +00:00
#define PKT_HEADER_ELEMENTS 2
pkt_handler pkt_handlers[] = {
2021-05-03 22:44:39 +00:00
{.id = MSG_ID_01_WELCOME, .handler = pkt_01_welcome_handler},
2021-01-26 19:16:08 +00:00
};
2021-05-03 22:44:39 +00:00
uint8_t pkt_buffer[PKT_BUFSIZ];
2021-01-26 19:16:08 +00:00
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};
2021-05-03 22:44:39 +00:00
pkt_unpack_msg_raw(&uc, data, datalen, PKT_HEADER_ELEMENTS);
2021-01-26 19:16:08 +00:00
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER || uc.item.as.u64 > UINT16_MAX) {
2021-05-03 22:44:39 +00:00
return -1; // invalid packet id
2021-01-26 19:16:08 +00:00
}
uint16_t pkt_id = (uint16_t)uc.item.as.u64;
cw_unpack_next(&uc);
const void *packed_blob = uc.item.as.bin.start;
2021-05-03 19:53:28 +00:00
uint32_t packed_size = uc.item.as.bin.length;
2021-01-26 19:16:08 +00:00
table->id = pkt_id;
table->data = packed_blob;
table->datalen = packed_size;
table->ok = 1;
2021-05-03 22:44:39 +00:00
return pkt_validate_eof_msg(&uc);
2021-01-26 19:16:08 +00:00
}