mirror of
https://github.com/libevent/libevent.git
synced 2025-01-09 00:56:20 +08:00
Update time-test.c to use event2
time-test.c wasn't crazy, but it used some old interfaces. There are probably more cleanups and explanations to do beyond the ones here.
This commit is contained in:
parent
d60a1bd50c
commit
f4190bfb85
@ -1,4 +1,9 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* Compile with:
|
||||
* cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
|
||||
*/
|
||||
@ -22,32 +27,40 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <event.h>
|
||||
#include <evutil.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/event_struct.h>
|
||||
#include <event2/util.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
#ifdef _EVENT___func__
|
||||
#define __func__ _EVENT___func__
|
||||
#endif
|
||||
|
||||
int lasttime;
|
||||
struct timeval lasttime;
|
||||
|
||||
int event_is_persistent;
|
||||
|
||||
static void
|
||||
timeout_cb(int fd, short event, void *arg)
|
||||
timeout_cb(evutil_socket_t fd, short event, void *arg)
|
||||
{
|
||||
struct timeval tv;
|
||||
struct timeval newtime, difference;
|
||||
struct event *timeout = arg;
|
||||
int newtime = time(NULL);
|
||||
double elapsed;
|
||||
|
||||
printf("%s: called at %d: %d\n", __func__, newtime,
|
||||
newtime - lasttime);
|
||||
evutil_gettimeofday(&newtime, NULL);
|
||||
evutil_timersub(&newtime, &lasttime, &difference);
|
||||
elapsed = difference.tv_sec +
|
||||
(difference.tv_usec / 1.0e6);
|
||||
|
||||
printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
|
||||
(int)newtime.tv_sec, elapsed);
|
||||
lasttime = newtime;
|
||||
|
||||
evutil_timerclear(&tv);
|
||||
tv.tv_sec = 2;
|
||||
event_add(timeout, &tv);
|
||||
if (! event_is_persistent) {
|
||||
struct timeval tv;
|
||||
evutil_timerclear(&tv);
|
||||
tv.tv_sec = 2;
|
||||
event_add(timeout, &tv);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
@ -55,6 +68,8 @@ main (int argc, char **argv)
|
||||
{
|
||||
struct event timeout;
|
||||
struct timeval tv;
|
||||
struct event_base *base;
|
||||
int flags;
|
||||
|
||||
#ifdef WIN32
|
||||
WORD wVersionRequested;
|
||||
@ -66,19 +81,27 @@ main (int argc, char **argv)
|
||||
err = WSAStartup(wVersionRequested, &wsaData);
|
||||
#endif
|
||||
|
||||
if (argc == 2 && !strcmp(argv[1], "-p")) {
|
||||
event_is_persistent = 1;
|
||||
flags = EV_PERSIST;
|
||||
} else {
|
||||
event_is_persistent = 0;
|
||||
flags = 0;
|
||||
}
|
||||
|
||||
/* Initalize the event library */
|
||||
event_init();
|
||||
base = event_base_new();
|
||||
|
||||
/* Initalize one event */
|
||||
evtimer_set(&timeout, timeout_cb, &timeout);
|
||||
event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
|
||||
|
||||
evutil_timerclear(&tv);
|
||||
tv.tv_sec = 2;
|
||||
event_add(&timeout, &tv);
|
||||
|
||||
lasttime = time(NULL);
|
||||
evutil_gettimeofday(&lasttime, NULL);
|
||||
|
||||
event_dispatch();
|
||||
event_base_dispatch(base);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user