|
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 */ |
|
10 |
|
11 #include <event2/event-config.h> |
|
12 |
|
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> |
|
28 |
|
29 #include <event.h> |
|
30 |
|
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 |
|
40 |
|
41 /* Reschedule this event */ |
|
42 event_add(ev, NULL); |
|
43 |
|
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); |
|
48 |
|
49 /* Check for end of file. */ |
|
50 if (len && dwBytesRead == 0) { |
|
51 fprintf(stderr, "End Of File"); |
|
52 event_del(ev); |
|
53 return; |
|
54 } |
|
55 |
|
56 buf[dwBytesRead] = '\0'; |
|
57 #else |
|
58 len = read(fd, buf, sizeof(buf) - 1); |
|
59 |
|
60 if (len == -1) { |
|
61 perror("read"); |
|
62 return; |
|
63 } else if (len == 0) { |
|
64 fprintf(stderr, "Connection closed\n"); |
|
65 return; |
|
66 } |
|
67 |
|
68 buf[len] = '\0'; |
|
69 #endif |
|
70 fprintf(stdout, "Read: %s\n", buf); |
|
71 } |
|
72 |
|
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 */ |
|
87 |
|
88 if (socket == INVALID_HANDLE_VALUE) |
|
89 return 1; |
|
90 |
|
91 #else |
|
92 struct stat st; |
|
93 const char *fifo = "event.fifo"; |
|
94 int socket; |
|
95 |
|
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 } |
|
103 |
|
104 unlink(fifo); |
|
105 if (mkfifo(fifo, 0600) == -1) { |
|
106 perror("mkfifo"); |
|
107 exit(1); |
|
108 } |
|
109 |
|
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 |
|
116 |
|
117 if (socket == -1) { |
|
118 perror("open"); |
|
119 exit(1); |
|
120 } |
|
121 |
|
122 fprintf(stderr, "Write data to %s\n", fifo); |
|
123 #endif |
|
124 /* Initalize the event library */ |
|
125 event_init(); |
|
126 |
|
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 |
|
133 |
|
134 /* Add it to the active events, without a timeout */ |
|
135 event_add(&evfifo, NULL); |
|
136 |
|
137 event_dispatch(); |
|
138 #ifdef WIN32 |
|
139 CloseHandle(socket); |
|
140 #endif |
|
141 return (0); |
|
142 } |
|
143 |