-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathev_common.h
108 lines (94 loc) · 3.62 KB
/
ev_common.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef ev_common_h
#define ev_common_h
/*
+---------------+ +------------------+
| struct peer | +------>| enum peer_state |
+---------------+ | +------------------+
| fd | | | PS_READ |
| state >---+ | PS_WRITE |
| writebuff[] | +------------------+
| writerb |
| *handler | +------------------+
+---------------+ +-->| struct ev_epoll |
| +------------------+
+-----------------+ +---------------+ | | epollfd |
| struct ev | +-->| union ev_priv | | +------------------+
+-----------------+ | +---------------+ |
| id | | | ev_epoll >-------+ +------------------+
| forks | | | ev_select >---------->| struct ev_select |
| children[] | | | ev_mock >-------+ +------------------+
| private_data >--+ +---------------+ | | ? |
+-----------------+ | +----------------- +
| *on_recvd | |
| *on_writefinish | | +------------------+
+-----------------+ +-->| struct ev_mock |
^ ^ +------------------+
| | | ? |
| +--------------+ +------------------+
| |
+---^-----------+ +---^------------+
| struct ev_srv | | struct ev_clnt |
+---------------+ +----------------+
| +struct ev | | +struct ev |
| listenfd | | hostname |
| bind | | port |
+---------------+ +----------------+
| *on_connect | | ? |
+---------------+ +----------------+
*/
#include "options.h"
#include "ringbuffer.h"
#include <sys/types.h>
#include <sys/epoll.h>
#include <inttypes.h>
#include <stdbool.h>
struct ev_epoll;
union ev_priv {
struct ev_epoll *epoll;
};
enum peer_state {
PS_UNKNOWN,
PS_CLOSE,
PS_READ,
PS_WRITE
};
struct ev;
struct evs;
struct peer {
int fd;
enum peer_state status;
char writebuff[EV_WRITE_BUFFSIZE];
struct ringbuffer writerb;
void *handler;
};
typedef void (*evs_conncb_t)(struct evs * evs, struct peer * c);
typedef void (*ev_writecb_t)(struct ev * ev, struct peer * c);
typedef void (*ev_recvcb_t)(struct ev * ev, struct peer * c, const char *data,
size_t len);
struct ev {
uint8_t id;
uint8_t forks;
bool cancel;
pid_t *children;
union ev_priv;
ev_recvcb_t on_recvd;
ev_writecb_t on_writefinish;
};
struct evs {
struct ev;
int listenfd;
uint16_t bind;
evs_conncb_t on_connect;
evs_conncb_t on_disconnect;
};
typedef int (*ev_cb_t)(struct ev * ev);
struct peer *ev_common_newconn(struct evs *evs);
void ev_common_peer_disconn(struct evs *evs, struct peer *c);
void ev_common_read(struct ev *ev, struct peer *c);
void ev_common_write(struct ev *ev, struct peer *c);
int ev_common_terminate(struct ev *ev);
int ev_common_join(struct ev *ev);
int ev_common_fork(struct ev *ev, ev_cb_t loop);
void ev_common_init(struct ev *ev);
void ev_common_deinit(struct ev *ev);
#endif