|
|
| version 1.1, 2013/10/14 10:32:47 | version 1.1.1.3, 2016/11/02 10:35:00 |
|---|---|
| Line 1 | Line 1 |
| #include "first.h" | |
| #include "fdevent.h" | #include "fdevent.h" |
| #include "buffer.h" | #include "buffer.h" |
| #include "log.h" | #include "log.h" |
| Line 67 static int fdevent_libev_event_set(fdevents *ev, int f | Line 69 static int fdevent_libev_event_set(fdevents *ev, int f |
| if (!watcher) { | if (!watcher) { |
| fdn->handler_ctx = watcher = calloc(1, sizeof(ev_io)); | fdn->handler_ctx = watcher = calloc(1, sizeof(ev_io)); |
| assert(watcher); | force_assert(watcher); |
| ev_io_init(watcher, io_watcher_cb, fd, ev_events); | ev_io_init(watcher, io_watcher_cb, fd, ev_events); |
| watcher->data = ev; | watcher->data = ev; |
| Line 87 static void timeout_watcher_cb(struct ev_loop *loop, e | Line 89 static void timeout_watcher_cb(struct ev_loop *loop, e |
| UNUSED(loop); | UNUSED(loop); |
| UNUSED(w); | UNUSED(w); |
| UNUSED(revents); | UNUSED(revents); |
| ev_timer_stop(loop, w); | |
| } | } |
| static ev_timer timeout_watcher; | |
| static int fdevent_libev_poll(fdevents *ev, int timeout_ms) { | static int fdevent_libev_poll(fdevents *ev, int timeout_ms) { |
| union { | timeout_watcher.repeat = (timeout_ms > 0) ? timeout_ms/1000.0 : 0.001; |
| struct ev_watcher w; | |
| struct ev_timer timer; | |
| } timeout_watcher; | |
| if (!timeout_ms) timeout_ms = 1; | ev_timer_again(ev->libev_loop, &timeout_watcher); |
| ev_run(ev->libev_loop, EVRUN_ONCE); | |
| ev_init(&timeout_watcher.w, NULL); | |
| ev_set_cb(&timeout_watcher.timer, timeout_watcher_cb); | |
| timeout_watcher.timer.repeat = ((ev_tstamp) timeout_ms)/1000.0; | |
| assert(timeout_watcher.timer.repeat); | |
| ev_timer_again(ev->libev_loop, &timeout_watcher.timer); | |
| ev_loop(ev->libev_loop, EVLOOP_ONESHOT); | |
| ev_timer_stop(ev->libev_loop, &timeout_watcher.timer); | |
| return 0; | return 0; |
| } | } |
| Line 143 static int fdevent_libev_reset(fdevents *ev) { | Line 132 static int fdevent_libev_reset(fdevents *ev) { |
| } | } |
| int fdevent_libev_init(fdevents *ev) { | int fdevent_libev_init(fdevents *ev) { |
| struct ev_timer * const timer = &timeout_watcher; | |
| ev->type = FDEVENT_HANDLER_LIBEV; | ev->type = FDEVENT_HANDLER_LIBEV; |
| #define SET(x) \ | #define SET(x) \ |
| ev->x = fdevent_libev_##x; | ev->x = fdevent_libev_##x; |
| Line 164 int fdevent_libev_init(fdevents *ev) { | Line 155 int fdevent_libev_init(fdevents *ev) { |
| return -1; | return -1; |
| } | } |
| ev_timer_init(timer, timeout_watcher_cb, 0.0, 1.0); | |
| return 0; | return 0; |
| } | } |