MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-eof.c
1 /*
2  * Compile with:
3  * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4  */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8 
9 
10 #ifdef WIN32
11 #include <winsock2.h>
12 #endif
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #ifdef HAVE_SYS_SOCKET_H
17 #include <sys/socket.h>
18 #endif
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 
26 #include <event.h>
27 #include <evutil.h>
28 
29 int test_okay = 1;
30 int called = 0;
31 
32 static void
33 read_cb(int fd, short event, void *arg)
34 {
35  char buf[256];
36  int len;
37 
38  len = read(fd, buf, sizeof(buf));
39 
40  printf("%s: read %d%s\n", __func__,
41  len, len ? "" : " - means EOF");
42 
43  if (len) {
44  if (!called)
45  event_add(arg, NULL);
46  } else if (called == 1)
47  test_okay = 0;
48 
49  called++;
50 }
51 
52 #ifndef SHUT_WR
53 #define SHUT_WR 1
54 #endif
55 
56 int
57 main (int argc, char **argv)
58 {
59  struct event ev;
60  const char *test = "test string";
61  int pair[2];
62 
63  if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
64  return (1);
65 
66 
67  write(pair[0], test, strlen(test)+1);
68  shutdown(pair[0], SHUT_WR);
69 
70  /* Initalize the event library */
71  event_init();
72 
73  /* Initalize one event */
74  event_set(&ev, pair[1], EV_READ, read_cb, &ev);
75 
76  event_add(&ev, NULL);
77 
78  event_dispatch();
79 
80  return (test_okay);
81 }
82