src/luaevent.c

changeset 20
71bc2e49366c
parent 19
cbaa6e6ef285
child 22
48a109847dce
equal deleted inserted replaced
19:cbaa6e6ef285 20:71bc2e49366c
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> 1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */ 2 * Licensed as LGPL - See doc/COPYING for details */
3 3
4 #include "luaevent.h" 4 #include "luaevent.h"
5 #include "event_callback.h"
5 6
6 #include <lua.h> 7 #include <lua.h>
7 #include <lauxlib.h> 8 #include <lauxlib.h>
8 #include <assert.h> 9 #include <assert.h>
9 10
10 #define EVENT_BASE_MT "EVENT_BASE_MT" 11 #define EVENT_BASE_MT "EVENT_BASE_MT"
11 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
12 12
13 int luaevent_newbase(lua_State* L) { 13 int luaevent_newbase(lua_State* L) {
14 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base)); 14 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
15 base->loop_L = NULL; /* No running loop */ 15 base->loop_L = NULL; /* No running loop */
16 base->base = event_init(); 16 base->base = event_init();
17 luaL_getmetatable(L, EVENT_BASE_MT); 17 luaL_getmetatable(L, EVENT_BASE_MT);
18 lua_setmetatable(L, -2); 18 lua_setmetatable(L, -2);
19 return 1; 19 return 1;
20 } 20 }
21 21
22 void freeCallbackArgs(le_callback* arg, lua_State* L) {
23 if(arg->base) {
24 arg->base = NULL;
25 event_del(&arg->ev);
26 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
27 }
28 }
29 /* le_callback is allocated at the beginning of the coroutine in which it
30 is used, no need to manually de-allocate */
31
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) {
34 le_callback* arg = p;
35 lua_State* L;
36 int ret;
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);
41 lua_call(L, 1, 1);
42 ret = lua_tointeger(L, -1);
43 lua_pop(L, 1);
44 if(ret == -1) {
45 freeCallbackArgs(arg, L);
46 } else {
47 struct event *ev = &arg->ev;
48 int newEvent = ret;
49 if(newEvent != event) { // Need to hook up new event...
50 event_del(ev);
51 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
52 event_add(ev, NULL);
53 }
54 }
55 }
56
57 static int luaevent_base_gc(lua_State* L) { 22 static int luaevent_base_gc(lua_State* L) {
58 le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT); 23 le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
59 if(base->base) { 24 if(base->base) {
60 event_base_free(base->base); 25 event_base_free(base->base);
61 base->base = NULL; 26 base->base = NULL;
62 } 27 }
63 return 0;
64 }
65
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);
69 return 0; 28 return 0;
70 } 29 }
71 30
72 int getSocketFd(lua_State* L, int idx) { 31 int getSocketFd(lua_State* L, int idx) {
73 int fd; 32 int fd;
144 } 103 }
145 } 104 }
146 105
147 /* Verified ok */ 106 /* Verified ok */
148 int luaopen_luaevent_core(lua_State* L) { 107 int luaopen_luaevent_core(lua_State* L) {
108 /* Register external items */
109 event_callback_register(L);
149 /* Setup metatable */ 110 /* Setup metatable */
150 luaL_newmetatable(L, EVENT_BASE_MT); 111 luaL_newmetatable(L, EVENT_BASE_MT);
151 lua_newtable(L); 112 lua_newtable(L);
152 luaL_register(L, NULL, base_funcs); 113 luaL_register(L, NULL, base_funcs);
153 lua_setfield(L, -2, "__index"); 114 lua_setfield(L, -2, "__index");
154 lua_pushcfunction(L, luaevent_base_gc); 115 lua_pushcfunction(L, luaevent_base_gc);
155 lua_setfield(L, -2, "__gc"); 116 lua_setfield(L, -2, "__gc");
156 lua_pop(L, 1); 117 lua_pop(L, 1);
157 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
158 lua_pushcfunction(L, luaevent_cb_gc);
159 lua_setfield(L, -2, "__gc");
160 lua_newtable(L);
161 lua_pushcfunction(L, luaevent_cb_gc);
162 lua_setfield(L, -2, "close");
163 lua_setfield(L, -2, "__index");
164 lua_pop(L, 1);
165 118
166 luaL_register(L, "luaevent.core", funcs); 119 luaL_register(L, "luaevent.core", funcs);
167 setNamedIntegers(L, consts); 120 setNamedIntegers(L, consts);
168 return 1; 121 return 1;
169 } 122 }

mercurial