Add base:loopexit() method
[luaevent.git] / src / luaevent.c
blob71188a68afb8b67587db603ef2054e76f55c0ae7
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, 10 };
95 int ret = event_base_loopexit(base->base, &tv);
96 lua_pushinteger(L, ret);
97 return 1;
100 static luaL_Reg base_funcs[] = {
101 { "addevent", luaevent_addevent },
102 { "loop", luaevent_loop },
103 { "loopexit", luaevent_loopexit },
104 { NULL, NULL }
107 static luaL_Reg funcs[] = {
108 { "new", luaevent_newbase },
109 { NULL, NULL }
112 typedef struct {
113 const char* name;
114 int value;
115 } namedInteger;
117 static namedInteger consts[] = {
118 {"LEAVE", -1},
119 {"EV_READ", EV_READ},
120 {"EV_WRITE", EV_WRITE},
121 {"EV_TIMEOUT", EV_TIMEOUT},
122 {"EV_SIGNAL", EV_SIGNAL},
123 {"EV_PERSIST", EV_PERSIST},
124 /* bufferevent */
125 {"EVBUFFER_READ", EVBUFFER_READ},
126 {"EVBUFFER_WRITE", EVBUFFER_WRITE},
127 {"EVBUFFER_EOF", EVBUFFER_EOF},
128 {"EVBUFFER_ERROR", EVBUFFER_ERROR},
129 {"EVBUFFER_TIMEOUT", EVBUFFER_TIMEOUT},
130 {NULL, 0}
133 void setNamedIntegers(lua_State* L, namedInteger* p) {
134 while(p->name) {
135 lua_pushinteger(L, p->value);
136 lua_setfield(L, -2, p->name);
137 p++;
141 /* Verified ok */
142 int luaopen_luaevent_core(lua_State* L) {
143 #ifdef _WIN32
144 WORD wVersionRequested = MAKEWORD(2, 2);
145 WSADATA wsaData;
146 WSAStartup(wVersionRequested, &wsaData);
147 #endif
148 event_init( );
149 /* Register external items */
150 event_callback_register(L);
151 event_buffer_register(L);
152 buffer_event_register(L);
153 lua_settop(L, 0);
154 /* Setup metatable */
155 luaL_newmetatable(L, EVENT_BASE_MT);
156 lua_newtable(L);
157 luaL_register(L, NULL, base_funcs);
158 lua_setfield(L, -2, "__index");
159 lua_pushcfunction(L, luaevent_base_gc);
160 lua_setfield(L, -2, "__gc");
161 lua_pop(L, 1);
163 luaL_register(L, "luaevent.core", funcs);
164 setNamedIntegers(L, consts);
165 return 1;