core: updates to MIT-style license and properly includes in file heading
[luaevent.git] / src / event_callback.c
blob6d55d6a59ec1b169cf29ffe7effdd55827ccad9d
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 assert(cb);
47 if(!cb->base)
48 return; /* Event has already been collected + destroyed */
49 assert(cb->base->loop_L);
50 L = cb->base->loop_L;
51 lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef);
52 lua_pushinteger(L, event);
53 if(lua_pcall(L, 1, 2, 0))
55 cb->base->errorMessage = luaL_ref(L, LUA_REGISTRYINDEX);
56 event_base_loopbreak(cb->base->base);
57 lua_pop(L, 1);
58 return;
60 if(!cb->base) {
61 lua_pop(L, 2);
62 return; /* event was destroyed during callback */
64 /* If nothing is returned, re-use the old event value */
65 ret = luaL_optinteger(L, -2, event);
66 /* Clone the old timeout value in case a new one wasn't set */
67 memcpy(&new_tv, &cb->timeout, sizeof(new_tv));
68 if(lua_isnumber(L, -1)) {
69 double newTimeout = lua_tonumber(L, -1);
70 if(newTimeout > 0) {
71 load_timeval(newTimeout, &new_tv);
74 lua_pop(L, 2);
75 if(ret == -1) {
76 freeCallbackArgs(cb, L);
77 } else {
78 struct event *ev = &cb->ev;
79 int newEvent = ret;
80 if( newEvent != event || (cb->timeout.tv_sec != new_tv.tv_sec || cb->timeout.tv_usec != new_tv.tv_usec) ) {
81 struct timeval *ptv = &cb->timeout;
82 cb->timeout = new_tv;
83 if(!cb->timeout.tv_sec && !cb->timeout.tv_usec)
84 ptv = NULL;
85 event_del(ev);
86 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb);
87 /* Assume cannot set a new timeout.. */
88 event_add(ev, ptv);
93 static int luaevent_cb_gc(lua_State* L) {
94 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
95 freeCallbackArgs(arg, L);
96 return 0;
99 le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) {
100 le_callback* cb;
101 le_base *base = event_base_get(L, baseIdx);
102 luaL_checktype(L, callbackIdx, LUA_TFUNCTION);
103 cb = lua_newuserdata(L, sizeof(*cb));
104 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
105 lua_setmetatable(L, -2);
107 lua_pushvalue(L, callbackIdx);
108 cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
109 cb->base = base;
110 memset(&cb->timeout, 0, sizeof(cb->timeout));
111 return cb;
114 void event_callback_register(lua_State* L) {
115 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
116 lua_pushcfunction(L, luaevent_cb_gc);
117 lua_setfield(L, -2, "__gc");
118 lua_newtable(L);
119 lua_pushcfunction(L, luaevent_cb_gc);
120 lua_setfield(L, -2, "close");
121 lua_setfield(L, -2, "__index");
122 lua_pop(L, 1);