2 * XXX This sample code was once meant to show how to use the basic Libevent
3 * interfaces, but it never worked on non-Unix platforms, and some of the
4 * interfaces have changed since it was first written. It should probably
5 * be removed or replaced with something better.
8 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
11 #include <event2/event-config.h>
13 #include <sys/types.h>
16 #include <sys/queue.h>
32 fifo_read(int fd
, short event
, void *arg
)
36 struct event
*ev
= arg
;
41 /* Reschedule this event */
44 fprintf(stderr
, "fifo_read called with fd: %d, event: %d, arg: %p\n",
47 len
= ReadFile((HANDLE
)fd
, buf
, sizeof(buf
) - 1, &dwBytesRead
, NULL
);
49 /* Check for end of file. */
50 if (len
&& dwBytesRead
== 0) {
51 fprintf(stderr
, "End Of File");
56 buf
[dwBytesRead
] = '\0';
58 len
= read(fd
, buf
, sizeof(buf
) - 1);
63 } else if (len
== 0) {
64 fprintf(stderr
, "Connection closed\n");
70 fprintf(stdout
, "Read: %s\n", buf
);
74 main(int argc
, char **argv
)
80 socket
= CreateFileA("test.txt", /* open File */
81 GENERIC_READ
, /* open for reading */
83 NULL
, /* no security */
84 OPEN_EXISTING
, /* existing file only */
85 FILE_ATTRIBUTE_NORMAL
, /* normal file */
86 NULL
); /* no attr. template */
88 if (socket
== INVALID_HANDLE_VALUE
)
93 const char *fifo
= "event.fifo";
96 if (lstat(fifo
, &st
) == 0) {
97 if ((st
.st_mode
& S_IFMT
) == S_IFREG
) {
105 if (mkfifo(fifo
, 0600) == -1) {
110 /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
112 socket
= open(fifo
, O_RDWR
| O_NONBLOCK
, 0);
114 socket
= open(fifo
, O_RDONLY
| O_NONBLOCK
, 0);
122 fprintf(stderr
, "Write data to %s\n", fifo
);
124 /* Initalize the event library */
127 /* Initalize one event */
129 event_set(&evfifo
, (int)socket
, EV_READ
, fifo_read
, &evfifo
);
131 event_set(&evfifo
, socket
, EV_READ
, fifo_read
, &evfifo
);
134 /* Add it to the active events, without a timeout */
135 event_add(&evfifo
, NULL
);