aboutsummaryrefslogtreecommitdiff
path: root/src/packet/inc/packet.h
blob: 6f555a6fb00866f44842943ddbe736f9cd8a49d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 */