-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioloop.c
348 lines (285 loc) · 8.23 KB
/
ioloop.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <netdb.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "ioloop.h"
#include "util.h"
#include "logger.h"
#define MAX_CANCELS_TIMER 512
#define DEFAULT_EPOLL_TIMEOUT 3600000
#define MAX_EPOLL_EVENTS 1024
#define INIT_HEAP_SIZE 100
IOLoop ioloop = {0}; // loop
// 类型定义
static int is_timer_valid(void* ep){
return ((pTimer)ep)->callback != NULL;
}
static void heap_timer_free(void* ep){
pTimer timer = (pTimer)ep;
if(timer){
free(ep);
}
}
static int timer_cmp(void* e1, void* e2){
if( ((pTimer)e1)->due > ((pTimer)e2)->due)
return 1;
else if( ((pTimer)e1)->due == ((pTimer)e2)->due)
return 0;
return -1;
}
static void deque_timer_free(void* ep){
pDequeEntry entry = (pDequeEntry)ep;
if(entry->val){
heap_timer_free(entry->val);
entry->val = NULL;
}
free(entry);
}
static DequeType deqTimerType = {
NULL,
deque_timer_free
};
static HeapType heapTimerType = {
timer_cmp,
heap_timer_free,
NULL,
is_timer_valid
};
// IOLoop 定义
static void serve_once(pIOLoop loop){
long timeout = DEFAULT_EPOLL_TIMEOUT;
size_t waitfds = 0, i = 0;
struct epoll_event evs[MAX_EPOLL_EVENTS];
Conn* conn;
if(loop->stop)
return;
if(loop->ready->count)
loop->run_ready(loop);
loop->check_due_timer(loop);
if(loop->ready->count){
timeout = 0;
} else if(loop->timers->used){
timeout = ((pTimer)loop->timers->buf[0])->due - tsnow();
timeout = timeout < 0 ? 0 : timeout;
}
if( (waitfds = epoll_wait(loop->efd, evs, loop->fd_count, timeout)) == -1 ){
logwarn("epoll wait error: errno=%d", errno);
return;
}
for(i = 0; i < waitfds; i++){
conn = (Conn*)(evs[i].data.ptr);
conn->handler(loop, conn, evs[i].events, 0);
}
}
static void serve_forever(pIOLoop loop){
if(loop->tcpserver){
if(!loop->conn_register(loop, loop->tcpserver->_sconn)){
logerror("register TCP server fd faield: %d", errno);
exit(-1);
}
}
if(loop->udpserver){
if(!loop->conn_register(loop, loop->udpserver->_sconn)){
logerror("register UDP server fd faield: %d", errno);
exit(-1);
}
}
while(!loop->stop){
loop->serve_once(loop);
}
}
static int conn_modregister(pIOLoop loop, Conn* conn){
struct epoll_event ev;
ev.events = conn->events;
ev.data.ptr = (void*)conn;
if(conn->fd <= 0) // conn 已经被关闭了
return 0;
if(epoll_ctl(loop->efd, EPOLL_CTL_MOD, conn->fd, &ev) < 0 ){
logwarn("fail to mod fd %d", conn->fd);
return 0;
}
return 1;
}
static void conn_unregister(pIOLoop loop, Conn* conn){
if(!conn->registered)
return;
if(epoll_ctl(loop->efd, EPOLL_CTL_DEL, conn->fd, NULL) < 0){
logwarn("unregister fd %d error, errno: %d", conn->fd, errno);
} else{
loop->fd_count--;
}
}
static int conn_register(pIOLoop loop, Conn* conn){
struct epoll_event ev;
FD fd = conn->fd;
ev.data.ptr = (void*)conn;
ev.events = conn->events;
if(epoll_ctl(loop->efd, EPOLL_CTL_ADD, fd, &ev) < 0 ){
logwarn("fail to register FD %d with events %d to epoll", fd, conn->events);
close(conn->fd);
putback_tcpconn(conn);
return 0;
}
loop->fd_count++;
conn->registered = 1;
return 1;
}
static void _heapify_timer(pIOLoop loop){
if ( loop->timer_cancels > MAX_CANCELS_TIMER
&& loop->timer_cancels > (loop->timers->used >> 1) ){
loop->timer_cancels = 0;
heapify(loop->timers);
}
}
static void check_due_timer(pIOLoop loop){
long current_ts;
HeapEntry timer = NULL;
if(!loop->timers->used)
return;
current_ts = tsnow();
while(loop->timers->used){
timer = loop->timers->buf[0];
if( invalidHeapEntry(loop->timers, timer) ){
timer = heap_pop(loop->timers);
heapFreeEntry(loop->timers, timer); // 这是个无效的timer, 释放内存
loop->timer_cancels--;
} else if ( ((pTimer)timer)->due <= current_ts ){
timer = heap_pop(loop->timers);
// pop 出来后不释放内存, 它会转移到deque中, 从deque中pop后才释放
deque_append(loop->ready, (void*)timer);
} else{
break;
}
}
_heapify_timer(loop);
}
static void add_timer(pIOLoop loop, pTimer timer){
heap_push(loop->timers, (HeapEntry)timer);
}
static void run_ready(pIOLoop loop){
pTimer timer = NULL;
handler cb = NULL;
pDequeEntry entry = NULL;
while(loop->ready->count){
entry = deque_popleft(loop->ready);
timer = (pTimer)(entry->val);
cb = (handler)timer->callback;
cb(timer->vars, SG_TIMEOUT);
dequeFreeEntry(loop->ready, entry);
}
}
pIOLoop ioloop_current(){
if(ioloop.initial)
return &ioloop;
ioloop.ready = new_deque(&deqTimerType);
if(!ioloop.ready){
logerror("failed to create server.ready");
exit(EXIT_FAILURE);
}
ioloop.timers = new_heap(INIT_HEAP_SIZE, &heapTimerType);
if(!ioloop.timers){
logerror("failed to create server.timers with size %d", INIT_HEAP_SIZE);
exit(EXIT_FAILURE);
}
ioloop.efd = epoll_create(MAX_EPOLL_EVENTS);
ioloop.connections = NULL; // 没有初始化, 不知道存什么好
ioloop.timer_cancels = 0;
ioloop.stop = 0;
ioloop.initial = 1;
ioloop.serve_forever = serve_forever;
ioloop.run_ready = run_ready;
ioloop.check_due_timer = check_due_timer;
ioloop.conn_register = conn_register;
ioloop.conn_unregister = conn_unregister;
ioloop.conn_modregister = conn_modregister;
ioloop.serve_once = serve_once;
ioloop.add_timer = add_timer;
return &ioloop;
}
void dealloc_ioloop(IOLoop* loop){
dealloc_deque(ioloop.ready);
ioloop.ready = NULL;
dealloc_heap(ioloop.timers);
ioloop.timers = NULL;
if(loop->tcpserver){
dealloc_tcpserver(loop->tcpserver);
loop->tcpserver = NULL;
}
if(loop->udpserver){
dealloc_udpserver(loop->udpserver);
loop->udpserver = NULL;
}
loop->initial = 0;
}
#ifdef TEST_TIMER
void cb(void* vars, Signal signal){
logdebug("%s", (char*)vars);
// 不需要释放内存
}
void test_timer(pIOLoop loop, long delay){
pTimer timer = (pTimer)malloc(sizeof(Timer));
timer->callback = cb;
timer->due = tsnow() + delay;
timer->vars = "hello world";
loop->add_timer(loop, timer);
}
void on_read(Conn* conn){
//printf("fd %d on read]\n", conn->fd);
RBSeg seg;
conn->write(conn, "[Server]", 8);
conn->write(conn, strnow(), TIME_BUF_SIZE);
while( rb_readable(conn->rbuf, &seg) ){
fwrite(seg.buf, seg.len, 1, stdout);
rb_start_forward(conn->rbuf, seg.len);
conn->write(conn, seg.buf, seg.len);
}
}
void on_udpread(Conn* conn){
RBSeg seg;
while( rb_readable(conn->rbuf, &seg) ){
fwrite(seg.buf, seg.len, 1, stdout);
rb_start_forward(conn->rbuf, seg.len);
conn->write(conn, seg.buf, seg.len);
}
putback_udpconn(conn);
}
void on_write(Conn* conn){
logdebug("all data were written to buffer");
}
void on_close(Conn* conn){
logdebug("conn closed!");
}
int main(){
TCPServer* tcpserver = new_tcpserver(on_read, on_write, on_close);
tcpserver->bind(tcpserver, 0, 28080)->listen(tcpserver, 128);
if(!tcpserver->start(tcpserver)){
dealloc_tcpserver(tcpserver);
return -1;
}
UDPServer* udpserver = new_udpserver(on_udpread, on_write, on_close);
udpserver->bind(udpserver, 0, 28080);
if(!udpserver->start(udpserver)){
dealloc_udpserver(udpserver);
return -1;
}
IOLoop* loop = ioloop_current();
#ifdef TEST_TIMER
long mask = (1 << 16) - 1;
for(int i = 0; i< 10; i ++){
long delay = rand();
test_timer(loop, delay & mask);
}
#endif
set_loglevel(LOGLV_DEBUG);
init_conn_cache(100);
loop->serve_forever(loop);
dealloc_ioloop(loop);
}
#endif