1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
10 #define EVENT_BASE_MT "EVENT_BASE_MT"
11 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
13 int luaevent_newbase(lua_State
* L
) {
14 le_base
*base
= (le_base
*)lua_newuserdata(L
, sizeof(le_base
));
15 base
->loop_L
= NULL
; /* No running loop */
16 base
->base
= event_init();
17 luaL_getmetatable(L
, EVENT_BASE_MT
);
18 lua_setmetatable(L
, -2);
22 void freeCallbackArgs(le_callback
* arg
, lua_State
* L
) {
26 luaL_unref(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
29 /* le_callback is allocated at the beginning of the coroutine in which it
30 is used, no need to manually de-allocate */
32 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
33 static void luaevent_callback(int fd
, short event
, void* p
) {
37 assert(arg
&& arg
->base
&& arg
->base
->loop_L
);
38 L
= arg
->base
->loop_L
;
39 lua_rawgeti(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
40 lua_pushinteger(L
, event
);
42 ret
= lua_tointeger(L
, -1);
45 freeCallbackArgs(arg
, L
);
47 struct event
*ev
= &arg
->ev
;
49 if(newEvent
!= event
) { // Need to hook up new event...
51 event_set(ev
, fd
, EV_PERSIST
| newEvent
, luaevent_callback
, arg
);
57 static int luaevent_base_gc(lua_State
* L
) {
58 le_base
*base
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
60 event_base_free(base
->base
);
66 static int luaevent_cb_gc(lua_State
* L
) {
67 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
68 freeCallbackArgs(arg
, L
);
72 int getSocketFd(lua_State
* L
, int idx
) {
74 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
75 lua_getfield(L
, idx
, "getfd");
77 return luaL_error(L
, "Socket type missing 'getfd' method");
78 lua_pushvalue(L
, idx
);
80 fd
= lua_tointeger(L
, -1);
85 /* sock, event, callback */
86 static int luaevent_addevent(lua_State
* L
) {
87 int fd
, event
, callbackRef
;
89 le_base
*base
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
90 fd
= getSocketFd(L
, 2);
91 event
= luaL_checkinteger(L
, 3);
92 luaL_checktype(L
, 4, LUA_TFUNCTION
);
94 callbackRef
= luaL_ref(L
, LUA_REGISTRYINDEX
);
95 arg
= lua_newuserdata(L
, sizeof(*arg
));
96 luaL_getmetatable(L
, EVENT_CALLBACK_ARG_MT
);
97 lua_setmetatable(L
, -2);
100 arg
->callbackRef
= callbackRef
;
102 event_set(&arg
->ev
, fd
, event
| EV_PERSIST
, luaevent_callback
, arg
);
103 event_base_set(base
->base
, &arg
->ev
);
104 event_add(&arg
->ev
, NULL
);
108 static int luaevent_loop(lua_State
* L
) {
109 le_base
*base
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
111 int ret
= event_base_loop(base
->base
, 0);
112 lua_pushinteger(L
, ret
);
116 static luaL_Reg base_funcs
[] = {
117 { "addevent", luaevent_addevent
},
118 { "loop", luaevent_loop
},
122 static luaL_Reg funcs
[] = {
123 { "new", luaevent_newbase
},
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 metatable */
150 luaL_newmetatable(L
, EVENT_BASE_MT
);
152 luaL_register(L
, NULL
, base_funcs
);
153 lua_setfield(L
, -2, "__index");
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");
161 lua_pushcfunction(L
, luaevent_cb_gc
);
162 lua_setfield(L
, -2, "close");
163 lua_setfield(L
, -2, "__index");
166 luaL_register(L
, "luaevent.core", funcs
);
167 setNamedIntegers(L
, consts
);