libevent/sample/event-read-fifo.c

163 lines
3.3 KiB
C
Raw Normal View History

2002-04-09 15:14:06 +00:00
/*
* This sample code shows how to use Libevent to read from a named pipe.
* XXX This code could make better use of the Libevent interfaces.
*
* XXX This does not work on Windows; ignore everything inside the _WIN32 block.
*
* On UNIX, compile with:
* cc -I/usr/local/include -o event-read-fifo event-read-fifo.c \
* -L/usr/local/lib -levent
2002-04-09 15:14:06 +00:00
*/
#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>
#include <signal.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
fifo_read(evutil_socket_t fd, short event, void *arg)
2002-04-09 15:14:06 +00:00
{
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",
(int)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
if (len <= 0) {
if (len == -1)
perror("read");
else if (len == 0)
fprintf(stderr, "Connection closed\n");
event_del(ev);
event_base_loopbreak(event_get_base(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);
}
/* On Unix, cleanup event.fifo if SIGINT is received. */
#ifndef _WIN32
static void
signal_cb(evutil_socket_t fd, short event, void *arg)
{
struct event_base *base = arg;
event_base_loopbreak(base);
}
#endif
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
struct event *signal_int;
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, (evutil_socket_t)socket, EV_READ|EV_PERSIST, fifo_read,
event_self_cbarg());
2003-09-25 03:29:37 +00:00
#else
/* catch SIGINT so that event.fifo can be cleaned up */
signal_int = evsignal_new(base, SIGINT, signal_cb, base);
event_add(signal_int, NULL);
evfifo = event_new(base, socket, EV_READ|EV_PERSIST, 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);
unlink(fifo);
2003-09-25 03:29:37 +00:00
#endif
libevent_global_shutdown();
2002-04-09 15:14:06 +00:00
return (0);
}