ipc/chromium/src/third_party/libevent/sample/event-test.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  * XXX This sample code was once meant to show how to use the basic Libevent
     3  * interfaces, but it never worked on non-Unix platforms, and some of the
     4  * interfaces have changed since it was first written.  It should probably
     5  * be removed or replaced with something better.
     6  *
     7  * Compile with:
     8  * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
     9  */
    11 #include <event2/event-config.h>
    13 #include <sys/types.h>
    14 #include <sys/stat.h>
    15 #ifndef WIN32
    16 #include <sys/queue.h>
    17 #include <unistd.h>
    18 #include <sys/time.h>
    19 #else
    20 #include <winsock2.h>
    21 #include <windows.h>
    22 #endif
    23 #include <fcntl.h>
    24 #include <stdlib.h>
    25 #include <stdio.h>
    26 #include <string.h>
    27 #include <errno.h>
    29 #include <event.h>
    31 static void
    32 fifo_read(evutil_socket_t fd, short event, void *arg)
    33 {
    34 	char buf[255];
    35 	int len;
    36 	struct event *ev = arg;
    37 #ifdef WIN32
    38 	DWORD dwBytesRead;
    39 #endif
    41 	/* Reschedule this event */
    42 	event_add(ev, NULL);
    44 	fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
    45 	    (int)fd, event, arg);
    46 #ifdef WIN32
    47 	len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
    49 	/* Check for end of file. */
    50 	if (len && dwBytesRead == 0) {
    51 		fprintf(stderr, "End Of File");
    52 		event_del(ev);
    53 		return;
    54 	}
    56 	buf[dwBytesRead] = '\0';
    57 #else
    58 	len = read(fd, buf, sizeof(buf) - 1);
    60 	if (len == -1) {
    61 		perror("read");
    62 		return;
    63 	} else if (len == 0) {
    64 		fprintf(stderr, "Connection closed\n");
    65 		return;
    66 	}
    68 	buf[len] = '\0';
    69 #endif
    70 	fprintf(stdout, "Read: %s\n", buf);
    71 }
    73 int
    74 main(int argc, char **argv)
    75 {
    76 	struct event evfifo;
    77 #ifdef WIN32
    78 	HANDLE socket;
    79 	/* Open a file. */
    80 	socket = CreateFileA("test.txt",	/* open File */
    81 			GENERIC_READ,		/* open for reading */
    82 			0,			/* do not share */
    83 			NULL,			/* no security */
    84 			OPEN_EXISTING,		/* existing file only */
    85 			FILE_ATTRIBUTE_NORMAL,	/* normal file */
    86 			NULL);			/* no attr. template */
    88 	if (socket == INVALID_HANDLE_VALUE)
    89 		return 1;
    91 #else
    92 	struct stat st;
    93 	const char *fifo = "event.fifo";
    94 	int socket;
    96 	if (lstat(fifo, &st) == 0) {
    97 		if ((st.st_mode & S_IFMT) == S_IFREG) {
    98 			errno = EEXIST;
    99 			perror("lstat");
   100 			exit(1);
   101 		}
   102 	}
   104 	unlink(fifo);
   105 	if (mkfifo(fifo, 0600) == -1) {
   106 		perror("mkfifo");
   107 		exit(1);
   108 	}
   110 	/* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
   111 #ifdef __linux
   112 	socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
   113 #else
   114 	socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);
   115 #endif
   117 	if (socket == -1) {
   118 		perror("open");
   119 		exit(1);
   120 	}
   122 	fprintf(stderr, "Write data to %s\n", fifo);
   123 #endif
   124 	/* Initalize the event library */
   125 	event_init();
   127 	/* Initalize one event */
   128 #ifdef WIN32
   129 	event_set(&evfifo, (evutil_socket_t)socket, EV_READ, fifo_read, &evfifo);
   130 #else
   131 	event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
   132 #endif
   134 	/* Add it to the active events, without a timeout */
   135 	event_add(&evfifo, NULL);
   137 	event_dispatch();
   138 #ifdef WIN32
   139 	CloseHandle(socket);
   140 #endif
   141 	return (0);
   142 }

mercurial