Setup management of socket create/close.
[luaevent.git] / luaevent / src / luaevent.c
blobe5e33cf2db07ede8d6b73f32adce34fc82d7257a
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 static 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 static 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);
34 luaL_unref(L, LUA_REGISTRYINDEX, arg->objectRef);
38 static int call_callback_function(lua_State* L, int argCount) {
39 int ret;
40 if(lua_pcall(L, argCount, 1, 0) || !(lua_isnil(L, -1) || lua_isnumber(L, -1))) {
41 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1));
42 lua_pop(L, 1);
43 return -1;
45 /* Lua_isnil returns 1 if the value is nil... */
46 ret = lua_tointeger(L, -1) | -lua_isnil(L, -1);
47 lua_pop(L, 1);
48 if(ret < 0) { /* Done, no need to setup event */
49 return -1;
51 if(ret != EV_READ && ret != EV_WRITE) {
52 printf("BAD RET_VAL IN INIT: %i\n", ret);
54 return ret;
57 static void luaevent_callback(int fd, short event, void* p);
59 static void setup_event(le_callback* arg, int fd, short event, int resetEvent) {
60 /* Setup event... */
61 if(resetEvent) event_del(&arg->ev);
62 event_set(&arg->ev, fd, event| EV_PERSIST, luaevent_callback, arg);
63 if(!resetEvent) event_base_set(getEventBase(arg->L), &arg->ev);
64 event_add(&arg->ev, NULL);
67 /* le_callback is allocated at the beginning of the coroutine in which it
68 is used, no need to manually de-allocate */
70 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
71 static void luaevent_callback(int fd, short event, void* p) {
72 le_callback* arg = p;
73 lua_State* L = arg->L;
74 int ret;
75 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
76 lua_pushinteger(L, event);
78 if(-1 == (ret = call_callback_function(L, 1))) {
79 freeCallbackArgs(arg);
80 return;
83 if(event != ret)
84 setup_event(arg, fd, ret, 1);
87 static int luaevent_base_gc(lua_State* L) {
88 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
89 if(*pbase) {
90 event_base_free(*pbase);
91 *pbase = NULL;
93 return 0;
96 static int luaevent_cb_gc(lua_State* L) {
97 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
98 freeCallbackArgs(arg);
99 return 0;
102 static int luaevent_cb_getfd(lua_State* L) {
103 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
104 lua_pushinteger(L, arg->ev.ev_fd);
105 return 1;
108 static int getSocketFd(lua_State* L, int idx) {
109 int fd;
110 luaL_checktype(L, idx, LUA_TUSERDATA);
111 lua_getfield(L, idx, "getfd");
112 if(lua_isnil(L, -1))
113 return luaL_error(L, "Socket type missing 'getfd' method");
114 lua_pushvalue(L, idx);
115 lua_call(L, 1, 1);
116 fd = lua_tointeger(L, -1);
117 lua_pop(L, 1);
118 return fd;
121 static void push_new_callback(lua_State* L, int callbackRef, int fd, short event) {
122 le_callback* arg = lua_newuserdata(L, sizeof(*arg));
123 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
124 lua_setmetatable(L, -2);
126 arg->L = L;
127 arg->callbackRef = callbackRef;
128 lua_pushvalue(L, -1);
129 arg->objectRef = luaL_ref(L, LUA_REGISTRYINDEX);
130 setup_event(arg, fd, event, 0);
132 /* Expected to be called at the beginning of the coro that uses it..
133 Value must be kept until coro is complete....
135 /* sock, callback */
136 static int luaevent_addevent(lua_State* L) {
137 int fd, callbackRef;
138 int top, ret;
139 fd = getSocketFd(L, 1);
140 luaL_checktype(L, 2, LUA_TFUNCTION);
141 top = lua_gettop(L);
142 /* Preserve the callback function */
143 lua_pushvalue(L, 2);
144 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
145 /* Call the callback with all arguments after it to get the loop primed.. */
146 if(-1 == (ret = call_callback_function(L, top - 2))) {
147 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
148 return 0;
151 push_new_callback(L, callbackRef, fd, ret);
152 return 1;
155 static int luaevent_loop(lua_State* L) {
156 int ret = event_base_loop(getEventBase(L), 0);
157 lua_pushinteger(L, ret);
158 return 1;
161 static luaL_Reg funcs[] = {
162 { "addevent", luaevent_addevent },
163 { "loop", luaevent_loop },
164 { NULL, NULL }
167 typedef struct {
168 const char* name;
169 int value;
170 } namedInteger;
172 static namedInteger consts[] = {
173 {"LEAVE", -1},
174 {"EV_READ", EV_READ},
175 {"EV_WRITE", EV_WRITE},
176 {NULL, 0}
179 void setNamedIntegers(lua_State* L, namedInteger* p) {
180 while(p->name) {
181 lua_pushinteger(L, p->value);
182 lua_setfield(L, -2, p->name);
183 p++;
187 /* Verified ok */
188 int luaopen_luaevent_core(lua_State* L) {
189 /* Setup environ table */
190 lua_createtable(L, 1, 0);
191 lua_replace(L, LUA_ENVIRONINDEX);
192 /* Setup metatable */
193 luaL_newmetatable(L, EVENT_BASE_MT);
194 lua_pushcfunction(L, luaevent_base_gc);
195 lua_setfield(L, -2, "__gc");
196 lua_pop(L, 1);
197 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
198 lua_pushcfunction(L, luaevent_cb_gc);
199 lua_setfield(L, -2, "__gc");
200 lua_pushcfunction(L, luaevent_cb_getfd);
201 lua_setfield(L, -2, "getfd");
202 lua_pop(L, 1);
204 setEventBase(L, event_init());
206 luaL_register(L, "luaevent.core", funcs);
207 setNamedIntegers(L, consts);
208 return 1;