aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/event_callback.h4
-rw-r--r--include/luaevent.h2
-rw-r--r--src/event_callback.c16
-rw-r--r--src/luaevent.c24
4 files changed, 29 insertions, 17 deletions
diff --git a/include/event_callback.h b/include/event_callback.h
index e6e60c4..cb34d8e 100644
--- a/include/event_callback.h
+++ b/include/event_callback.h
@@ -9,8 +9,6 @@
#include <sys/time.h>
#include <event.h>
-#define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
-
typedef struct {
struct event ev;
le_base* base;
@@ -19,6 +17,8 @@ typedef struct {
int event_callback_register(lua_State* L);
+le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx);
+
void luaevent_callback(int fd, short event, void* p);
#endif
diff --git a/include/luaevent.h b/include/luaevent.h
index ce2624e..e8b8f05 100644
--- a/include/luaevent.h
+++ b/include/luaevent.h
@@ -13,6 +13,8 @@ typedef struct {
lua_State* loop_L;
} le_base;
+le_base* event_base_get(lua_State* L, int idx);
+
int luaopen_luaevent(lua_State* L);
#endif
diff --git a/src/event_callback.c b/src/event_callback.c
index 0d320eb..f2cd0bc 100644
--- a/src/event_callback.c
+++ b/src/event_callback.c
@@ -4,6 +4,8 @@
#include <assert.h>
#include <lauxlib.h>
+#define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
+
void freeCallbackArgs(le_callback* arg, lua_State* L) {
if(arg->base) {
arg->base = NULL;
@@ -45,6 +47,20 @@ static int luaevent_cb_gc(lua_State* L) {
return 0;
}
+le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) {
+ le_callback* cb;
+ le_base *base = event_base_get(L, baseIdx);
+ luaL_checktype(L, callbackIdx, LUA_TFUNCTION);
+ cb = lua_newuserdata(L, sizeof(*cb));
+ luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
+ lua_setmetatable(L, -2);
+
+ lua_pushvalue(L, callbackIdx);
+ cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
+ cb->base = base;
+ return cb;
+}
+
int event_callback_register(lua_State* L) {
luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
lua_pushcfunction(L, luaevent_cb_gc);
diff --git a/src/luaevent.c b/src/luaevent.c
index 72c1ce5..4cadbb5 100644
--- a/src/luaevent.c
+++ b/src/luaevent.c
@@ -10,6 +10,10 @@
#define EVENT_BASE_MT "EVENT_BASE_MT"
+le_base* event_base_get(lua_State* L, int idx) {
+ return (le_base*)luaL_checkudata(L, idx, EVENT_BASE_MT);
+}
+
int luaevent_newbase(lua_State* L) {
le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
base->loop_L = NULL; /* No running loop */
@@ -20,7 +24,7 @@ int luaevent_newbase(lua_State* L) {
}
static int luaevent_base_gc(lua_State* L) {
- le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
+ le_base *base = event_base_get(L, 1);
if(base->base) {
event_base_free(base->base);
base->base = NULL;
@@ -43,29 +47,19 @@ int getSocketFd(lua_State* L, int idx) {
/* sock, event, callback */
static int luaevent_addevent(lua_State* L) {
- int fd, event, callbackRef;
- le_callback* arg;
- le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
+ int fd, event;
+ le_callback* arg = event_callback_push(L, 1, 4);
fd = getSocketFd(L, 2);
event = luaL_checkinteger(L, 3);
- luaL_checktype(L, 4, LUA_TFUNCTION);
- lua_pushvalue(L, 4);
- callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
- arg = lua_newuserdata(L, sizeof(*arg));
- luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
- lua_setmetatable(L, -2);
-
- arg->base = base;
- arg->callbackRef = callbackRef;
/* Setup event... */
event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
- event_base_set(base->base, &arg->ev);
+ event_base_set(arg->base->base, &arg->ev);
event_add(&arg->ev, NULL);
return 1;
}
static int luaevent_loop(lua_State* L) {
- le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
+ le_base *base = event_base_get(L, 1);
base->loop_L = L;
int ret = event_base_loop(base->base, 0);
lua_pushinteger(L, ret);