-
Notifications
You must be signed in to change notification settings - Fork 91
/
framework.c
599 lines (505 loc) · 15.9 KB
/
framework.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright (c) 2018-2020 The EFramework Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "framework.h"
#include "coroutine.h"
#include "util/list.h"
#include "util/util.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
/*
* the global pointer
*/
ef_runtime_t *ef_runtime = NULL;
inline int ef_queue_fd(ef_runtime_t *rt, ef_listen_info_t *li, int fd) __attribute__((always_inline));
inline int ef_routine_run(ef_runtime_t *rt, ef_routine_proc_t proc, int socket) __attribute__((always_inline));
long ef_proc(void *param)
{
ef_routine_t *er = (ef_routine_t*)param;
int fd = er->poll_data.fd;
long retval = 0;
if (er->poll_data.ef_proc) {
retval = er->poll_data.ef_proc(fd, er);
}
/*
* it may or may not closed by the user code
*/
ef_routine_close(er, fd);
return retval;
}
inline int ef_routine_run(ef_runtime_t *rt, ef_routine_proc_t proc, int socket)
{
ef_routine_t *er = (ef_routine_t*)ef_coroutine_create(&rt->co_pool, sizeof(ef_routine_t), ef_proc, NULL);
if (er) {
er->poll_data.type = FD_TYPE_RWC;
er->poll_data.fd = socket;
er->poll_data.routine_ptr = er;
er->poll_data.runtime_ptr = rt;
er->poll_data.ef_proc = proc;
ef_coroutine_resume(&rt->co_pool, &er->co, 0);
return 0;
}
return -1;
}
inline int ef_queue_fd(ef_runtime_t *rt, ef_listen_info_t *li, int fd)
{
ef_queue_fd_t *qf;
int retval, flags;
flags = fcntl(fd, F_GETFL);
if (!(flags & O_NONBLOCK)) {
retval = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (retval < 0) {
return retval;
}
}
if (!ef_list_empty(&rt->free_fd_list)) {
qf = CAST_PARENT_PTR(ef_list_remove_after(&rt->free_fd_list), ef_queue_fd_t, list_entry);
} else {
qf = (ef_queue_fd_t*)malloc(sizeof(ef_queue_fd_t));
}
/*
* close the new connection when no memory
*/
if (!qf) {
close(fd);
return -1;
}
qf->fd = fd;
ef_list_insert_before(&li->fd_list, &qf->list_entry);
return 0;
}
int ef_init(ef_runtime_t *rt, size_t stack_size, int limit_min, int limit_max, int shrink_millisecs, int count_per_shrink)
{
ef_poll_t *p = ef_create_poll(1024);
if (!p) {
return -1;
}
/*
* the global pointer
*/
ef_runtime = rt;
rt->p = p;
rt->stopping = 0;
rt->shrink_millisecs = shrink_millisecs;
rt->count_per_shrink = count_per_shrink;
if (ef_coroutine_pool_init(&rt->co_pool, stack_size, limit_min, limit_max) < 0) {
return -1;
}
ef_list_init(&rt->listen_list);
ef_list_init(&rt->free_fd_list);
return 0;
}
int ef_add_listen(ef_runtime_t *rt, int socket, ef_routine_proc_t proc)
{
/*
* set the listen socket in non-block mode
*/
int retval = fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
if (retval < 0) {
return retval;
}
ef_listen_info_t *li = (ef_listen_info_t*)malloc(sizeof(ef_listen_info_t));
if (li == NULL) {
return -1;
}
li->poll_data.type = FD_TYPE_LISTEN;
li->poll_data.fd = socket;
li->poll_data.routine_ptr = NULL;
li->poll_data.runtime_ptr = rt;
li->ef_proc = proc;
ef_list_init(&li->fd_list);
ef_list_insert_after(&rt->listen_list, &li->list_entry);
return 0;
}
int ef_run_loop(ef_runtime_t *rt)
{
ef_event_t evts[1024];
/*
* add all listen socket fds to poll object
*/
ef_list_entry_t *ent = ef_list_entry_after(&rt->listen_list);
while (ent != &rt->listen_list) {
ef_listen_info_t *li = CAST_PARENT_PTR(ent, ef_listen_info_t, list_entry);
int ret = rt->p->associate(rt->p, li->poll_data.fd, EF_POLLIN, &li->poll_data, 0);
if (ret < 0) {
return ret;
}
ent = ef_list_entry_after(ent);
}
/*
* the main event loop
*/
while (1) {
int cnt = rt->p->wait(rt->p, &evts[0], 1024, 1000);
if (cnt < 0 && errno != EINTR) {
return cnt;
}
/*
* check all events returned by poll wait function
*/
for (int i = 0; i < cnt; ++i) {
ef_poll_data_t *ed = (ef_poll_data_t*)evts[i].ptr;
if (ed->type == FD_TYPE_LISTEN) {
while (1) {
int socket = accept(ed->fd, NULL, NULL);
if (socket < 0) {
rt->p->unset(rt->p, ed->fd, EF_POLLIN);
break;
}
ef_listen_info_t *li = CAST_PARENT_PTR(ed, ef_listen_info_t, poll_data);
/*
* put new connection to queue
*/
int ret = ef_queue_fd(rt, li, socket);
if (ret < 0) {
break;
}
}
/*
* solaris event port will auto dissociate fd after event fired
*/
rt->p->associate(rt->p, ed->fd, EF_POLLIN, ed, 1);
} else if (ed->type == FD_TYPE_RWC) {
ef_coroutine_resume(&rt->co_pool, &ed->routine_ptr->co, evts[i].events);
}
}
/*
* handle queued connections
*/
ent = ef_list_entry_after(&rt->listen_list);
/*
* every listening sockets
*/
while (ent != &rt->listen_list) {
ef_listen_info_t *li = CAST_PARENT_PTR(ent, ef_listen_info_t, list_entry);
ef_list_entry_t *enf = ef_list_entry_after(&li->fd_list);
/*
* every queued connection
*/
while (enf != &li->fd_list) {
ef_queue_fd_t *qf = CAST_PARENT_PTR(enf, ef_queue_fd_t, list_entry);
enf = ef_list_entry_after(enf);
int ret = ef_routine_run(rt, li->ef_proc, qf->fd);
if (ret < 0) {
goto exit_queue;
} else {
ef_list_remove(&qf->list_entry);
ef_list_insert_after(&rt->free_fd_list, &qf->list_entry);
}
}
ent = ef_list_entry_after(ent);
}
exit_queue:
if (rt->stopping) {
/*
* close all listening socket
*/
if (!ef_list_empty(&rt->listen_list)) {
ent = ef_list_entry_after(&rt->listen_list);
while (ent != &rt->listen_list) {
ef_listen_info_t *li = CAST_PARENT_PTR(ent, ef_listen_info_t, list_entry);
ent = ef_list_entry_after(ent);
/*
* close listening socket
*/
if (li->poll_data.fd >= 0) {
rt->p->dissociate(rt->p, li->poll_data.fd, 0, 0);
close(li->poll_data.fd);
li->poll_data.fd = -1;
}
/*
* free listen info if connection queue empty
*/
if (ef_list_empty(&li->fd_list)) {
ef_list_remove(&li->list_entry);
free(li);
}
}
}
/*
* destroy the unused fd queue item
*/
if (!ef_list_empty(&rt->free_fd_list)) {
ent = ef_list_remove_after(&rt->free_fd_list);
while (ent != NULL) {
ef_queue_fd_t *qf = CAST_PARENT_PTR(ent, ef_queue_fd_t, list_entry);
ent = ef_list_remove_after(&rt->free_fd_list);
free(qf);
}
}
/*
* shrink coroutine pool, to free
*/
if (rt->co_pool.free_count == rt->co_pool.full_count) {
rt->p->free(rt->p);
ef_coroutine_pool_shrink(&rt->co_pool, 0, -rt->co_pool.full_count);
break;
} else {
ef_coroutine_pool_shrink(&rt->co_pool, 0, -rt->co_pool.free_count);
}
}
if (rt->co_pool.free_count > 0 && rt->co_pool.full_count > rt->co_pool.limit_min) {
ef_coroutine_pool_shrink(&rt->co_pool, rt->shrink_millisecs, rt->count_per_shrink);
}
}
return 0;
}
int ef_routine_close(ef_routine_t *er, int fd)
{
if (er == NULL) {
er = ef_routine_current();
}
/*
* dissociate fd before close
*/
er->poll_data.runtime_ptr->p->dissociate(er->poll_data.runtime_ptr->p, fd, 0, 1);
return close(fd);
}
int ef_routine_connect(ef_routine_t *er, int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int retval, flags, error = 0;
long events;
if (er == NULL) {
er = ef_routine_current();
}
er->poll_data.type = FD_TYPE_RWC;
er->poll_data.fd = sockfd;
/*
* set non-block mode if needed
*/
flags = fcntl(sockfd, F_GETFL);
if (!(flags & O_NONBLOCK)) {
retval = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
if (retval < 0) {
error = errno;
goto exit_conn;
}
}
retval = connect(sockfd, addr, addrlen);
if (retval < 0) {
if (errno != EINPROGRESS) {
error = errno;
goto exit_conn;
}
retval = er->poll_data.runtime_ptr->p->associate(er->poll_data.runtime_ptr->p, sockfd, EF_POLLOUT, &er->poll_data, 0);
if (retval < 0) {
error = errno;
goto exit_conn;
}
} else {
return retval;
}
/*
* yield and wait event
*/
events = ef_fiber_yield(er->co.fiber.sched, 0);
if (events & (EF_POLLERR | EF_POLLHUP)) {
error = EBADF;
retval = -1;
} else if (events & EF_POLLOUT) {
socklen_t len = sizeof(error);
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len);
}
/*
* dissociate fd after event fired
*/
er->poll_data.runtime_ptr->p->dissociate(er->poll_data.runtime_ptr->p, sockfd, 1, 0);
exit_conn:
errno = error;
return retval;
}
ssize_t ef_routine_read(ef_routine_t *er, int fd, void *buf, size_t count)
{
int error = 0;
long events;
ssize_t retval;
if (er == NULL) {
er = ef_routine_current();
}
er->poll_data.type = FD_TYPE_RWC;
er->poll_data.fd = fd;
/*
* always associate
*/
retval = er->poll_data.runtime_ptr->p->associate(er->poll_data.runtime_ptr->p, fd, EF_POLLIN, &er->poll_data, 0);
if (retval < 0) {
return retval;
} else if (retval > 0) {
goto ready;
}
yield:
/*
* yield and wait event
*/
events = ef_fiber_yield(er->co.fiber.sched, 0);
if (events & EF_POLLERR) {
error = EBADF;
retval = -1;
} else if (events & (EF_POLLIN | EF_POLLHUP)) {
ready:
retval = read(fd, buf, count);
if (retval < 0 && errno == EAGAIN) {
er->poll_data.runtime_ptr->p->unset(er->poll_data.runtime_ptr->p, fd, EF_POLLIN | EF_POLLHUP);
goto yield;
} else if (retval < 0) {
error = errno;
}
}
/*
* dissociate fd after event fired
*/
er->poll_data.runtime_ptr->p->dissociate(er->poll_data.runtime_ptr->p, fd, 1, 0);
errno = error;
return retval;
}
ssize_t ef_routine_write(ef_routine_t *er, int fd, const void *buf, size_t count)
{
int error = 0;
long events;
ssize_t retval;
if (er == NULL) {
er = ef_routine_current();
}
er->poll_data.type = FD_TYPE_RWC;
er->poll_data.fd = fd;
/*
* always associate
*/
retval = er->poll_data.runtime_ptr->p->associate(er->poll_data.runtime_ptr->p, fd, EF_POLLOUT, &er->poll_data, 0);
if (retval < 0) {
return retval;
} else if (retval > 0) {
goto ready;
}
yield:
/*
* yield and wait event
*/
events = ef_fiber_yield(er->co.fiber.sched, 0);
if (events & (EF_POLLERR | EF_POLLHUP)) {
error = EBADF;
retval = -1;
} else if(events & EF_POLLOUT) {
ready:
retval = write(fd, buf, count);
if (retval < 0 && errno == EAGAIN) {
er->poll_data.runtime_ptr->p->unset(er->poll_data.runtime_ptr->p, fd, EF_POLLOUT);
goto yield;
} else if (retval < 0) {
error = errno;
}
}
/*
* dissociate fd after event fired
*/
er->poll_data.runtime_ptr->p->dissociate(er->poll_data.runtime_ptr->p, fd, 1, 0);
errno = error;
return retval;
}
ssize_t ef_routine_recv(ef_routine_t *er, int sockfd, void *buf, size_t len, int flags)
{
int retval, error = 0;
long events;
if (er == NULL) {
er = ef_routine_current();
}
er->poll_data.type = FD_TYPE_RWC;
er->poll_data.fd = sockfd;
/*
* always associate
*/
retval = er->poll_data.runtime_ptr->p->associate(er->poll_data.runtime_ptr->p, sockfd, EF_POLLIN, &er->poll_data, 0);
if (retval < 0) {
return retval;
} else if (retval > 0) {
goto ready;
}
yield:
/*
* yield and wait event
*/
events = ef_fiber_yield(er->co.fiber.sched, 0);
if (events & EF_POLLERR) {
error = EBADF;
retval = -1;
} else if (events & (EF_POLLIN | EF_POLLHUP)) {
ready:
retval = recv(sockfd, buf, len, flags);
if (retval < 0 && errno == EAGAIN) {
er->poll_data.runtime_ptr->p->unset(er->poll_data.runtime_ptr->p, sockfd, EF_POLLIN | EF_POLLHUP);
goto yield;
} else if (retval < 0) {
error = errno;
}
}
/*
* dissociate fd after event fired
*/
er->poll_data.runtime_ptr->p->dissociate(er->poll_data.runtime_ptr->p, sockfd, 1, 0);
errno = error;
return retval;
}
ssize_t ef_routine_send(ef_routine_t *er, int sockfd, const void *buf, size_t len, int flags)
{
int retval, error = 0;
long events;
if (er == NULL) {
er = ef_routine_current();
}
er->poll_data.type = FD_TYPE_RWC;
er->poll_data.fd = sockfd;
/*
* always associate
*/
retval = er->poll_data.runtime_ptr->p->associate(er->poll_data.runtime_ptr->p, sockfd, EF_POLLOUT, &er->poll_data, 0);
if (retval < 0) {
return retval;
} else if (retval > 0) {
goto ready;
}
yield:
/*
* yield and wait event
*/
events = ef_fiber_yield(er->co.fiber.sched, 0);
if (events & (EF_POLLERR | EF_POLLHUP)) {
error = EBADF;
retval = -1;
} else if(events & EF_POLLOUT) {
ready:
retval = send(sockfd, buf, len, flags);
if (retval < 0 && errno == EAGAIN) {
er->poll_data.runtime_ptr->p->unset(er->poll_data.runtime_ptr->p, sockfd, EF_POLLOUT);
goto yield;
} else if (retval < 0) {
error = errno;
}
}
/*
* dissociate fd after event fired
*/
er->poll_data.runtime_ptr->p->dissociate(er->poll_data.runtime_ptr->p, sockfd, 1, 0);
errno = error;
return retval;
}