1 /* $NetBSD: event-test.c,v 1.1.1.1 2013/04/11 16:43:31 christos Exp $ */
3 * XXX This sample code was once meant to show how to use the basic Libevent
4 * interfaces, but it never worked on non-Unix platforms, and some of the
5 * interfaces have changed since it was first written. It should probably
6 * be removed or replaced with something better.
9 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
12 #include <event2/event-config.h>
14 #include <sys/types.h>
17 #include <sys/queue.h>
33 fifo_read(evutil_socket_t fd
, short event
, void *arg
)
37 struct event
*ev
= arg
;
42 /* Reschedule this event */
45 fprintf(stderr
, "fifo_read called with fd: %d, event: %d, arg: %p\n",
48 len
= ReadFile((HANDLE
)fd
, buf
, sizeof(buf
) - 1, &dwBytesRead
, NULL
);
50 /* Check for end of file. */
51 if (len
&& dwBytesRead
== 0) {
52 fprintf(stderr
, "End Of File");
57 buf
[dwBytesRead
] = '\0';
59 len
= read(fd
, buf
, sizeof(buf
) - 1);
64 } else if (len
== 0) {
65 fprintf(stderr
, "Connection closed\n");
71 fprintf(stdout
, "Read: %s\n", buf
);
75 main(int argc
, char **argv
)
81 socket
= CreateFileA("test.txt", /* open File */
82 GENERIC_READ
, /* open for reading */
84 NULL
, /* no security */
85 OPEN_EXISTING
, /* existing file only */
86 FILE_ATTRIBUTE_NORMAL
, /* normal file */
87 NULL
); /* no attr. template */
89 if (socket
== INVALID_HANDLE_VALUE
)
94 const char *fifo
= "event.fifo";
97 if (lstat(fifo
, &st
) == 0) {
98 if ((st
.st_mode
& S_IFMT
) == S_IFREG
) {
106 if (mkfifo(fifo
, 0600) == -1) {
111 /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
113 socket
= open(fifo
, O_RDWR
| O_NONBLOCK
, 0);
115 socket
= open(fifo
, O_RDONLY
| O_NONBLOCK
, 0);
123 fprintf(stderr
, "Write data to %s\n", fifo
);
125 /* Initalize the event library */
128 /* Initalize one event */
130 event_set(&evfifo
, (evutil_socket_t
)socket
, EV_READ
, fifo_read
, &evfifo
);
132 event_set(&evfifo
, socket
, EV_READ
, fifo_read
, &evfifo
);
135 /* Add it to the active events, without a timeout */
136 event_add(&evfifo
, NULL
);