mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-11 13:24:43 -04: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:
|
* Compile with:
|
||||||
* cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
|
* cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
|
||||||
*/
|
*/
|
||||||
@ -22,32 +27,40 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <event.h>
|
#include <event2/event.h>
|
||||||
#include <evutil.h>
|
#include <event2/event_struct.h>
|
||||||
|
#include <event2/util.h>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef _EVENT___func__
|
|
||||||
#define __func__ _EVENT___func__
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int lasttime;
|
struct timeval lasttime;
|
||||||
|
|
||||||
|
int event_is_persistent;
|
||||||
|
|
||||||
static void
|
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;
|
struct event *timeout = arg;
|
||||||
int newtime = time(NULL);
|
double elapsed;
|
||||||
|
|
||||||
printf("%s: called at %d: %d\n", __func__, newtime,
|
evutil_gettimeofday(&newtime, NULL);
|
||||||
newtime - lasttime);
|
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;
|
lasttime = newtime;
|
||||||
|
|
||||||
evutil_timerclear(&tv);
|
if (! event_is_persistent) {
|
||||||
tv.tv_sec = 2;
|
struct timeval tv;
|
||||||
event_add(timeout, &tv);
|
evutil_timerclear(&tv);
|
||||||
|
tv.tv_sec = 2;
|
||||||
|
event_add(timeout, &tv);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -55,6 +68,8 @@ main (int argc, char **argv)
|
|||||||
{
|
{
|
||||||
struct event timeout;
|
struct event timeout;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
struct event_base *base;
|
||||||
|
int flags;
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WORD wVersionRequested;
|
WORD wVersionRequested;
|
||||||
@ -66,19 +81,27 @@ main (int argc, char **argv)
|
|||||||
err = WSAStartup(wVersionRequested, &wsaData);
|
err = WSAStartup(wVersionRequested, &wsaData);
|
||||||
#endif
|
#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 */
|
/* Initalize the event library */
|
||||||
event_init();
|
base = event_base_new();
|
||||||
|
|
||||||
/* Initalize one event */
|
/* Initalize one event */
|
||||||
evtimer_set(&timeout, timeout_cb, &timeout);
|
event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
|
||||||
|
|
||||||
evutil_timerclear(&tv);
|
evutil_timerclear(&tv);
|
||||||
tv.tv_sec = 2;
|
tv.tv_sec = 2;
|
||||||
event_add(&timeout, &tv);
|
event_add(&timeout, &tv);
|
||||||
|
|
||||||
lasttime = time(NULL);
|
evutil_gettimeofday(&lasttime, NULL);
|
||||||
|
|
||||||
event_dispatch();
|
event_base_dispatch(base);
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user