aboutsummaryrefslogtreecommitdiff
path: root/src/net/inc/net.h
blob: f7f12586c203d6f0debe5096a497d66ec0e11404 (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
#ifndef NET_H
#define NET_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

enum net_domain
{
    NET_DOMAIN_IPV4,
    NET_DOMAIN_SERIAL
};

union net_connect
{
    struct net_connect_common
    {
        enum net_domain domain;

        struct net_connect_ev
        {
            void (*connected)(void *arg);
            void (*disconnected)(void *arg);
            void *arg;
        } ev;
    } common;

    struct net_connect_ipv4
    {
        struct net_connect_common common;
        const char *addr;
        uint16_t port;
    } ipv4;

    struct net_connect_serial
    {
        struct net_connect_common common;
        const char *dev;
        unsigned long baud;

        enum
        {
            NET_PARITY_NONE,
            NET_PARITY_ODD,
            NET_PARITY_EVEN
        } parity;
    } serial;
};

union net_server
{
    struct net_server_common
    {
        enum net_domain domain;
        unsigned max_players;
    } common;

    struct net_server_ipv4
    {
        struct net_server_common common;
        uint16_t port;
    } ipv4;

    struct net_server_serial
    {
        struct net_server_common common;
    } serial;
};

struct net_socket;

int net_init(void);
void net_deinit(void);
int net_update(struct net_socket *s);
bool net_available(enum net_domain d);
struct net_socket *net_server(const union net_server *srv);
struct net_socket *net_connect(const union net_connect *c);
int net_read(struct net_socket *s, void *buf, size_t n);
int net_write(struct net_socket *s, const void *buf, size_t n);
int net_close(struct net_socket *s);
const char *net_domain_str(enum net_domain d);

#endif /* NET_H */