src/event_callback.c

Wed, 05 Sep 2007 23:33:46 -0400

author
Thomas Harning Jr <harningt@gmail.com>
date
Wed, 05 Sep 2007 23:33:46 -0400
changeset 23
897150985f13
parent 22
48a109847dce
child 24
cda8e1a2dfa2
permissions
-rw-r--r--

Added support for timeouts and timers.

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
22
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
7 #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
8
20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
9 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
10 if(arg->base) {
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
11 arg->base = NULL;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
12 event_del(&arg->ev);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
13 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
14 }
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 /* 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
17 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
18
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
19 /* 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
20 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
21 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
22 lua_State* L;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
23 int ret;
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
24 double newTimeout = -1;
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
25 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
26 L = cb->base->loop_L;
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
27 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
28 lua_pushinteger(L, event);
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
29 lua_call(L, 1, 2);
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
30 ret = lua_tointeger(L, -2);
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
31 if(lua_isnumber(L, -1)) {
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
32 newTimeout = lua_tonumber(L, -1);
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
33 if(newTimeout <= 0) {
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
34 memset(&cb->timeout, 0, sizeof(arg->timeout));
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
35 } else {
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
36 load_timeval(newTimeout, &cb->timeout);
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
37 }
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
38 }
20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
39 lua_pop(L, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
40 if(ret == -1) {
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
41 freeCallbackArgs(cb, L);
20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
42 } else {
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
43 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
44 int newEvent = ret;
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
45 /* 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
46 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
47 struct timeval *ptv = &cb->timeout;
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
48 if(!cb->timeout.sec && !cb->timeout.usec)
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
49 ptv = NULL;
20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
50 event_del(ev);
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
51 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
52 /* Assume cannot set a new timeout.. */
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
53 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
54 }
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 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
59 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
60 freeCallbackArgs(arg, L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
61 return 0;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
62 }
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
63
22
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
64 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
65 le_callback* cb;
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
66 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
67 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
68 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
69 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
70 lua_setmetatable(L, -2);
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
71
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
72 lua_pushvalue(L, callbackIdx);
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
73 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
74 cb->base = base;
23
897150985f13 Added support for timeouts and timers.
Thomas Harning Jr <harningt@gmail.com>
parents: 22
diff changeset
75 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
76 return cb;
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
77 }
48a109847dce Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents: 21
diff changeset
78
20
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
79 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
80 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
81 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
82 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
83 lua_newtable(L);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
84 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
85 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
86 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
87 lua_pop(L, 1);
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
88 return 0;
71bc2e49366c Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
89 }

mercurial