transmission: update from 2.13 to 2.22
[tomato.git] / release / src / router / libevent / test / test-eof.c
blob90b40938a62afbc875c695a37f168b6d6854fb37
1 /*
2 * Compile with:
3 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4 */
5 #include "event2/event-config.h"
7 #ifdef WIN32
8 #include <winsock2.h>
9 #else
10 #include <unistd.h>
11 #endif
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #ifdef _EVENT_HAVE_SYS_TIME_H
15 #include <sys/time.h>
16 #endif
17 #ifdef _EVENT_HAVE_SYS_SOCKET_H
18 #include <sys/socket.h>
19 #endif
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
26 #include <event.h>
27 #include <evutil.h>
29 #ifdef _EVENT___func__
30 #define __func__ _EVENT___func__
31 #endif
33 int test_okay = 1;
34 int called = 0;
36 static void
37 read_cb(evutil_socket_t fd, short event, void *arg)
39 char buf[256];
40 int len;
42 len = recv(fd, buf, sizeof(buf), 0);
44 printf("%s: read %d%s\n", __func__,
45 len, len ? "" : " - means EOF");
47 if (len) {
48 if (!called)
49 event_add(arg, NULL);
50 } else if (called == 1)
51 test_okay = 0;
53 called++;
56 #ifndef SHUT_WR
57 #define SHUT_WR 1
58 #endif
60 int
61 main(int argc, char **argv)
63 struct event ev;
64 const char *test = "test string";
65 evutil_socket_t pair[2];
67 #ifdef WIN32
68 WORD wVersionRequested;
69 WSADATA wsaData;
70 int err;
72 wVersionRequested = MAKEWORD(2, 2);
74 err = WSAStartup(wVersionRequested, &wsaData);
75 #endif
77 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
78 return (1);
81 send(pair[0], test, (int)strlen(test)+1, 0);
82 shutdown(pair[0], SHUT_WR);
84 /* Initalize the event library */
85 event_init();
87 /* Initalize one event */
88 event_set(&ev, pair[1], EV_READ, read_cb, &ev);
90 event_add(&ev, NULL);
92 event_dispatch();
94 return (test_okay);