libevent/sample/event-test.c

144 lines
2.8 KiB
C
Raw Normal View History

2002-04-09 15:14:06 +00:00
/*
* 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 have changed since it was first written. It should probably
* be removed or replaced with something better.
*
2002-04-09 15:14:06 +00:00
* Compile with:
* cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
*/
#include <event2/event-config.h>
2007-06-14 04:38:42 +00:00
2002-04-09 15:14:06 +00:00
#include <sys/types.h>
#include <sys/stat.h>
2003-09-25 03:29:37 +00:00
#ifndef WIN32
2002-04-09 15:14:06 +00:00
#include <sys/queue.h>
2003-09-25 03:29:37 +00:00
#include <unistd.h>
2002-04-09 15:14:06 +00:00
#include <sys/time.h>
2003-09-25 03:29:37 +00:00
#else
#include <winsock2.h>
2003-09-25 03:29:37 +00:00
#include <windows.h>
#endif
2002-04-09 15:14:06 +00:00
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event.h>
static void
2002-04-09 15:14:06 +00:00
fifo_read(int fd, short event, void *arg)
{
char buf[255];
int len;
struct event *ev = arg;
2003-09-25 03:29:37 +00:00
#ifdef WIN32
DWORD dwBytesRead;
#endif
2002-04-09 15:14:06 +00:00
/* Reschedule this event */
event_add(ev, NULL);
fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
fd, event, arg);
2003-09-25 03:29:37 +00:00
#ifdef WIN32
len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
2002-04-09 15:14:06 +00:00
/* Check for end of file. */
if (len && dwBytesRead == 0) {
2003-09-25 03:29:37 +00:00
fprintf(stderr, "End Of File");
event_del(ev);
return;
}
buf[dwBytesRead] = '\0';
2003-09-25 03:29:37 +00:00
#else
2002-04-09 15:14:06 +00:00
len = read(fd, buf, sizeof(buf) - 1);
2003-09-25 03:29:37 +00:00
2002-04-09 15:14:06 +00:00
if (len == -1) {
perror("read");
return;
} else if (len == 0) {
fprintf(stderr, "Connection closed\n");
return;
}
buf[len] = '\0';
2003-09-25 03:29:37 +00:00
#endif
2002-04-09 15:14:06 +00:00
fprintf(stdout, "Read: %s\n", buf);
}
int
main(int argc, char **argv)
2002-04-09 15:14:06 +00:00
{
2003-09-25 03:29:37 +00:00
struct event evfifo;
#ifdef WIN32
HANDLE socket;
/* Open a file. */
socket = CreateFileA("test.txt", /* open File */
GENERIC_READ, /* open for reading */
0, /* do not share */
NULL, /* no security */
OPEN_EXISTING, /* existing file only */
FILE_ATTRIBUTE_NORMAL, /* normal file */
NULL); /* no attr. template */
2003-09-25 03:29:37 +00:00
if (socket == INVALID_HANDLE_VALUE)
2003-09-25 03:29:37 +00:00
return 1;
#else
2002-04-09 15:14:06 +00:00
struct stat st;
const char *fifo = "event.fifo";
2002-04-09 15:14:06 +00:00
int socket;
2010-03-05 13:00:15 -05:00
if (lstat(fifo, &st) == 0) {
2002-04-09 15:14:06 +00:00
if ((st.st_mode & S_IFMT) == S_IFREG) {
errno = EEXIST;
perror("lstat");
2010-03-05 13:00:15 -05:00
exit(1);
2002-04-09 15:14:06 +00:00
}
}
2010-03-05 13:00:15 -05:00
unlink(fifo);
if (mkfifo(fifo, 0600) == -1) {
2002-04-09 15:14:06 +00:00
perror("mkfifo");
2010-03-05 13:00:15 -05:00
exit(1);
2002-04-09 15:14:06 +00:00
}
/* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
#ifdef __linux
2010-03-05 13:00:15 -05:00
socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
2002-04-09 15:14:06 +00:00
#else
2010-03-05 13:00:15 -05:00
socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);
2002-04-09 15:14:06 +00:00
#endif
if (socket == -1) {
perror("open");
2010-03-05 13:00:15 -05:00
exit(1);
2002-04-09 15:14:06 +00:00
}
fprintf(stderr, "Write data to %s\n", fifo);
2003-09-25 03:29:37 +00:00
#endif
2002-04-09 15:14:06 +00:00
/* Initalize the event library */
event_init();
/* Initalize one event */
2003-09-25 03:29:37 +00:00
#ifdef WIN32
event_set(&evfifo, (int)socket, EV_READ, fifo_read, &evfifo);
#else
2002-04-09 15:14:06 +00:00
event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
2003-09-25 03:29:37 +00:00
#endif
2002-04-09 15:14:06 +00:00
/* Add it to the active events, without a timeout */
event_add(&evfifo, NULL);
2002-04-09 15:14:06 +00:00
event_dispatch();
2003-09-25 03:29:37 +00:00
#ifdef WIN32
CloseHandle(socket);
#endif
2002-04-09 15:14:06 +00:00
return (0);
}