* Added some cheap protection code for failures in callback
[luaevent.git] / luaevent / src / luaevent.c
blob120d43311a0f5cfb084eb0230344b7ff17d757c7
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
4 #include "luaevent.h"
6 #include <lua.h>
7 #include <lauxlib.h>
9 #define EVENT_BASE_MT "EVENT_BASE_MT"
10 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
11 #define EVENT_BASE_LOCATION 1
13 void setEventBase(lua_State* L, struct event_base* base) {
14 struct event_base** pbase = lua_newuserdata(L, sizeof(base));
15 *pbase = base;
16 luaL_getmetatable(L, EVENT_BASE_MT);
17 lua_setmetatable(L, -2);
18 lua_rawseti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
20 struct event_base* getEventBase(lua_State* L) {
21 struct event_base* base;
22 lua_rawgeti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
23 base = *(struct event_base**)lua_topointer(L, -1);
24 lua_pop(L, 1);
25 return base;
28 void freeCallbackArgs(le_callback* arg) {
29 if(arg->L) {
30 lua_State* L = arg->L;
31 arg->L = NULL;
32 event_del(&arg->ev);
33 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
36 /* le_callback is allocated at the beginning of the coroutine in which it
37 is used, no need to manually de-allocate */
39 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
40 static void luaevent_callback(int fd, short event, void* p) {
41 le_callback* arg = p;
42 lua_State* L = arg->L;
43 int ret;
44 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
45 lua_pushinteger(L, event);
46 if(lua_pcall(L, 1, 1, 0) || !lua_isnumber(L, -1)) {
47 printf("ERROR IN CB: %s\n", lua_tostring(L, -1));
48 freeCallbackArgs(arg);
49 return;
51 ret = lua_tointeger(L, -1);
52 lua_pop(L, 1);
53 if(ret == -1) {
54 freeCallbackArgs(arg);
55 return;
57 if(ret != EV_READ && ret != EV_WRITE) {
58 printf("BAD RET_VAL: %i\n", ret);
61 struct event *ev = &arg->ev;
62 int newEvent = ret;
63 if(newEvent != event) { // Need to hook up new event...
64 event_del(ev);
65 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
66 event_add(ev, NULL);
70 static int luaevent_base_gc(lua_State* L) {
71 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
72 if(*pbase) {
73 event_base_free(*pbase);
74 *pbase = NULL;
76 return 0;
79 static int luaevent_cb_gc(lua_State* L) {
80 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
81 freeCallbackArgs(arg);
82 return 0;
85 static int luaevent_cb_getfd(lua_State* L) {
86 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
87 lua_pushinteger(L, arg->ev.ev_fd);
88 return 1;
91 int getSocketFd(lua_State* L, int idx) {
92 int fd;
93 luaL_checktype(L, idx, LUA_TUSERDATA);
94 lua_getfield(L, idx, "getfd");
95 if(lua_isnil(L, -1))
96 return luaL_error(L, "Socket type missing 'getfd' method");
97 lua_pushvalue(L, idx);
98 lua_call(L, 1, 1);
99 fd = lua_tointeger(L, -1);
100 lua_pop(L, 1);
101 return fd;
104 /* Expected to be called at the beginning of the coro that uses it..
105 Value must be kept until coro is complete....
107 /* sock, callback */
108 static int luaevent_addevent(lua_State* L) {
109 int fd, callbackRef;
110 int top, ret;
111 le_callback* arg;
112 fd = getSocketFd(L, 1);
113 luaL_checktype(L, 2, LUA_TFUNCTION);
114 top = lua_gettop(L);
115 /* Preserve the callback function */
116 lua_pushvalue(L, 2);
117 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
119 /* Call the callback with all arguments after it to get the loop primed.. */
120 if(lua_pcall(L, top - 2, 1, 0) || !lua_isnumber(L, -1)) {
121 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1));
122 return 0;
124 ret = lua_tointeger(L, -1);
125 lua_pop(L, 1);
126 if(ret == -1) { /* Done, no need to setup event */
127 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
128 return 0;
130 if(ret != EV_READ && ret != EV_WRITE) {
131 printf("BAD RET_VAL IN INIT: %i\n", ret);
133 arg = lua_newuserdata(L, sizeof(*arg));
134 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
135 lua_setmetatable(L, -2);
137 arg->L = L;
138 arg->callbackRef = callbackRef;
140 /* Setup event... */
141 event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg);
142 event_base_set(getEventBase(L), &arg->ev);
143 event_add(&arg->ev, NULL);
144 return 1;
147 static int luaevent_loop(lua_State* L) {
148 int ret = event_base_loop(getEventBase(L), 0);
149 lua_pushinteger(L, ret);
150 return 1;
153 static luaL_Reg funcs[] = {
154 { "addevent", luaevent_addevent },
155 { "loop", luaevent_loop },
156 { NULL, NULL }
159 typedef struct {
160 const char* name;
161 int value;
162 } namedInteger;
164 static namedInteger consts[] = {
165 {"LEAVE", -1},
166 {"EV_READ", EV_READ},
167 {"EV_WRITE", EV_WRITE},
168 {NULL, 0}
171 void setNamedIntegers(lua_State* L, namedInteger* p) {
172 while(p->name) {
173 lua_pushinteger(L, p->value);
174 lua_setfield(L, -2, p->name);
175 p++;
179 /* Verified ok */
180 int luaopen_luaevent_core(lua_State* L) {
181 /* Setup environ table */
182 lua_createtable(L, 1, 0);
183 lua_replace(L, LUA_ENVIRONINDEX);
184 /* Setup metatable */
185 luaL_newmetatable(L, EVENT_BASE_MT);
186 lua_pushcfunction(L, luaevent_base_gc);
187 lua_setfield(L, -2, "__gc");
188 lua_pop(L, 1);
189 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
190 lua_pushcfunction(L, luaevent_cb_gc);
191 lua_setfield(L, -2, "__gc");
192 lua_pushcfunction(L, luaevent_cb_getfd);
193 lua_setfield(L, -2, "getfd");
194 lua_pop(L, 1);
196 setEventBase(L, event_init());
198 luaL_register(L, "luaevent.core", funcs);
199 setNamedIntegers(L, consts);
200 return 1;