src/event_callback.c

Wed, 05 Sep 2007 22:48:49 -0400

author
Thomas Harning Jr <harningt@gmail.com>
date
Wed, 05 Sep 2007 22:48:49 -0400
changeset 20
71bc2e49366c
child 21
728aafac9682
permissions
-rw-r--r--

Beginning refactoring of the event_callback outside of the core

20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
1 #include "event_callback.h"
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
2 #include <assert.h>
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
3 #include <lauxlib.h>
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
4
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
5 void freeCallbackArgs(le_callback* arg, lua_State* L) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
6 if(arg->base) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
7 arg->base = NULL;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
8 event_del(&arg->ev);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
9 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
10 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
11 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
12 /* le_callback is allocated at the beginning of the coroutine in which it
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
13 is used, no need to manually de-allocate */
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
14
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
15 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
16 void luaevent_callback(int fd, short event, void* p) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
17 le_callback* arg = p;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
18 lua_State* L;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
19 int ret;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
20 assert(arg && arg->base && arg->base->loop_L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
21 L = arg->base->loop_L;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
22 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
23 lua_pushinteger(L, event);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
24 lua_call(L, 1, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
25 ret = lua_tointeger(L, -1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
26 lua_pop(L, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
27 if(ret == -1) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
28 freeCallbackArgs(arg, L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
29 } else {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
30 struct event *ev = &arg->ev;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
31 int newEvent = ret;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
32 if(newEvent != event) { // Need to hook up new event...
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
33 event_del(ev);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
34 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
35 event_add(ev, NULL);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
36 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
37 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
38 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
39
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
40 static int luaevent_cb_gc(lua_State* L) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
41 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
42 freeCallbackArgs(arg, L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
43 return 0;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
44 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
45
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
46 int event_callback_register(lua_State* L) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
47 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
48 lua_pushcfunction(L, luaevent_cb_gc);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
49 lua_setfield(L, -2, "__gc");
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
50 lua_newtable(L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
51 lua_pushcfunction(L, luaevent_cb_gc);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
52 lua_setfield(L, -2, "close");
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
53 lua_setfield(L, -2, "__index");
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
54 lua_pop(L, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
55 return 0;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
56 }

mercurial