Fix up sample/event-test.c to use newer interfaces and make it actually work.

This commit is contained in:
Ross Lagerwall 2012-03-12 18:45:00 +02:00
parent 33e42ef3d3
commit 19bab4fbd0

View File

@ -1,7 +1,6 @@
/* /*
* XXX This sample code was once meant to show how to use the basic Libevent * XXX This sample code was once meant to show how to use the basic Libevent
* interfaces, but it never worked on non-Unix platforms, and some of the * interfaces, but it never worked on non-Unix platforms. It should probably
* interfaces have changed since it was first written. It should probably
* be removed or replaced with something better. * be removed or replaced with something better.
* *
* Compile with: * Compile with:
@ -26,7 +25,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <event.h> #include <event2/event.h>
static void static void
fifo_read(int fd, short event, void *arg) fifo_read(int fd, short event, void *arg)
@ -38,9 +37,6 @@ fifo_read(int fd, short event, void *arg)
DWORD dwBytesRead; DWORD dwBytesRead;
#endif #endif
/* Reschedule this event */
event_add(ev, NULL);
fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n", fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
fd, event, arg); fd, event, arg);
#ifdef _WIN32 #ifdef _WIN32
@ -59,21 +55,27 @@ fifo_read(int fd, short event, void *arg)
if (len == -1) { if (len == -1) {
perror("read"); perror("read");
event_del(ev);
return; return;
} else if (len == 0) { } else if (len == 0) {
fprintf(stderr, "Connection closed\n"); fprintf(stderr, "Connection closed\n");
event_del(ev);
return; return;
} }
buf[len] = '\0'; buf[len] = '\0';
#endif #endif
fprintf(stdout, "Read: %s\n", buf); fprintf(stdout, "Read: %s\n", buf);
/* Reschedule this event */
event_add(ev, NULL);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
struct event evfifo; struct event *evfifo;
struct event_base* base;
#ifdef _WIN32 #ifdef _WIN32
HANDLE socket; HANDLE socket;
/* Open a file. */ /* Open a file. */
@ -107,12 +109,7 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
/* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
#ifdef __linux
socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
#else
socket = open(fifo, O_RDONLY | O_NONBLOCK, 0); socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);
#endif
if (socket == -1) { if (socket == -1) {
perror("open"); perror("open");
@ -122,21 +119,26 @@ main(int argc, char **argv)
fprintf(stderr, "Write data to %s\n", fifo); fprintf(stderr, "Write data to %s\n", fifo);
#endif #endif
/* Initalize the event library */ /* Initalize the event library */
event_init(); base = event_base_new();
/* Initalize one event */ /* Initalize one event */
#ifdef _WIN32 #ifdef _WIN32
event_set(&evfifo, (int)socket, EV_READ, fifo_read, &evfifo); evfifo = event_new(base, (int)socket, EV_READ, fifo_read,
event_self_cbarg());
#else #else
event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo); evfifo = event_new(base, socket, EV_READ, fifo_read,
event_self_cbarg());
#endif #endif
/* Add it to the active events, without a timeout */ /* Add it to the active events, without a timeout */
event_add(&evfifo, NULL); event_add(evfifo, NULL);
event_dispatch(); event_base_dispatch(base);
event_base_free(base);
#ifdef _WIN32 #ifdef _WIN32
CloseHandle(socket); CloseHandle(socket);
#else
close(socket);
#endif #endif
return (0); return (0);
} }