aboutsummaryrefslogtreecommitdiffstats
path: root/src/event_callback.c
diff options
context:
space:
mode:
authorThomas Harning Jr <harningt@gmail.com>2007-09-05 23:33:46 -0400
committerThomas Harning Jr <harningt@gmail.com>2007-09-05 23:33:46 -0400
commitfb5f78f8c75ce907ae2796d4300c7f8053d20732 (patch)
treeb4ba07932f35e29e431af481165812efab48769b /src/event_callback.c
parent3bf12e614c65f22f012b35a19eafad28ad3d4ed5 (diff)
downloadluaevent-prosody-fb5f78f8c75ce907ae2796d4300c7f8053d20732.tar.gz
luaevent-prosody-fb5f78f8c75ce907ae2796d4300c7f8053d20732.zip
Added support for timeouts and timers.
Diffstat (limited to 'src/event_callback.c')
-rw-r--r--src/event_callback.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/event_callback.c b/src/event_callback.c
index f2cd0bc..4a6c04d 100644
--- a/src/event_callback.c
+++ b/src/event_callback.c
@@ -18,25 +18,39 @@ is used, no need to manually de-allocate */
/* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
void luaevent_callback(int fd, short event, void* p) {
- le_callback* arg = p;
+ le_callback* cb = p;
lua_State* L;
int ret;
- assert(arg && arg->base && arg->base->loop_L);
- L = arg->base->loop_L;
- lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
+ double newTimeout = -1;
+ assert(cb && cb->base && cb->base->loop_L);
+ L = cb->base->loop_L;
+ lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef);
lua_pushinteger(L, event);
- lua_call(L, 1, 1);
- ret = lua_tointeger(L, -1);
+ lua_call(L, 1, 2);
+ ret = lua_tointeger(L, -2);
+ if(lua_isnumber(L, -1)) {
+ newTimeout = lua_tonumber(L, -1);
+ if(newTimeout <= 0) {
+ memset(&cb->timeout, 0, sizeof(arg->timeout));
+ } else {
+ load_timeval(newTimeout, &cb->timeout);
+ }
+ }
lua_pop(L, 1);
if(ret == -1) {
- freeCallbackArgs(arg, L);
+ freeCallbackArgs(cb, L);
} else {
- struct event *ev = &arg->ev;
+ struct event *ev = &cb->ev;
int newEvent = ret;
- if(newEvent != event) { // Need to hook up new event...
+ /* NOTE: Currently, even if new timeout is the same as the old, a new event is setup regardless... */
+ if(newEvent != event || newTimeout != -1) { // Need to hook up new event...
+ struct timeval *ptv = &cb->timeout;
+ if(!cb->timeout.sec && !cb->timeout.usec)
+ ptv = NULL;
event_del(ev);
- event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
- event_add(ev, NULL);
+ event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb);
+ /* Assume cannot set a new timeout.. */
+ event_add(ev, ptv);
}
}
}
@@ -58,6 +72,7 @@ le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) {
lua_pushvalue(L, callbackIdx);
cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
cb->base = base;
+ memset(&cb->timeout, 0, sizeof(cb->timeout));
return cb;
}