michael@0: /* michael@0: * XXX This sample code was once meant to show how to use the basic Libevent michael@0: * interfaces, but it never worked on non-Unix platforms, and some of the michael@0: * interfaces have changed since it was first written. It should probably michael@0: * be removed or replaced with something better. michael@0: * michael@0: * Compile with: michael@0: * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: static void michael@0: fifo_read(evutil_socket_t fd, short event, void *arg) michael@0: { michael@0: char buf[255]; michael@0: int len; michael@0: struct event *ev = arg; michael@0: #ifdef WIN32 michael@0: DWORD dwBytesRead; michael@0: #endif michael@0: michael@0: /* Reschedule this event */ michael@0: event_add(ev, NULL); michael@0: michael@0: fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n", michael@0: (int)fd, event, arg); michael@0: #ifdef WIN32 michael@0: len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL); michael@0: michael@0: /* Check for end of file. */ michael@0: if (len && dwBytesRead == 0) { michael@0: fprintf(stderr, "End Of File"); michael@0: event_del(ev); michael@0: return; michael@0: } michael@0: michael@0: buf[dwBytesRead] = '\0'; michael@0: #else michael@0: len = read(fd, buf, sizeof(buf) - 1); michael@0: michael@0: if (len == -1) { michael@0: perror("read"); michael@0: return; michael@0: } else if (len == 0) { michael@0: fprintf(stderr, "Connection closed\n"); michael@0: return; michael@0: } michael@0: michael@0: buf[len] = '\0'; michael@0: #endif michael@0: fprintf(stdout, "Read: %s\n", buf); michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: struct event evfifo; michael@0: #ifdef WIN32 michael@0: HANDLE socket; michael@0: /* Open a file. */ michael@0: socket = CreateFileA("test.txt", /* open File */ michael@0: GENERIC_READ, /* open for reading */ michael@0: 0, /* do not share */ michael@0: NULL, /* no security */ michael@0: OPEN_EXISTING, /* existing file only */ michael@0: FILE_ATTRIBUTE_NORMAL, /* normal file */ michael@0: NULL); /* no attr. template */ michael@0: michael@0: if (socket == INVALID_HANDLE_VALUE) michael@0: return 1; michael@0: michael@0: #else michael@0: struct stat st; michael@0: const char *fifo = "event.fifo"; michael@0: int socket; michael@0: michael@0: if (lstat(fifo, &st) == 0) { michael@0: if ((st.st_mode & S_IFMT) == S_IFREG) { michael@0: errno = EEXIST; michael@0: perror("lstat"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: unlink(fifo); michael@0: if (mkfifo(fifo, 0600) == -1) { michael@0: perror("mkfifo"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */ michael@0: #ifdef __linux michael@0: socket = open(fifo, O_RDWR | O_NONBLOCK, 0); michael@0: #else michael@0: socket = open(fifo, O_RDONLY | O_NONBLOCK, 0); michael@0: #endif michael@0: michael@0: if (socket == -1) { michael@0: perror("open"); michael@0: exit(1); michael@0: } michael@0: michael@0: fprintf(stderr, "Write data to %s\n", fifo); michael@0: #endif michael@0: /* Initalize the event library */ michael@0: event_init(); michael@0: michael@0: /* Initalize one event */ michael@0: #ifdef WIN32 michael@0: event_set(&evfifo, (evutil_socket_t)socket, EV_READ, fifo_read, &evfifo); michael@0: #else michael@0: event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo); michael@0: #endif michael@0: michael@0: /* Add it to the active events, without a timeout */ michael@0: event_add(&evfifo, NULL); michael@0: michael@0: event_dispatch(); michael@0: #ifdef WIN32 michael@0: CloseHandle(socket); michael@0: #endif michael@0: return (0); michael@0: } michael@0: