epoll et demo

简单实现了一个 epoll et 的服务器,接收数据并打印

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/socket.h>

#define PERROR() printf("%s:%d: %s\n", __FUNCTION__, __LINE__, strerror(errno))

#define MAX_EVENTS (64)

#define SERVER_HOST NULL
#define SERVER_PORT (8880)

struct event_data {
int fd;
int type;
int r;
int w;
};

int epfd = -1;

int ep_add(int fd, int e, void *data)
{
struct epoll_event ev;

fcntl(fd, F_SETFL, O_NONBLOCK);
ev.events = e;
ev.data.ptr = data;
return epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
}

int ep_del(int fd)
{
struct epoll_event ev;

return epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);
}

int enum_addr(const char *host, int port, int flags, int (*f)(const struct addrinfo *ai))
{
char service[12];
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo *ai;
int r;

sprintf(service, "%d", port);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = flags;
r = getaddrinfo(host, service, &hints, &res);
if (r == 0) {
for (ai = res; ai != NULL; ai = ai->ai_next) {
if (f(ai)) {
break;
}
}
freeaddrinfo(res);
}
return r;
}

int accept_client(int s, void (*f)(int, struct sockaddr *, socklen_t))
{
struct sockaddr in_addr;
socklen_t in_len;
int c;
int r;

r = 0;
for (;;) {
c = accept(s, &in_addr, &in_len);
if (c == -1) {
if (errno != EAGAIN) {
r = 1;
}
break;
}
printf("%d accept\n", c);
f(c, &in_addr, in_len);
}
return r;
}

int echo_data(int s)
{
char buf[1024];
int n;
int r;

puts("read");
r = 0;
for (;;) {
n = read(s, buf, 1023);
if (n == 0) {
printf("%d EOF\n", s);
r = 2;
break;
}
if (n == -1) {
if (errno != EAGAIN) {
r = 1;
}
break;
}
buf[n] = 0;
puts(buf);
}
return r;
}

void on_accept_client(int s, struct sockaddr *addr, socklen_t addrlen)
{
struct event_data *d;

d = (struct event_data *)malloc(sizeof(struct event_data));
d->fd = s;
d->type = 1;
d->r = 0;
d->w = 0;
ep_add(s, EPOLLET | EPOLLIN | EPOLLOUT | EPOLLRDHUP, d);
}

void on_poll(struct event_data *data)
{
int r;

switch (data->type) {
case 0:
if (data->r) {
r = accept_client(data->fd, on_accept_client);
data->r = 0;
}
break;
case 1:
if (data->r) {
r = echo_data(data->fd);
data->r = 0;
}
break;
}
if (r != 0) {
if (r != 2) {
PERROR();
}
ep_del(data->fd);
printf("%d close\n", data->fd);
close(data->fd);
free(data);
}
}

int on_listen(const struct addrinfo *ai)
{
int s;
struct event_data *d;
int on;

on = 1;
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (s != -1) {
if (ai->ai_family == AF_INET6) {
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
}
if (bind(s, ai->ai_addr, ai->ai_addrlen) == 0) {
d = (struct event_data *)malloc(sizeof(struct event_data));
d->fd = s;
d->type = 0;
d->r = 0;
d->w = 0;
ep_add(s, EPOLLET | EPOLLIN, d);
printf("%d listen\n", s);
if (listen(s, SOMAXCONN) == 0) {
return 0;
} else {
ep_del(s);
free(d);
}
}
}
PERROR();
if (s != -1) {
close(s);
}
return 0;
}

int main()
{
struct epoll_event events[MAX_EVENTS];
struct event_data *data;
int r;
int n;
int i;
uint32_t ev;

r = 0;
epfd = epoll_create1(0);
if (epfd == -1) {
PERROR();
return 1;
}
r = enum_addr(SERVER_HOST, SERVER_PORT, AI_PASSIVE, on_listen);
if (r != 0) {
PERROR();
} else {
for (;;) {
n = epoll_wait(epfd, events, MAX_EVENTS, -1);
if (n == -1) {
if (errno == EINTR) {
continue;
}
break;
}
for (i = 0; i < n; i++) {
data = (struct event_data *)events[i].data.ptr;
ev = events[i].events;
if (ev & (EPOLLIN | EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
data->r = 1;
}
if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) {
data->w = 1;
}
printf("%d r %d w %d\n", data->fd, data->r, data->w);
on_poll(data);
}
}
}
if (epfd != -1) {
close(epfd);
}
return 0;
}