1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
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
));
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);
28 void freeCallbackArgs(le_callback
* arg
) {
30 lua_State
* L
= arg
->L
;
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
) {
42 lua_State
* L
= arg
->L
;
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
);
51 ret
= lua_tointeger(L
, -1);
54 freeCallbackArgs(arg
);
57 if(ret
!= EV_READ
&& ret
!= EV_WRITE
) {
58 printf("BAD RET_VAL: %i\n", ret
);
61 struct event
*ev
= &arg
->ev
;
63 if(newEvent
!= event
) { // Need to hook up new event...
65 event_set(ev
, fd
, EV_PERSIST
| newEvent
, luaevent_callback
, arg
);
70 static int luaevent_base_gc(lua_State
* L
) {
71 struct event_base
** pbase
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
73 event_base_free(*pbase
);
79 static int luaevent_cb_gc(lua_State
* L
) {
80 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
81 freeCallbackArgs(arg
);
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
);
91 int getSocketFd(lua_State
* L
, int idx
) {
93 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
94 lua_getfield(L
, idx
, "getfd");
96 return luaL_error(L
, "Socket type missing 'getfd' method");
97 lua_pushvalue(L
, idx
);
99 fd
= lua_tointeger(L
, -1);
104 /* Expected to be called at the beginning of the coro that uses it..
105 Value must be kept until coro is complete....
108 static int luaevent_addevent(lua_State
* L
) {
112 fd
= getSocketFd(L
, 1);
113 luaL_checktype(L
, 2, LUA_TFUNCTION
);
115 /* Preserve the callback function */
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));
124 ret
= lua_tointeger(L
, -1);
126 if(ret
== -1) { /* Done, no need to setup event */
127 luaL_unref(L
, LUA_REGISTRYINDEX
, callbackRef
);
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);
138 arg
->callbackRef
= callbackRef
;
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
);
147 static int luaevent_loop(lua_State
* L
) {
148 int ret
= event_base_loop(getEventBase(L
), 0);
149 lua_pushinteger(L
, ret
);
153 static luaL_Reg funcs
[] = {
154 { "addevent", luaevent_addevent
},
155 { "loop", luaevent_loop
},
164 static namedInteger consts
[] = {
166 {"EV_READ", EV_READ
},
167 {"EV_WRITE", EV_WRITE
},
171 void setNamedIntegers(lua_State
* L
, namedInteger
* p
) {
173 lua_pushinteger(L
, p
->value
);
174 lua_setfield(L
, -2, p
->name
);
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");
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");
196 setEventBase(L
, event_init());
198 luaL_register(L
, "luaevent.core", funcs
);
199 setNamedIntegers(L
, consts
);