-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_base_once.c
55 lines (41 loc) · 1.28 KB
/
event_base_once.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
#include <stdio.h>
#include <event.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
int pp[2];
struct event *evw;
struct event *evr;
void
read_cb(evutil_socket_t fd, short evtype, void *arg) {
printf("read_cb\n");
char buf[1024];
int ret = read(fd, buf, 1024);
buf[ret] = '\0';
printf("read == %s\n", buf);
/* test event_active and event_pending function. */
if(event_pending(evw, EV_WRITE, NULL) == 0) {
printf("evw being pending, make it active now.\n");
event_active(evw, EV_WRITE, 1); /* no matter the event is pending nor non-pending. */
}
}
void
write_cb(evutil_socket_t fd, short evtype, void *arg) {
printf("write_cb\n");
/* when callback is executed, event are active. */
/* just write once */
static bool bfirst = true;
if(bfirst == true) {
write(pp[1], "hello", 5);
bfirst = false;
}
}
int
main() {
pipe(pp);
struct event_base *base = event_base_new();
event_base_once(base, pp[0], EV_READ, read_cb, NULL, NULL); /* must not specify EV_PERSIST */
event_base_once(base, pp[1], EV_WRITE, write_cb, NULL, NULL);
event_base_dispatch(base);
return 0;
}