# HG changeset patch # User Thomas Harning Jr # Date 1181705351 0 # Node ID b134613f6303b10a18662c32147de40a40c5e03e # Parent e13f7c9cb6c716196e222f4d7226f11d3df8505a Performed some refactoring... diff -r e13f7c9cb6c7 -r b134613f6303 luaevent/src/luaevent.c --- a/luaevent/src/luaevent.c Wed Jun 13 02:13:18 2007 +0000 +++ b/luaevent/src/luaevent.c Wed Jun 13 03:29:11 2007 +0000 @@ -10,7 +10,7 @@ #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" #define EVENT_BASE_LOCATION 1 -void setEventBase(lua_State* L, struct event_base* base) { +static void setEventBase(lua_State* L, struct event_base* base) { struct event_base** pbase = lua_newuserdata(L, sizeof(base)); *pbase = base; luaL_getmetatable(L, EVENT_BASE_MT); @@ -25,7 +25,7 @@ return base; } -void freeCallbackArgs(le_callback* arg) { +static void freeCallbackArgs(le_callback* arg) { if(arg->L) { lua_State* L = arg->L; arg->L = NULL; @@ -33,6 +33,36 @@ luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef); } } + +static int call_callback_function(lua_State* L, int argCount) { + int ret; + if(lua_pcall(L, argCount, 1, 0) || !(lua_isnil(L, -1) || lua_isnumber(L, -1))) { + printf("ERROR IN INIT: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + return 0; + } + /* Lua_isnil returns 1 if the value is nil... */ + ret = lua_tointeger(L, -1) | -lua_isnil(L, -1); + lua_pop(L, 1); + if(ret < 0) { /* Done, no need to setup event */ + return 0; + } + if(ret != EV_READ && ret != EV_WRITE) { + printf("BAD RET_VAL IN INIT: %i\n", ret); + } + return 1; +} + +static void luaevent_callback(int fd, short event, void* p); + +static void setup_event(le_callback* arg, int fd, short event, int resetEvent) { + /* Setup event... */ + if(resetEvent) event_del(&arg->ev); + event_set(&arg->ev, fd, event| EV_PERSIST, luaevent_callback, arg); + if(!resetEvent) event_base_set(getEventBase(arg->L), &arg->ev); + event_add(&arg->ev, NULL); +} + /* le_callback is allocated at the beginning of the coroutine in which it is used, no need to manually de-allocate */ @@ -43,29 +73,15 @@ int ret; lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef); lua_pushinteger(L, event); - if(lua_pcall(L, 1, 1, 0) || !(lua_isnil(L, -1) || lua_isnumber(L, -1))) { - printf("ERROR IN CB: %s\n", lua_tostring(L, -1)); - lua_pop(L, 1); - freeCallbackArgs(arg); - return; - } - ret = lua_tointeger(L, -1) | -1lua_isnil(L, -1); - lua_pop(L, 1); - if(ret < 0) { + + if(0 == call_callback_function(L, 1)) { freeCallbackArgs(arg); return; } - if(ret != EV_READ && ret != EV_WRITE) { - printf("BAD RET_VAL: %i\n", ret); - } + printf("RET VAL: %i\n", ret); - struct event *ev = &arg->ev; - int newEvent = ret; - if(newEvent != event) { // Need to hook up new event... - event_del(ev); - event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg); - event_add(ev, NULL); - } + if(event != ret) + setup_event(arg, fd, ret, 1); } static int luaevent_base_gc(lua_State* L) { @@ -89,7 +105,7 @@ return 1; } -int getSocketFd(lua_State* L, int idx) { +static int getSocketFd(lua_State* L, int idx) { int fd; luaL_checktype(L, idx, LUA_TUSERDATA); lua_getfield(L, idx, "getfd"); @@ -102,6 +118,16 @@ return fd; } +static void push_new_callback(lua_State* L, int callbackRef, int fd, short event) { + le_callback* 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(arg, fd, event, 0); +} /* Expected to be called at the beginning of the coro that uses it.. Value must be kept until coro is complete.... */ @@ -109,41 +135,19 @@ static int luaevent_addevent(lua_State* L) { int fd, callbackRef; int top, ret; - le_callback* arg; fd = getSocketFd(L, 1); 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.. */ - if(lua_pcall(L, top - 2, 1, 0) || !(lua_isnil(L, -1) || lua_isnumber(L, -1))) { - printf("ERROR IN INIT: %s\n", lua_tostring(L, -1)); - lua_pop(L, 1); - return 0; - } - /* Lua_isnil returns 1 if the value is nil... */ - ret = lua_tointeger(L, -1) | -lua_isnil(L, -1); - lua_pop(L, 1); - if(ret < 0) { /* Done, no need to setup event */ + if(0 == call_callback_function(L, top - 2)) { luaL_unref(L, LUA_REGISTRYINDEX, callbackRef); return 0; } - if(ret != EV_READ && ret != EV_WRITE) { - printf("BAD RET_VAL IN INIT: %i\n", ret); - } - 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, ret | EV_PERSIST, luaevent_callback, arg); - event_base_set(getEventBase(L), &arg->ev); - event_add(&arg->ev, NULL); + push_new_callback(L, callbackRef, fd, ret); return 1; }