Wed, 05 Sep 2007 23:35:31 -0400
Fixed compilation issues
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> |
24
cda8e1a2dfa2
Fixed compilation issues
Thomas Harning Jr <harningt@gmail.com>
parents:
23
diff
changeset
|
6 | #include <string.h> |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
7 | |
22
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
8 | #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
9 | |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
10 | 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
|
11 | if(arg->base) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
12 | arg->base = NULL; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
13 | event_del(&arg->ev); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
14 | 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
|
15 | } |
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 | /* 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
|
18 | 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
|
19 | |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
20 | /* 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
|
21 | void luaevent_callback(int fd, short event, void* p) { |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
22 | le_callback* cb = p; |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
23 | lua_State* L; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
24 | int ret; |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
25 | double newTimeout = -1; |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
26 | assert(cb && cb->base && cb->base->loop_L); |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
27 | L = cb->base->loop_L; |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
28 | lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef); |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
29 | lua_pushinteger(L, event); |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
30 | lua_call(L, 1, 2); |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
31 | ret = lua_tointeger(L, -2); |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
32 | if(lua_isnumber(L, -1)) { |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
33 | newTimeout = lua_tonumber(L, -1); |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
34 | if(newTimeout <= 0) { |
24
cda8e1a2dfa2
Fixed compilation issues
Thomas Harning Jr <harningt@gmail.com>
parents:
23
diff
changeset
|
35 | memset(&cb->timeout, 0, sizeof(cb->timeout)); |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
36 | } else { |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
37 | load_timeval(newTimeout, &cb->timeout); |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
38 | } |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
39 | } |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
40 | lua_pop(L, 1); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
41 | if(ret == -1) { |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
42 | freeCallbackArgs(cb, L); |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
43 | } else { |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
44 | struct event *ev = &cb->ev; |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
45 | int newEvent = ret; |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
46 | /* NOTE: Currently, even if new timeout is the same as the old, a new event is setup regardless... */ |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
47 | if(newEvent != event || newTimeout != -1) { // Need to hook up new event... |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
48 | struct timeval *ptv = &cb->timeout; |
24
cda8e1a2dfa2
Fixed compilation issues
Thomas Harning Jr <harningt@gmail.com>
parents:
23
diff
changeset
|
49 | if(!cb->timeout.tv_sec && !cb->timeout.tv_usec) |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
50 | ptv = NULL; |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
51 | event_del(ev); |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
52 | event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb); |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
53 | /* Assume cannot set a new timeout.. */ |
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
54 | event_add(ev, ptv); |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
55 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
56 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
57 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
58 | |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
59 | 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
|
60 | 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
|
61 | freeCallbackArgs(arg, L); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
62 | return 0; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
63 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
64 | |
22
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
65 | le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) { |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
66 | le_callback* cb; |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
67 | le_base *base = event_base_get(L, baseIdx); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
68 | luaL_checktype(L, callbackIdx, LUA_TFUNCTION); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
69 | cb = lua_newuserdata(L, sizeof(*cb)); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
70 | luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
71 | lua_setmetatable(L, -2); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
72 | |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
73 | lua_pushvalue(L, callbackIdx); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
74 | cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
75 | cb->base = base; |
23
897150985f13
Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents:
22
diff
changeset
|
76 | memset(&cb->timeout, 0, sizeof(cb->timeout)); |
22
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
77 | return cb; |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
78 | } |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
79 | |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
80 | 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
|
81 | 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
|
82 | 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
|
83 | 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
|
84 | lua_newtable(L); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
85 | 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
|
86 | 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
|
87 | 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
|
88 | lua_pop(L, 1); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
89 | return 0; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
90 | } |