Merge branch 'feature/travis' into develop
[luaevent.git] / src / event_callback.c
blobafe8773267af2b514420dfd735cd0af46454ea53
1 /* LuaEvent
2 Copyright (C) 2007,2012,2013 Thomas Harning <harningt@gmail.com>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #include "event_callback.h"
24 #include <assert.h>
25 #include <lauxlib.h>
26 #include <string.h>
28 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
30 void freeCallbackArgs(le_callback* arg, lua_State* L) {
31 if(arg->base) {
32 arg->base = NULL;
33 event_del(&arg->ev);
34 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
37 /* le_callback is allocated at the beginning of the coroutine in which it
38 is used, no need to manually de-allocate */
40 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
41 void luaevent_callback(int fd, short event, void* p) {
42 le_callback* cb = p;
43 lua_State* L;
44 int ret;
45 struct timeval new_tv = { 0, 0 };
46 le_base* base;
47 assert(cb);
48 if(!cb->base)
49 return; /* Event has already been collected + destroyed */
50 assert(cb->base->loop_L);
51 L = cb->base->loop_L;
52 lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef);
53 lua_pushinteger(L, event);
54 /* cb->base may be NULL after the pcall, if the event is destroyed */
55 base = cb->base;
56 if(lua_pcall(L, 1, 2, 0))
58 base->errorMessage = luaL_ref(L, LUA_REGISTRYINDEX);
59 event_base_loopbreak(base->base);
60 lua_pop(L, 1);
61 return;
63 if(!cb->base) {
64 lua_pop(L, 2);
65 return; /* event was destroyed during callback */
67 /* If nothing is returned, re-use the old event value */
68 ret = luaL_optinteger(L, -2, event);
69 /* Clone the old timeout value in case a new one wasn't set */
70 memcpy(&new_tv, &cb->timeout, sizeof(new_tv));
71 if(lua_isnumber(L, -1)) {
72 double newTimeout = lua_tonumber(L, -1);
73 if(newTimeout > 0) {
74 load_timeval(newTimeout, &new_tv);
77 lua_pop(L, 2);
78 if(ret == -1) {
79 freeCallbackArgs(cb, L);
80 } else {
81 struct event *ev = &cb->ev;
82 int newEvent = ret;
83 if( newEvent != event || (cb->timeout.tv_sec != new_tv.tv_sec || cb->timeout.tv_usec != new_tv.tv_usec) ) {
84 struct timeval *ptv = &cb->timeout;
85 cb->timeout = new_tv;
86 if(!cb->timeout.tv_sec && !cb->timeout.tv_usec)
87 ptv = NULL;
88 event_del(ev);
89 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb);
90 /* Assume cannot set a new timeout.. */
91 event_add(ev, ptv);
96 static int luaevent_cb_gc(lua_State* L) {
97 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
98 freeCallbackArgs(arg, L);
99 return 0;
102 le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) {
103 le_callback* cb;
104 le_base *base = event_base_get(L, baseIdx);
105 luaL_checktype(L, callbackIdx, LUA_TFUNCTION);
106 cb = lua_newuserdata(L, sizeof(*cb));
107 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
108 lua_setmetatable(L, -2);
110 lua_pushvalue(L, callbackIdx);
111 cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
112 cb->base = base;
113 memset(&cb->timeout, 0, sizeof(cb->timeout));
114 return cb;
117 void event_callback_register(lua_State* L) {
118 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
119 lua_pushcfunction(L, luaevent_cb_gc);
120 lua_setfield(L, -2, "__gc");
121 lua_newtable(L);
122 lua_pushcfunction(L, luaevent_cb_gc);
123 lua_setfield(L, -2, "close");
124 lua_setfield(L, -2, "__index");
125 lua_pop(L, 1);