michael@0: /* michael@0: * XXX This sample code was once meant to show how to use the basic Libevent michael@0: * interfaces, but it never worked on non-Unix platforms, and some of the michael@0: * interfaces have changed since it was first written. It should probably michael@0: * be removed or replaced with something better. michael@0: * michael@0: * Compile with: michael@0: * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #endif michael@0: michael@0: struct timeval lasttime; michael@0: michael@0: int event_is_persistent; michael@0: michael@0: static void michael@0: timeout_cb(evutil_socket_t fd, short event, void *arg) michael@0: { michael@0: struct timeval newtime, difference; michael@0: struct event *timeout = arg; michael@0: double elapsed; michael@0: michael@0: evutil_gettimeofday(&newtime, NULL); michael@0: evutil_timersub(&newtime, &lasttime, &difference); michael@0: elapsed = difference.tv_sec + michael@0: (difference.tv_usec / 1.0e6); michael@0: michael@0: printf("timeout_cb called at %d: %.3f seconds elapsed.\n", michael@0: (int)newtime.tv_sec, elapsed); michael@0: lasttime = newtime; michael@0: michael@0: if (! event_is_persistent) { michael@0: struct timeval tv; michael@0: evutil_timerclear(&tv); michael@0: tv.tv_sec = 2; michael@0: event_add(timeout, &tv); michael@0: } michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: struct event timeout; michael@0: struct timeval tv; michael@0: struct event_base *base; michael@0: int flags; michael@0: michael@0: #ifdef WIN32 michael@0: WORD wVersionRequested; michael@0: WSADATA wsaData; michael@0: michael@0: wVersionRequested = MAKEWORD(2, 2); michael@0: michael@0: (void)WSAStartup(wVersionRequested, &wsaData); michael@0: #endif michael@0: michael@0: if (argc == 2 && !strcmp(argv[1], "-p")) { michael@0: event_is_persistent = 1; michael@0: flags = EV_PERSIST; michael@0: } else { michael@0: event_is_persistent = 0; michael@0: flags = 0; michael@0: } michael@0: michael@0: /* Initalize the event library */ michael@0: base = event_base_new(); michael@0: michael@0: /* Initalize one event */ michael@0: event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout); michael@0: michael@0: evutil_timerclear(&tv); michael@0: tv.tv_sec = 2; michael@0: event_add(&timeout, &tv); michael@0: michael@0: evutil_gettimeofday(&lasttime, NULL); michael@0: michael@0: event_base_dispatch(base); michael@0: michael@0: return (0); michael@0: } michael@0: