Add base:method() to return string identifying the current libevent backend in use
[luaevent.git] / src / luaevent.c
blob0c5c37bdd48a748ab6ce3985c950033cad0c1850
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 static int luaevent_base_gc(lua_State* L) {
33 le_base *base = event_base_get(L, 1);
34 if(base->base) {
35 event_base_free(base->base);
36 base->base = NULL;
38 return 0;
41 int getSocketFd(lua_State* L, int idx) {
42 int fd;
43 luaL_checktype(L, idx, LUA_TUSERDATA);
44 lua_getfield(L, idx, "getfd");
45 if(lua_isnil(L, -1))
46 return luaL_error(L, "Socket type missing 'getfd' method");
47 lua_pushvalue(L, idx);
48 lua_call(L, 1, 1);
49 fd = lua_tointeger(L, -1);
50 lua_pop(L, 1);
51 return fd;
54 void load_timeval(double time, struct timeval *tv) {
55 tv->tv_sec = (int)time;
56 tv->tv_usec = (int)(time * 1000000) % 1000000;
59 /* sock, event, callback, timeout */
60 static int luaevent_addevent(lua_State* L) {
61 int fd, event;
62 le_callback* arg = event_callback_push(L, 1, 4);
63 struct timeval *tv = &arg->timeout;
64 if(lua_isnil(L, 2) && lua_isnumber(L, 5)) {
65 fd = -1; /* Per event_timer_set.... */
66 } else {
67 fd = getSocketFd(L, 2);
69 event = luaL_checkinteger(L, 3);
70 if(lua_isnumber(L, 5)) {
71 double time = lua_tonumber(L, 5);
72 load_timeval(time, tv);
73 } else {
74 tv = NULL;
77 /* Setup event... */
78 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
79 event_base_set(arg->base->base, &arg->ev);
80 event_add(&arg->ev, tv);
81 return 1;
84 static int luaevent_loop(lua_State* L) {
85 le_base *base = event_base_get(L, 1);
86 base->loop_L = L;
87 int ret = event_base_loop(base->base, 0);
88 lua_pushinteger(L, ret);
89 return 1;
92 static int luaevent_loopexit(lua_State*L) {
93 le_base *base = event_base_get(L, 1);
94 struct timeval tv = { 0, 0 };
95 if(lua_gettop(L) >= 2) /* Optional timeout before exiting the loop */
96 load_timeval(luaL_checknumber(L, 2), &tv);
97 int ret = event_base_loopexit(base->base, &tv);
98 lua_pushinteger(L, ret);
99 return 1;
102 static int luaevent_method(lua_State* L) {
103 le_base *base = event_base_get(L, 1);
104 lua_pushstring(L, event_base_get_method(base->base));
105 return 1;
108 static luaL_Reg base_funcs[] = {
109 { "addevent", luaevent_addevent },
110 { "loop", luaevent_loop },
111 { "loopexit", luaevent_loopexit },
112 { "method", luaevent_method },
113 { NULL, NULL }
116 static luaL_Reg funcs[] = {
117 { "new", luaevent_newbase },
118 { NULL, NULL }
121 typedef struct {
122 const char* name;
123 int value;
124 } namedInteger;
126 static namedInteger consts[] = {
127 {"LEAVE", -1},
128 {"EV_READ", EV_READ},
129 {"EV_WRITE", EV_WRITE},
130 {"EV_TIMEOUT", EV_TIMEOUT},
131 {"EV_SIGNAL", EV_SIGNAL},
132 {"EV_PERSIST", EV_PERSIST},
133 /* bufferevent */
134 {"EVBUFFER_READ", EVBUFFER_READ},
135 {"EVBUFFER_WRITE", EVBUFFER_WRITE},
136 {"EVBUFFER_EOF", EVBUFFER_EOF},
137 {"EVBUFFER_ERROR", EVBUFFER_ERROR},
138 {"EVBUFFER_TIMEOUT", EVBUFFER_TIMEOUT},
139 {NULL, 0}
142 void setNamedIntegers(lua_State* L, namedInteger* p) {
143 while(p->name) {
144 lua_pushinteger(L, p->value);
145 lua_setfield(L, -2, p->name);
146 p++;
150 /* Verified ok */
151 int luaopen_luaevent_core(lua_State* L) {
152 #ifdef _WIN32
153 WORD wVersionRequested = MAKEWORD(2, 2);
154 WSADATA wsaData;
155 WSAStartup(wVersionRequested, &wsaData);
156 #endif
157 event_init( );
158 /* Register external items */
159 event_callback_register(L);
160 event_buffer_register(L);
161 buffer_event_register(L);
162 lua_settop(L, 0);
163 /* Setup metatable */
164 luaL_newmetatable(L, EVENT_BASE_MT);
165 lua_newtable(L);
166 luaL_register(L, NULL, base_funcs);
167 lua_setfield(L, -2, "__index");
168 lua_pushcfunction(L, luaevent_base_gc);
169 lua_setfield(L, -2, "__gc");
170 lua_pop(L, 1);
172 luaL_register(L, "luaevent.core", funcs);
173 setNamedIntegers(L, consts);
174 return 1;