Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libevent / sample / time-test.c
blob61b4a6c1d9aa59c36aed65148cb410d2d7cc7c6c
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.
7 * Compile with:
8 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
9 */
11 #include <sys/types.h>
13 #include <event2/event-config.h>
15 #include <sys/stat.h>
16 #ifndef WIN32
17 #include <sys/queue.h>
18 #include <unistd.h>
19 #endif
20 #include <time.h>
21 #ifdef _EVENT_HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
30 #include <event2/event.h>
31 #include <event2/event_struct.h>
32 #include <event2/util.h>
34 #ifdef WIN32
35 #include <winsock2.h>
36 #endif
38 struct timeval lasttime;
40 int event_is_persistent;
42 static void
43 timeout_cb(evutil_socket_t fd, short event, void *arg)
45 struct timeval newtime, difference;
46 struct event *timeout = arg;
47 double elapsed;
49 evutil_gettimeofday(&newtime, NULL);
50 evutil_timersub(&newtime, &lasttime, &difference);
51 elapsed = difference.tv_sec +
52 (difference.tv_usec / 1.0e6);
54 printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
55 (int)newtime.tv_sec, elapsed);
56 lasttime = newtime;
58 if (! event_is_persistent) {
59 struct timeval tv;
60 evutil_timerclear(&tv);
61 tv.tv_sec = 2;
62 event_add(timeout, &tv);
66 int
67 main(int argc, char **argv)
69 struct event timeout;
70 struct timeval tv;
71 struct event_base *base;
72 int flags;
74 #ifdef WIN32
75 WORD wVersionRequested;
76 WSADATA wsaData;
77 int err;
79 wVersionRequested = MAKEWORD(2, 2);
81 err = WSAStartup(wVersionRequested, &wsaData);
82 #endif
84 if (argc == 2 && !strcmp(argv[1], "-p")) {
85 event_is_persistent = 1;
86 flags = EV_PERSIST;
87 } else {
88 event_is_persistent = 0;
89 flags = 0;
92 /* Initalize the event library */
93 base = event_base_new();
95 /* Initalize one event */
96 event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
98 evutil_timerclear(&tv);
99 tv.tv_sec = 2;
100 event_add(&timeout, &tv);
102 evutil_gettimeofday(&lasttime, NULL);
104 event_base_dispatch(base);
106 return (0);