diff options
author | Thomas Harning Jr <harningt@gmail.com> | 2007-06-13 03:29:11 +0000 |
---|---|---|
committer | Thomas Harning Jr <harningt@gmail.com> | 2007-06-13 03:29:11 +0000 |
commit | 15c4477d99a84a206453b538b58f20bfc7fdc70d (patch) | |
tree | 0eadc2b3a4ee49263863266e5a093ba1d499b048 | |
parent | feac98c085c1c9d19ac54b6c2bd5deb36958779b (diff) | |
download | luaevent-prosody-15c4477d99a84a206453b538b58f20bfc7fdc70d.tar.gz luaevent-prosody-15c4477d99a84a206453b538b58f20bfc7fdc70d.zip |
Performed some refactoring...
-rw-r--r-- | luaevent/src/luaevent.c | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/luaevent/src/luaevent.c b/luaevent/src/luaevent.c index 7272bf2..261b2de 100644 --- a/luaevent/src/luaevent.c +++ b/luaevent/src/luaevent.c @@ -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 @@ struct event_base* getEventBase(lua_State* L) { 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 @@ void freeCallbackArgs(le_callback* arg) { 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 @@ static void luaevent_callback(int fd, short event, void* p) { 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 @@ static int luaevent_cb_getfd(lua_State* L) { 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 @@ int getSocketFd(lua_State* L, int idx) { 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 @@ Value must be kept until coro is complete.... 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; } |