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.
8 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
11 #include <sys/types.h>
13 #include <event2/event-config.h>
17 #include <sys/queue.h>
21 #ifdef _EVENT_HAVE_SYS_TIME_H
30 #include <event2/event.h>
31 #include <event2/event_struct.h>
32 #include <event2/util.h>
38 struct timeval lasttime
;
40 int event_is_persistent
;
43 timeout_cb(evutil_socket_t fd
, short event
, void *arg
)
45 struct timeval newtime
, difference
;
46 struct event
*timeout
= arg
;
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
);
58 if (! event_is_persistent
) {
60 evutil_timerclear(&tv
);
62 event_add(timeout
, &tv
);
67 main(int argc
, char **argv
)
71 struct event_base
*base
;
75 WORD wVersionRequested
;
78 wVersionRequested
= MAKEWORD(2, 2);
80 (void)WSAStartup(wVersionRequested
, &wsaData
);
83 if (argc
== 2 && !strcmp(argv
[1], "-p")) {
84 event_is_persistent
= 1;
87 event_is_persistent
= 0;
91 /* Initalize the event library */
92 base
= event_base_new();
94 /* Initalize one event */
95 event_assign(&timeout
, base
, -1, flags
, timeout_cb
, (void*) &timeout
);
97 evutil_timerclear(&tv
);
99 event_add(&timeout
, &tv
);
101 evutil_gettimeofday(&lasttime
, NULL
);
103 event_base_dispatch(base
);