1c43e99fdSEd Maste /*
2c43e99fdSEd Maste * XXX This sample code was once meant to show how to use the basic Libevent
3c43e99fdSEd Maste * interfaces, but it never worked on non-Unix platforms, and some of the
4c43e99fdSEd Maste * interfaces have changed since it was first written. It should probably
5c43e99fdSEd Maste * be removed or replaced with something better.
6c43e99fdSEd Maste *
7c43e99fdSEd Maste * Compile with:
8c43e99fdSEd Maste * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
9c43e99fdSEd Maste */
10c43e99fdSEd Maste
11c43e99fdSEd Maste #include <sys/types.h>
12c43e99fdSEd Maste
13c43e99fdSEd Maste #include <event2/event-config.h>
14c43e99fdSEd Maste
15c43e99fdSEd Maste #include <sys/stat.h>
16c43e99fdSEd Maste #ifndef _WIN32
17c43e99fdSEd Maste #include <sys/queue.h>
18c43e99fdSEd Maste #include <unistd.h>
19c43e99fdSEd Maste #endif
20c43e99fdSEd Maste #include <time.h>
21c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
22c43e99fdSEd Maste #include <sys/time.h>
23c43e99fdSEd Maste #endif
24c43e99fdSEd Maste #include <fcntl.h>
25c43e99fdSEd Maste #include <stdlib.h>
26c43e99fdSEd Maste #include <stdio.h>
27c43e99fdSEd Maste #include <string.h>
28c43e99fdSEd Maste #include <errno.h>
29c43e99fdSEd Maste
30c43e99fdSEd Maste #include <event2/event.h>
31c43e99fdSEd Maste #include <event2/event_struct.h>
32c43e99fdSEd Maste #include <event2/util.h>
33c43e99fdSEd Maste
34c43e99fdSEd Maste #ifdef _WIN32
35c43e99fdSEd Maste #include <winsock2.h>
36c43e99fdSEd Maste #endif
37c43e99fdSEd Maste
38c43e99fdSEd Maste struct timeval lasttime;
39c43e99fdSEd Maste
40c43e99fdSEd Maste int event_is_persistent;
41c43e99fdSEd Maste
42c43e99fdSEd Maste static void
timeout_cb(evutil_socket_t fd,short event,void * arg)43c43e99fdSEd Maste timeout_cb(evutil_socket_t fd, short event, void *arg)
44c43e99fdSEd Maste {
45c43e99fdSEd Maste struct timeval newtime, difference;
46c43e99fdSEd Maste struct event *timeout = arg;
47c43e99fdSEd Maste double elapsed;
48c43e99fdSEd Maste
49c43e99fdSEd Maste evutil_gettimeofday(&newtime, NULL);
50c43e99fdSEd Maste evutil_timersub(&newtime, &lasttime, &difference);
51c43e99fdSEd Maste elapsed = difference.tv_sec +
52c43e99fdSEd Maste (difference.tv_usec / 1.0e6);
53c43e99fdSEd Maste
54c43e99fdSEd Maste printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
55c43e99fdSEd Maste (int)newtime.tv_sec, elapsed);
56c43e99fdSEd Maste lasttime = newtime;
57c43e99fdSEd Maste
58c43e99fdSEd Maste if (! event_is_persistent) {
59c43e99fdSEd Maste struct timeval tv;
60c43e99fdSEd Maste evutil_timerclear(&tv);
61c43e99fdSEd Maste tv.tv_sec = 2;
62c43e99fdSEd Maste event_add(timeout, &tv);
63c43e99fdSEd Maste }
64c43e99fdSEd Maste }
65c43e99fdSEd Maste
66c43e99fdSEd Maste int
main(int argc,char ** argv)67c43e99fdSEd Maste main(int argc, char **argv)
68c43e99fdSEd Maste {
69c43e99fdSEd Maste struct event timeout;
70c43e99fdSEd Maste struct timeval tv;
71c43e99fdSEd Maste struct event_base *base;
72c43e99fdSEd Maste int flags;
73c43e99fdSEd Maste
74c43e99fdSEd Maste #ifdef _WIN32
75c43e99fdSEd Maste WORD wVersionRequested;
76c43e99fdSEd Maste WSADATA wsaData;
77c43e99fdSEd Maste
78c43e99fdSEd Maste wVersionRequested = MAKEWORD(2, 2);
79c43e99fdSEd Maste
80c43e99fdSEd Maste (void)WSAStartup(wVersionRequested, &wsaData);
81c43e99fdSEd Maste #endif
82c43e99fdSEd Maste
83c43e99fdSEd Maste if (argc == 2 && !strcmp(argv[1], "-p")) {
84c43e99fdSEd Maste event_is_persistent = 1;
85c43e99fdSEd Maste flags = EV_PERSIST;
86c43e99fdSEd Maste } else {
87c43e99fdSEd Maste event_is_persistent = 0;
88c43e99fdSEd Maste flags = 0;
89c43e99fdSEd Maste }
90c43e99fdSEd Maste
91*b50261e2SCy Schubert /* Initialize the event library */
92c43e99fdSEd Maste base = event_base_new();
93c43e99fdSEd Maste
94*b50261e2SCy Schubert /* Initialize one event */
95c43e99fdSEd Maste event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
96c43e99fdSEd Maste
97c43e99fdSEd Maste evutil_timerclear(&tv);
98c43e99fdSEd Maste tv.tv_sec = 2;
99c43e99fdSEd Maste event_add(&timeout, &tv);
100c43e99fdSEd Maste
101c43e99fdSEd Maste evutil_gettimeofday(&lasttime, NULL);
102c43e99fdSEd Maste
103*b50261e2SCy Schubert setbuf(stdout, NULL);
104*b50261e2SCy Schubert setbuf(stderr, NULL);
105*b50261e2SCy Schubert
106c43e99fdSEd Maste event_base_dispatch(base);
107c43e99fdSEd Maste
108c43e99fdSEd Maste return (0);
109c43e99fdSEd Maste }
110c43e99fdSEd Maste
111