libevent/sample/event-test.c

146 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. 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>
#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 <event2/event.h>
2002-04-09 15:14:06 +00:00
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;
#ifdef _WIN32
2003-09-25 03:29:37 +00:00
DWORD dwBytesRead;
#endif
2002-04-09 15:14:06 +00:00
fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
fd, event, arg);
#ifdef _WIN32
2003-09-25 03:29:37 +00:00
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");
event_del(ev);
2002-04-09 15:14:06 +00:00
return;
} else if (len == 0) {
fprintf(stderr, "Connection closed\n");
event_del(ev);
2002-04-09 15:14:06 +00:00
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);
/* Reschedule this event */
event_add(ev, NULL);
2002-04-09 15:14:06 +00:00
}
int
main(int argc, char **argv)
2002-04-09 15:14:06 +00:00
{
struct event *evfifo;
struct event_base* base;
#ifdef _WIN32
2003-09-25 03:29:37 +00:00
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
}
2010-03-05 13:00:15 -05:00
socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);
2002-04-09 15:14:06 +00:00
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 */
base = event_base_new();
2002-04-09 15:14:06 +00:00
/* Initalize one event */
#ifdef _WIN32
evfifo = event_new(base, (int)socket, EV_READ, fifo_read,
event_self_cbarg());
2003-09-25 03:29:37 +00:00
#else
evfifo = event_new(base, socket, EV_READ, fifo_read,
event_self_cbarg());
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);
event_base_dispatch(base);
event_base_free(base);
#ifdef _WIN32
2003-09-25 03:29:37 +00:00
CloseHandle(socket);
#else
close(socket);
2003-09-25 03:29:37 +00:00
#endif
2002-04-09 15:14:06 +00:00
return (0);
}