aboutsummaryrefslogtreecommitdiffstats
path: root/luaevent/src/luaevent.c
diff options
context:
space:
mode:
authorThomas Harning Jr <harningt@gmail.com>2007-06-11 01:08:59 +0000
committerThomas Harning Jr <harningt@gmail.com>2007-06-11 01:08:59 +0000
commit35ac0581fa8e21502283480b99fc67ed7d46dc3b (patch)
treeedb989b681d95ca16caaebb55712cec6d23a1999 /luaevent/src/luaevent.c
parent9839406cc22bd47bbf4bd82d73860f9f7e270155 (diff)
downloadluaevent-prosody-35ac0581fa8e21502283480b99fc67ed7d46dc3b.tar.gz
luaevent-prosody-35ac0581fa8e21502283480b99fc67ed7d46dc3b.zip
* Completed mostly working version
* Moved to a mode where addevent calls a callback rather than it being instantiated within. * If the callback returns -1, then no event is ever setup * Otherwise the integer value is used to setup the event * This allows for using coroutine.wrap rather than a cooked-up wrapper * Tests work, although there are a few remaining issues: * Need to figure a good way of preserving the event object, not sure if current method is good enough, since the socket is the only anchor, and it is only held inside the coro.. circular reference, something that Lua 'handles' well. * Doing more than the maximum sockets the process is allows causes strangeness to occur in libevent.. somehow it is getting around to epoll_add which is causing valgrind to barf.
Diffstat (limited to 'luaevent/src/luaevent.c')
-rw-r--r--luaevent/src/luaevent.c32
1 files changed, 26 insertions, 6 deletions
diff --git a/luaevent/src/luaevent.c b/luaevent/src/luaevent.c
index 311ad0e..d79345e 100644
--- a/luaevent/src/luaevent.c
+++ b/luaevent/src/luaevent.c
@@ -74,6 +74,12 @@ static int luaevent_cb_gc(lua_State* L) {
return 0;
}
+static int luaevent_cb_getfd(lua_State* L) {
+ le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
+ lua_pushinteger(L, arg->ev.ev_fd);
+ return 1;
+}
+
int getSocketFd(lua_State* L, int idx) {
int fd;
luaL_checktype(L, idx, LUA_TUSERDATA);
@@ -90,23 +96,35 @@ int getSocketFd(lua_State* L, int idx) {
/* Expected to be called at the beginning of the coro that uses it..
Value must be kept until coro is complete....
*/
-/* sock, event, callback */
+/* sock, callback */
static int luaevent_addevent(lua_State* L) {
- int fd, event, callbackRef;
+ int fd, callbackRef;
+ int top, ret;
le_callback* arg;
fd = getSocketFd(L, 1);
- event = luaL_checkinteger(L, 2);
- luaL_checktype(L, 3, LUA_TFUNCTION);
- lua_pushvalue(L, 3);
+ luaL_checktype(L, 2, LUA_TFUNCTION);
+ top = lua_gettop(L);
+ /* Preserve the callback function */
+ lua_pushvalue(L, 2);
callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
+
+ /* Call the callback with all arguments after it to get the loop primed.. */
+ lua_call(L, top - 2, 1);
+ ret = lua_tointeger(L, -1);
+ lua_pop(L, 1);
+ if(ret == -1) { /* Done, no need to setup event */
+ luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
+ return 0;
+ }
arg = lua_newuserdata(L, sizeof(*arg));
luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
lua_setmetatable(L, -2);
arg->L = L;
arg->callbackRef = callbackRef;
+
/* Setup event... */
- event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
+ event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg);
event_base_set(getEventBase(L), &arg->ev);
event_add(&arg->ev, NULL);
return 1;
@@ -157,6 +175,8 @@ int luaopen_luaevent_core(lua_State* L) {
luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
lua_pushcfunction(L, luaevent_cb_gc);
lua_setfield(L, -2, "__gc");
+ lua_pushcfunction(L, luaevent_cb_getfd);
+ lua_setfield(L, -2, "getfd");
lua_pop(L, 1);
setEventBase(L, event_init());