base:addevent(): Accept integer as fd parameter
[luaevent.git] / src / luaevent.c
bloba8d5f7dd3251fe065fe1f054420f30511af4490d
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
4 #include "luaevent.h"
5 #include "event_callback.h"
6 #include "event_buffer.h"
7 #include "buffer_event.h"
9 #include <lua.h>
10 #include <lauxlib.h>
11 #include <assert.h>
13 #define EVENT_BASE_MT "EVENT_BASE_MT"
15 #ifdef _WIN32
16 #include <winsock2.h>
17 #endif
19 le_base* event_base_get(lua_State* L, int idx) {
20 return (le_base*)luaL_checkudata(L, idx, EVENT_BASE_MT);
23 int luaevent_newbase(lua_State* L) {
24 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
25 base->loop_L = NULL; /* No running loop */
26 base->base = event_init();
27 luaL_getmetatable(L, EVENT_BASE_MT);
28 lua_setmetatable(L, -2);
29 return 1;
32 int luaevent_libevent_version(lua_State* L) {
33 lua_pushstring(L, event_get_version());
34 return 1;
37 static int luaevent_base_gc(lua_State* L) {
38 le_base *base = event_base_get(L, 1);
39 if(base->base) {
40 event_base_free(base->base);
41 base->base = NULL;
43 return 0;
46 int getSocketFd(lua_State* L, int idx) {
47 int fd;
48 if(lua_isnumber(L, idx)) {
49 fd = lua_tonumber(L, idx);
50 } else {
51 luaL_checktype(L, idx, LUA_TUSERDATA);
52 lua_getfield(L, idx, "getfd");
53 if(lua_isnil(L, -1))
54 return luaL_error(L, "Socket type missing 'getfd' method");
55 lua_pushvalue(L, idx);
56 lua_call(L, 1, 1);
57 fd = lua_tointeger(L, -1);
58 lua_pop(L, 1);
60 return fd;
63 void load_timeval(double time, struct timeval *tv) {
64 tv->tv_sec = (int)time;
65 tv->tv_usec = (int)(time * 1000000) % 1000000;
68 /* sock, event, callback, timeout */
69 static int luaevent_addevent(lua_State* L) {
70 int fd, event;
71 le_callback* arg = event_callback_push(L, 1, 4);
72 struct timeval *tv = &arg->timeout;
73 if(lua_isnil(L, 2) && lua_isnumber(L, 5)) {
74 fd = -1; /* Per event_timer_set.... */
75 } else {
76 fd = getSocketFd(L, 2);
78 event = luaL_checkinteger(L, 3);
79 if(lua_isnumber(L, 5)) {
80 double time = lua_tonumber(L, 5);
81 load_timeval(time, tv);
82 } else {
83 tv = NULL;
86 /* Setup event... */
87 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
88 event_base_set(arg->base->base, &arg->ev);
89 event_add(&arg->ev, tv);
90 return 1;
93 static int luaevent_loop(lua_State* L) {
94 le_base *base = event_base_get(L, 1);
95 base->loop_L = L;
96 int ret = event_base_loop(base->base, 0);
97 lua_pushinteger(L, ret);
98 return 1;
101 static int luaevent_loopexit(lua_State*L) {
102 le_base *base = event_base_get(L, 1);
103 struct timeval tv = { 0, 0 };
104 if(lua_gettop(L) >= 2) /* Optional timeout before exiting the loop */
105 load_timeval(luaL_checknumber(L, 2), &tv);
106 int ret = event_base_loopexit(base->base, &tv);
107 lua_pushinteger(L, ret);
108 return 1;
111 static int luaevent_method(lua_State* L) {
112 le_base *base = event_base_get(L, 1);
113 lua_pushstring(L, event_base_get_method(base->base));
114 return 1;
117 static luaL_Reg base_funcs[] = {
118 { "addevent", luaevent_addevent },
119 { "loop", luaevent_loop },
120 { "loopexit", luaevent_loopexit },
121 { "method", luaevent_method },
122 { NULL, NULL }
125 static luaL_Reg funcs[] = {
126 { "new", luaevent_newbase },
127 { "libevent_version", luaevent_libevent_version },
128 { NULL, NULL }
131 typedef struct {
132 const char* name;
133 int value;
134 } namedInteger;
136 static namedInteger consts[] = {
137 {"LEAVE", -1},
138 {"EV_READ", EV_READ},
139 {"EV_WRITE", EV_WRITE},
140 {"EV_TIMEOUT", EV_TIMEOUT},
141 {"EV_SIGNAL", EV_SIGNAL},
142 {"EV_PERSIST", EV_PERSIST},
143 /* bufferevent */
144 {"EVBUFFER_READ", EVBUFFER_READ},
145 {"EVBUFFER_WRITE", EVBUFFER_WRITE},
146 {"EVBUFFER_EOF", EVBUFFER_EOF},
147 {"EVBUFFER_ERROR", EVBUFFER_ERROR},
148 {"EVBUFFER_TIMEOUT", EVBUFFER_TIMEOUT},
149 {NULL, 0}
152 void setNamedIntegers(lua_State* L, namedInteger* p) {
153 while(p->name) {
154 lua_pushinteger(L, p->value);
155 lua_setfield(L, -2, p->name);
156 p++;
160 /* Verified ok */
161 int luaopen_luaevent_core(lua_State* L) {
162 #ifdef _WIN32
163 WORD wVersionRequested = MAKEWORD(2, 2);
164 WSADATA wsaData;
165 WSAStartup(wVersionRequested, &wsaData);
166 #endif
167 event_init( );
168 /* Register external items */
169 event_callback_register(L);
170 event_buffer_register(L);
171 buffer_event_register(L);
172 lua_settop(L, 0);
173 /* Setup metatable */
174 luaL_newmetatable(L, EVENT_BASE_MT);
175 lua_newtable(L);
176 luaL_register(L, NULL, base_funcs);
177 lua_setfield(L, -2, "__index");
178 lua_pushcfunction(L, luaevent_base_gc);
179 lua_setfield(L, -2, "__gc");
180 lua_pop(L, 1);
182 luaL_register(L, "luaevent.core", funcs);
183 setNamedIntegers(L, consts);
184 return 1;