3 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
10 #include <sys/types.h>
13 #include <sys/queue.h>
28 fifo_read(int fd
, short event
, void *arg
)
32 struct event
*ev
= arg
;
37 /* Reschedule this event */
40 fprintf(stderr
, "fifo_read called with fd: %d, event: %d, arg: %p\n",
43 len
= ReadFile((HANDLE
)fd
, buf
, sizeof(buf
) - 1, &dwBytesRead
, NULL
);
45 // Check for end of file.
46 if(len
&& dwBytesRead
== 0) {
47 fprintf(stderr
, "End Of File");
52 buf
[dwBytesRead
] = '\0';
54 len
= read(fd
, buf
, sizeof(buf
) - 1);
59 } else if (len
== 0) {
60 fprintf(stderr
, "Connection closed\n");
66 fprintf(stdout
, "Read: %s\n", buf
);
70 main (int argc
, char **argv
)
76 socket
= CreateFile("test.txt", // open File
77 GENERIC_READ
, // open for reading
80 OPEN_EXISTING
, // existing file only
81 FILE_ATTRIBUTE_NORMAL
, // normal file
82 NULL
); // no attr. template
84 if(socket
== INVALID_HANDLE_VALUE
)
89 const char *fifo
= "event.fifo";
92 if (lstat (fifo
, &st
) == 0) {
93 if ((st
.st_mode
& S_IFMT
) == S_IFREG
) {
101 if (mkfifo (fifo
, 0600) == -1) {
106 /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
108 socket
= open (fifo
, O_RDWR
| O_NONBLOCK
, 0);
110 socket
= open (fifo
, O_RDONLY
| O_NONBLOCK
, 0);
118 fprintf(stderr
, "Write data to %s\n", fifo
);
120 /* Initalize the event library */
123 /* Initalize one event */
125 event_set(&evfifo
, (int)socket
, EV_READ
, fifo_read
, &evfifo
);
127 event_set(&evfifo
, socket
, EV_READ
, fifo_read
, &evfifo
);
130 /* Add it to the active events, without a timeout */
131 event_add(&evfifo
, NULL
);