diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-09-27 17:03:06 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-11-01 16:26:16 +0100 |
| commit | 980858186149651df5543b6fc99a4f7db0cdd089 (patch) | |
| tree | d347200b0a562d84df505097651ad0642f207fdd /src/packet/inc | |
| parent | 39f50e601d395bbd2d78d0147ac530b756da2fff (diff) | |
| download | jancity-980858186149651df5543b6fc99a4f7db0cdd089.tar.gz | |
WIP
Diffstat (limited to 'src/packet/inc')
| -rw-r--r-- | src/packet/inc/packet.h | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/packet/inc/packet.h b/src/packet/inc/packet.h new file mode 100644 index 0000000..6f555a6 --- /dev/null +++ b/src/packet/inc/packet.h @@ -0,0 +1,91 @@ +#ifndef PACKET_H +#define PACKET_H + +#include <game.h> +#include <net.h> +#include <stdbool.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +union packet +{ + struct packet_common + { + enum packet_type + { + PACKET_TYPE_NAME, + PACKET_TYPE_CHAT, + PACKET_TYPE_READY, + + MAX_PACKET_TYPES + } type; + } common; + + struct packet_name + { + struct packet_common common; + const char *name; + } name; + + struct packet_chat + { + struct packet_common common; + const char *msg; + } chat; + + struct packet_ready + { + struct packet_common common; + bool ready; + } ready; +}; + +enum {PACKET_MAX_MESSAGE_LEN = 64}; + +struct packet_ctx +{ + struct net_host *host; + void (*cb)(net_peer, const union packet *, void *); + void *arg; +}; + +struct packet_input +{ + union packet p; + size_t body_i; + + enum + { + PACKET_STATE_HEADER, + PACKET_STATE_BODY + } state; + + union packet_data + { + struct packet_ctx_name + { + size_t len; + char name[GAME_PLAYER_NAME_LEN]; + } name; + + struct packet_ctx_chat + { + size_t len; + char msg[PACKET_MAX_MESSAGE_LEN]; + } chat; + } data; +}; + +int packet_send(const struct packet_ctx *h, net_peer peer, const union packet *pkt); +int packet_feed(const struct packet_ctx *h, net_peer p, + struct packet_input *in, const void *buf, size_t n); + +#ifdef __cplusplus +} +#endif + +#endif /* PACKET_H */ |
