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
);
47 ret
= lua_tointeger(L
, -1);
50 freeCallbackArgs(arg
);
52 struct event
*ev
= &arg
->ev
;
54 if(newEvent
!= event
) { // Need to hook up new event...
56 event_set(ev
, fd
, EV_PERSIST
| newEvent
, luaevent_callback
, arg
);
62 static int luaevent_base_gc(lua_State
* L
) {
63 struct event_base
** pbase
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
65 event_base_free(*pbase
);
71 static int luaevent_cb_gc(lua_State
* L
) {
72 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
73 freeCallbackArgs(arg
);
77 int getSocketFd(lua_State
* L
, int idx
) {
79 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
80 lua_getfield(L
, idx
, "getfd");
82 return luaL_error(L
, "Socket type missing 'getfd' method");
83 lua_pushvalue(L
, idx
);
85 fd
= lua_tointeger(L
, -1);
90 /* Expected to be called at the beginning of the coro that uses it..
91 Value must be kept until coro is complete....
93 /* sock, event, callback */
94 static int luaevent_addevent(lua_State
* L
) {
95 int fd
, event
, callbackRef
;
97 fd
= getSocketFd(L
, 1);
98 event
= luaL_checkinteger(L
, 2);
99 luaL_checktype(L
, 3, LUA_TFUNCTION
);
101 callbackRef
= luaL_ref(L
, LUA_REGISTRYINDEX
);
102 arg
= lua_newuserdata(L
, sizeof(*arg
));
103 luaL_getmetatable(L
, EVENT_CALLBACK_ARG_MT
);
104 lua_setmetatable(L
, -2);
107 arg
->callbackRef
= callbackRef
;
109 event_set(&arg
->ev
, fd
, event
| EV_PERSIST
, luaevent_callback
, arg
);
110 event_base_set(getEventBase(L
), &arg
->ev
);
111 event_add(&arg
->ev
, NULL
);
115 static int luaevent_loop(lua_State
* L
) {
116 int ret
= event_base_loop(getEventBase(L
), 0);
117 lua_pushinteger(L
, ret
);
121 static luaL_Reg funcs
[] = {
122 { "addevent", luaevent_addevent
},
123 { "loop", luaevent_loop
},
132 static namedInteger consts
[] = {
134 {"EV_READ", EV_READ
},
135 {"EV_WRITE", EV_WRITE
},
139 void setNamedIntegers(lua_State
* L
, namedInteger
* p
) {
141 lua_pushinteger(L
, p
->value
);
142 lua_setfield(L
, -2, p
->name
);
148 int luaopen_luaevent_core(lua_State
* L
) {
149 /* Setup environ table */
150 lua_createtable(L
, 1, 0);
151 lua_replace(L
, LUA_ENVIRONINDEX
);
152 /* Setup metatable */
153 luaL_newmetatable(L
, EVENT_BASE_MT
);
154 lua_pushcfunction(L
, luaevent_base_gc
);
155 lua_setfield(L
, -2, "__gc");
157 luaL_newmetatable(L
, EVENT_CALLBACK_ARG_MT
);
158 lua_pushcfunction(L
, luaevent_cb_gc
);
159 lua_setfield(L
, -2, "__gc");
162 setEventBase(L
, event_init());
164 luaL_register(L
, "luaevent.core", funcs
);
165 setNamedIntegers(L
, consts
);