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