src/event_callback.c

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

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

Added missing license header

21
728aafac9682 Added missing license header
Thomas Harning Jr <harningt@gmail.com>
parents: 20
diff changeset
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
728aafac9682 Added missing license header
Thomas Harning Jr <harningt@gmail.com>
parents: 20
diff changeset
2 * Licensed as LGPL - See doc/COPYING for details */
20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
3 #include "event_callback.h"
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
4 #include <assert.h>
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
5 #include <lauxlib.h>
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
6
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
7 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
8 if(arg->base) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
9 arg->base = NULL;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
10 event_del(&arg->ev);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
11 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
12 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
13 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
14 /* 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
15 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
16
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
17 /* 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
18 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
19 le_callback* arg = p;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
20 lua_State* L;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
21 int ret;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
22 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
23 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
24 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
25 lua_pushinteger(L, event);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
26 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
27 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
28 lua_pop(L, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
29 if(ret == -1) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
30 freeCallbackArgs(arg, L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
31 } else {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
32 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
33 int newEvent = ret;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
34 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
35 event_del(ev);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
36 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
37 event_add(ev, NULL);
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 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
41
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
42 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
43 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
44 freeCallbackArgs(arg, L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
45 return 0;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
46 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
47
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
48 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
49 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
50 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
51 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
52 lua_newtable(L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
53 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
54 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
55 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
56 lua_pop(L, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
57 return 0;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
58 }

mercurial