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>
|
2023-11-26 14:53:23 -06:00
|
|
|
#include <signal.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
|
|
|
}
|
|
|
|
|
2023-11-26 14:53:23 -06:00
|
|
|
static void
|
|
|
|
signal_cb(evutil_socket_t sig, short events, void *user_data)
|
|
|
|
{
|
|
|
|
struct event_base *base = user_data;
|
|
|
|
|
|
|
|
printf("Caught an interrupt signal; exiting cleanly.\n");
|
|
|
|
|
|
|
|
event_base_loopexit(base, NULL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-11-26 14:53:23 -06:00
|
|
|
struct event *timeout,*signal_event;
|
2002-04-09 15:14:06 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
|
2012-11-01 17:38:34 -04:00
|
|
|
(void)WSAStartup(wVersionRequested, &wsaData);
|
2009-05-22 19:11:59 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:19:15 +08:00
|
|
|
/* Initialize the event library */
|
2010-01-27 01:47:36 -05:00
|
|
|
base = event_base_new();
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2019-08-06 18:19:15 +08:00
|
|
|
/* Initialize one event */
|
2023-11-26 14:53:23 -06:00
|
|
|
timeout = event_new(base, -1, flags, timeout_cb, event_self_cbarg());
|
|
|
|
signal_event = evsignal_new(base, SIGINT, signal_cb, (void*)base );
|
2024-04-22 14:47:34 +08:00
|
|
|
if (timeout == NULL) {
|
|
|
|
fprintf(stderr, "Couldn't create event");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (signal_event == NULL) {
|
|
|
|
fprintf(stderr, "Couldn't create event");
|
|
|
|
goto err;
|
|
|
|
}
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timerclear(&tv);
|
2002-04-09 15:14:06 +00:00
|
|
|
tv.tv_sec = 2;
|
2023-11-26 14:53:23 -06:00
|
|
|
event_add(timeout, &tv);
|
|
|
|
event_add(signal_event, NULL);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
evutil_gettimeofday(&lasttime, NULL);
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2019-05-21 10:59:05 +03:00
|
|
|
setbuf(stdout, NULL);
|
|
|
|
setbuf(stderr, NULL);
|
|
|
|
|
2010-01-27 01:47:36 -05:00
|
|
|
event_base_dispatch(base);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2023-11-26 14:53:23 -06:00
|
|
|
event_free(signal_event);
|
|
|
|
event_free(timeout);
|
|
|
|
event_base_free(base);
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
return (0);
|
2024-04-22 14:47:34 +08:00
|
|
|
err:
|
|
|
|
if (signal_event)
|
|
|
|
event_free(signal_event);
|
|
|
|
if (timeout)
|
|
|
|
event_free(timeout);
|
|
|
|
if (base)
|
|
|
|
event_base_free(base);
|
|
|
|
return (1);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|