forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibgevent.c
184 lines (167 loc) · 4.28 KB
/
libgevent.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
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "libgevent.h"
#include <libmacro.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
extern const struct gevent_ops selectops;
extern const struct gevent_ops pollops;
extern const struct gevent_ops epollops;
static const struct gevent_ops *eventops[] = {
// &selectops,
&epollops,
NULL
};
static void event_in(int fd, void *arg)
{
}
struct gevent_base *gevent_base_create(void)
{
int i;
int fds[2];
struct gevent_base *eb = NULL;
if (pipe(fds)) {
perror("pipe failed");
return NULL;
}
eb = CALLOC(1, struct gevent_base);
if (!eb) {
printf("malloc gevent_base failed!\n");
close(fds[0]);
close(fds[1]);
return NULL;
}
for (i = 0; eventops[i]; i++) {
eb->ctx = eventops[i]->init();
eb->evop = eventops[i];
}
eb->loop = 1;
eb->rfd = fds[0];
eb->wfd = fds[1];
fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK);
struct gevent *e = gevent_create(eb->rfd, event_in, NULL, NULL, NULL);
gevent_add(eb, e);
return eb;
}
void gevent_base_destroy(struct gevent_base *eb)
{
if (!eb) {
return;
}
gevent_base_loop_break(eb);
close(eb->rfd);
close(eb->wfd);
eb->evop->deinit(eb->ctx);
free(eb);
}
int gevent_base_loop(struct gevent_base *eb)
{
const struct gevent_ops *evop = eb->evop;
int ret;
while (eb->loop) {
ret = evop->dispatch(eb, NULL);
if (ret == -1) {
printf("dispatch failed\n");
// return -1;
}
}
return 0;
}
int gevent_base_wait(struct gevent_base *eb)
{
const struct gevent_ops *evop = eb->evop;
return evop->dispatch(eb, NULL);
}
void gevent_base_loop_break(struct gevent_base *eb)
{
char buf[1];
buf[0] = 0;
eb->loop = 0;
if (1 != write(eb->wfd, buf, 1)) {
perror("write error");
}
}
void gevent_base_signal(struct gevent_base *eb)
{
char buf[1];
buf[0] = 0;
if (1 != write(eb->wfd, buf, 1)) {
perror("write error");
}
}
struct gevent *gevent_create(int fd,
void (ev_in)(int, void *),
void (ev_out)(int, void *),
void (ev_err)(int, void *),
void *args)
{
int flags = 0;
struct gevent *e = CALLOC(1, struct gevent);
if (!e) {
printf("malloc gevent failed!\n");
return NULL;
}
struct gevent_cbs *evcb = CALLOC(1, struct gevent_cbs);
if (!evcb) {
printf("malloc gevent failed!\n");
return NULL;
}
evcb->ev_in = ev_in;
evcb->ev_out = ev_out;
evcb->ev_err = ev_err;
evcb->args = args;
if (ev_in) {
flags |= EVENT_READ;
}
if (ev_out) {
flags |= EVENT_WRITE;
}
if (ev_err) {
flags |= EVENT_ERROR;
}
e->evfd = fd;
e->flags = flags;
e->evcb = evcb;
return e;
}
void gevent_destroy(struct gevent *e)
{
if (!e)
return;
if (e->evcb)
free(e->evcb);
free(e);
}
int gevent_add(struct gevent_base *eb, struct gevent *e)
{
if (!e || !eb) {
printf("%s:%d paraments is NULL\n", __func__, __LINE__);
return -1;
}
return eb->evop->add(eb, e);
}
int gevent_del(struct gevent_base *eb, struct gevent *e)
{
if (!e || !eb) {
printf("%s:%d paraments is NULL\n", __func__, __LINE__);
return -1;
}
return eb->evop->del(eb, e);
}