2002-04-09 15:14:06 +00:00
|
|
|
/*
|
2010-01-27 01:47:36 -05:00
|
|
|
* XXX This sample code was once meant to show how to use the basic Libevent
|
|
|
|
* interfaces, but it never worked on non-Unix platforms, and some of the
|
|
|
|
* interfaces have changed since it was first written. It should probably
|
|
|
|
* be removed or replaced with something better.
|
|
|
|
*
|
2002-04-09 15:14:06 +00:00
|
|
|
* Compile with:
|
|
|
|
* cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2004-05-24 00:19:52 +00:00
|
|
|
|
2010-07-07 16:45:03 -04:00
|
|
|
#include <event2/event-config.h>
|
2007-03-01 06:25:18 +00:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/stat.h>
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifndef _WIN32
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/queue.h>
|
2003-09-25 03:29:37 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2007-08-10 15:59:31 +00:00
|
|
|
#include <time.h>
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_SYS_TIME_H
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/time.h>
|
2004-05-24 00:19:52 +00:00
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/event_struct.h>
|
|
|
|
#include <event2/util.h>
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2009-05-22 20:11:29 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
2009-05-22 19:11:59 +00:00
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
struct timeval lasttime;
|
|
|
|
|
|
|
|
int event_is_persistent;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-11-26 19:18:49 +00:00
|
|
|
static void
|
2010-01-27 01:47:36 -05:00
|
|
|
timeout_cb(evutil_socket_t fd, short event, void *arg)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2010-01-27 01:47:36 -05:00
|
|
|
struct timeval newtime, difference;
|
2002-04-09 15:14:06 +00:00
|
|
|
struct event *timeout = arg;
|
2010-01-27 01:47:36 -05:00
|
|
|
double elapsed;
|
|
|
|
|
|
|
|
evutil_gettimeofday(&newtime, NULL);
|
|
|
|
evutil_timersub(&newtime, &lasttime, &difference);
|
|
|
|
elapsed = difference.tv_sec +
|
|
|
|
(difference.tv_usec / 1.0e6);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
|
|
|
|
(int)newtime.tv_sec, elapsed);
|
2002-04-09 15:14:06 +00:00
|
|
|
lasttime = newtime;
|
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
if (! event_is_persistent) {
|
|
|
|
struct timeval tv;
|
|
|
|
evutil_timerclear(&tv);
|
|
|
|
tv.tv_sec = 2;
|
|
|
|
event_add(timeout, &tv);
|
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-02-19 03:39:50 -05:00
|
|
|
main(int argc, char **argv)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
|
|
|
struct event timeout;
|
|
|
|
struct timeval tv;
|
2010-01-27 01:47:36 -05:00
|
|
|
struct event_base *base;
|
|
|
|
int flags;
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2009-05-22 19:11:59 +00:00
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
|
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
#endif
|
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "-p")) {
|
|
|
|
event_is_persistent = 1;
|
|
|
|
flags = EV_PERSIST;
|
|
|
|
} else {
|
|
|
|
event_is_persistent = 0;
|
|
|
|
flags = 0;
|
|
|
|
}
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
/* Initalize the event library */
|
2010-01-27 01:47:36 -05:00
|
|
|
base = event_base_new();
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
/* Initalize one event */
|
2010-01-27 01:47:36 -05:00
|
|
|
event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timerclear(&tv);
|
2002-04-09 15:14:06 +00:00
|
|
|
tv.tv_sec = 2;
|
|
|
|
event_add(&timeout, &tv);
|
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
evutil_gettimeofday(&lasttime, NULL);
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
event_base_dispatch(base);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|