Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1481)

Side by Side Diff: third_party/libevent/sample/event-test.c

Issue 1531573008: move libevent into base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix shim path Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libevent/sample/Makefile.am ('k') | third_party/libevent/sample/signal-test.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Compile with:
3 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
4 */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #ifndef WIN32
13 #include <sys/queue.h>
14 #include <unistd.h>
15 #include <sys/time.h>
16 #else
17 #include <windows.h>
18 #endif
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <event.h>
26
27 static void
28 fifo_read(int fd, short event, void *arg)
29 {
30 char buf[255];
31 int len;
32 struct event *ev = arg;
33 #ifdef WIN32
34 DWORD dwBytesRead;
35 #endif
36
37 /* Reschedule this event */
38 event_add(ev, NULL);
39
40 fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
41 fd, event, arg);
42 #ifdef WIN32
43 len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
44
45 // Check for end of file.
46 if(len && dwBytesRead == 0) {
47 fprintf(stderr, "End Of File");
48 event_del(ev);
49 return;
50 }
51
52 buf[dwBytesRead] = '\0';
53 #else
54 len = read(fd, buf, sizeof(buf) - 1);
55
56 if (len == -1) {
57 perror("read");
58 return;
59 } else if (len == 0) {
60 fprintf(stderr, "Connection closed\n");
61 return;
62 }
63
64 buf[len] = '\0';
65 #endif
66 fprintf(stdout, "Read: %s\n", buf);
67 }
68
69 int
70 main (int argc, char **argv)
71 {
72 struct event evfifo;
73 #ifdef WIN32
74 HANDLE socket;
75 // Open a file.
76 socket = CreateFileA("test.txt", // open File
77 GENERIC_READ, // open for reading
78 0, // do not share
79 NULL, // no security
80 OPEN_EXISTING, // existing file only
81 FILE_ATTRIBUTE_NORMAL, // normal file
82 NULL); // no attr. template
83
84 if(socket == INVALID_HANDLE_VALUE)
85 return 1;
86
87 #else
88 struct stat st;
89 const char *fifo = "event.fifo";
90 int socket;
91
92 if (lstat (fifo, &st) == 0) {
93 if ((st.st_mode & S_IFMT) == S_IFREG) {
94 errno = EEXIST;
95 perror("lstat");
96 exit (1);
97 }
98 }
99
100 unlink (fifo);
101 if (mkfifo (fifo, 0600) == -1) {
102 perror("mkfifo");
103 exit (1);
104 }
105
106 /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
107 #ifdef __linux
108 socket = open (fifo, O_RDWR | O_NONBLOCK, 0);
109 #else
110 socket = open (fifo, O_RDONLY | O_NONBLOCK, 0);
111 #endif
112
113 if (socket == -1) {
114 perror("open");
115 exit (1);
116 }
117
118 fprintf(stderr, "Write data to %s\n", fifo);
119 #endif
120 /* Initalize the event library */
121 event_init();
122
123 /* Initalize one event */
124 #ifdef WIN32
125 event_set(&evfifo, (int)socket, EV_READ, fifo_read, &evfifo);
126 #else
127 event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
128 #endif
129
130 /* Add it to the active events, without a timeout */
131 event_add(&evfifo, NULL);
132
133 event_dispatch();
134 #ifdef WIN32
135 CloseHandle(socket);
136 #endif
137 return (0);
138 }
139
OLDNEW
« no previous file with comments | « third_party/libevent/sample/Makefile.am ('k') | third_party/libevent/sample/signal-test.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698