1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
2 * Licensed as LGPL - See doc/COPYING for details */ |
2 * Licensed as LGPL - See doc/COPYING for details */ |
3 #include "event_callback.h" |
3 #include "event_callback.h" |
4 #include <assert.h> |
4 #include <assert.h> |
5 #include <lauxlib.h> |
5 #include <lauxlib.h> |
|
6 #include <string.h> |
6 |
7 |
7 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
8 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
8 |
9 |
9 void freeCallbackArgs(le_callback* arg, lua_State* L) { |
10 void freeCallbackArgs(le_callback* arg, lua_State* L) { |
10 if(arg->base) { |
11 if(arg->base) { |
29 lua_call(L, 1, 2); |
30 lua_call(L, 1, 2); |
30 ret = lua_tointeger(L, -2); |
31 ret = lua_tointeger(L, -2); |
31 if(lua_isnumber(L, -1)) { |
32 if(lua_isnumber(L, -1)) { |
32 newTimeout = lua_tonumber(L, -1); |
33 newTimeout = lua_tonumber(L, -1); |
33 if(newTimeout <= 0) { |
34 if(newTimeout <= 0) { |
34 memset(&cb->timeout, 0, sizeof(arg->timeout)); |
35 memset(&cb->timeout, 0, sizeof(cb->timeout)); |
35 } else { |
36 } else { |
36 load_timeval(newTimeout, &cb->timeout); |
37 load_timeval(newTimeout, &cb->timeout); |
37 } |
38 } |
38 } |
39 } |
39 lua_pop(L, 1); |
40 lua_pop(L, 1); |
43 struct event *ev = &cb->ev; |
44 struct event *ev = &cb->ev; |
44 int newEvent = ret; |
45 int newEvent = ret; |
45 /* NOTE: Currently, even if new timeout is the same as the old, a new event is setup regardless... */ |
46 /* NOTE: Currently, even if new timeout is the same as the old, a new event is setup regardless... */ |
46 if(newEvent != event || newTimeout != -1) { // Need to hook up new event... |
47 if(newEvent != event || newTimeout != -1) { // Need to hook up new event... |
47 struct timeval *ptv = &cb->timeout; |
48 struct timeval *ptv = &cb->timeout; |
48 if(!cb->timeout.sec && !cb->timeout.usec) |
49 if(!cb->timeout.tv_sec && !cb->timeout.tv_usec) |
49 ptv = NULL; |
50 ptv = NULL; |
50 event_del(ev); |
51 event_del(ev); |
51 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb); |
52 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb); |
52 /* Assume cannot set a new timeout.. */ |
53 /* Assume cannot set a new timeout.. */ |
53 event_add(ev, ptv); |
54 event_add(ev, ptv); |