Fixed timeout issues

This commit is contained in:
Łukasz Magiera 2016-02-26 23:19:03 +01:00
parent 6b5bbba8bd
commit 170ce567ce

View File

@ -40,7 +40,11 @@ static void handleStdin(evutil_socket_t fd, short what, void *ptr) {
}
void event_prepare() {
base = event_base_new();
struct event_config* cfg = event_config_new();
event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
base = event_base_new_with_config(cfg);
evutil_make_socket_nonblocking(STDIN_FILENO);
event_assign(&stdinEvent, base, STDIN_FILENO, EV_READ, handleStdin, NULL);
}
@ -48,18 +52,17 @@ static void add_events(struct timeval* timeout) {
event_add(&stdinEvent, timeout);
}
int event_pull(int _timeout) {
if(_timeout > 0) { /* wait max this much time for event */
struct timeval timeout = {_timeout / 1000, (_timeout % 1000) * 1000000};
struct timeval timeout = {_timeout / 1000, (_timeout % 1000) * 1000};
add_events(&timeout);
/* event_base_loopexit(base, &timeout); */
event_base_loop(base, EVLOOP_ONCE);
} else if(_timeout == 0) { /* Get event without blocking */
add_events(NULL);
event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
} else { /* wait for event to appear */
add_events(NULL);
event_base_loopexit(base, NULL);
event_base_loop(base, EVLOOP_ONCE);
}