Added EVBUFFER_* constants to luaevent.core
[luaevent.git] / src / luaevent.c
blobb1f57ebeba74dbdd1e719163283df8c2e7f1cea6
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 le_base* event_base_get(lua_State* L, int idx) {
16 return (le_base*)luaL_checkudata(L, idx, EVENT_BASE_MT);
19 int luaevent_newbase(lua_State* L) {
20 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
21 base->loop_L = NULL; /* No running loop */
22 base->base = event_init();
23 luaL_getmetatable(L, EVENT_BASE_MT);
24 lua_setmetatable(L, -2);
25 return 1;
28 static int luaevent_base_gc(lua_State* L) {
29 le_base *base = event_base_get(L, 1);
30 if(base->base) {
31 event_base_free(base->base);
32 base->base = NULL;
34 return 0;
37 int getSocketFd(lua_State* L, int idx) {
38 int fd;
39 luaL_checktype(L, idx, LUA_TUSERDATA);
40 lua_getfield(L, idx, "getfd");
41 if(lua_isnil(L, -1))
42 return luaL_error(L, "Socket type missing 'getfd' method");
43 lua_pushvalue(L, idx);
44 lua_call(L, 1, 1);
45 fd = lua_tointeger(L, -1);
46 lua_pop(L, 1);
47 return fd;
50 void load_timeval(double time, struct timeval *tv) {
51 tv->tv_sec = (int)time;
52 tv->tv_usec = (int)(time * 1000000) % 1000000;
55 /* sock, event, callback, timeout */
56 static int luaevent_addevent(lua_State* L) {
57 int fd, event;
58 le_callback* arg = event_callback_push(L, 1, 4);
59 struct timeval *tv = &arg->timeout;
60 if(lua_isnil(L, 2) && lua_isnumber(L, 5)) {
61 fd = -1; /* Per event_timer_set.... */
62 } else {
63 fd = getSocketFd(L, 2);
65 event = luaL_checkinteger(L, 3);
66 if(lua_isnumber(L, 5)) {
67 double time = lua_tonumber(L, 5);
68 load_timeval(time, tv);
69 } else {
70 tv = NULL;
73 /* Setup event... */
74 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
75 event_base_set(arg->base->base, &arg->ev);
76 event_add(&arg->ev, tv);
77 return 1;
80 static int luaevent_loop(lua_State* L) {
81 le_base *base = event_base_get(L, 1);
82 base->loop_L = L;
83 int ret = event_base_loop(base->base, 0);
84 lua_pushinteger(L, ret);
85 return 1;
88 static luaL_Reg base_funcs[] = {
89 { "addevent", luaevent_addevent },
90 { "loop", luaevent_loop },
91 { NULL, NULL }
94 static luaL_Reg funcs[] = {
95 { "new", luaevent_newbase },
96 { NULL, NULL }
99 typedef struct {
100 const char* name;
101 int value;
102 } namedInteger;
104 static namedInteger consts[] = {
105 {"LEAVE", -1},
106 {"EV_READ", EV_READ},
107 {"EV_WRITE", EV_WRITE},
108 {"EV_TIMEOUT", EV_TIMEOUT},
109 /* bufferevent */
110 {"EVBUFFER_READ", EVBUFFER_READ},
111 {"EVBUFFER_WRITE", EVBUFFER_WRITE},
112 {"EVBUFFER_EOF", EVBUFFER_EOF},
113 {"EVBUFFER_ERROR", EVBUFFER_ERROR},
114 {"EVBUFFER_TIMEOUT", EVBUFFER_TIMEOUT},
115 {NULL, 0}
118 void setNamedIntegers(lua_State* L, namedInteger* p) {
119 while(p->name) {
120 lua_pushinteger(L, p->value);
121 lua_setfield(L, -2, p->name);
122 p++;
126 /* Verified ok */
127 int luaopen_luaevent_core(lua_State* L) {
128 /* Register external items */
129 event_callback_register(L);
130 event_buffer_register(L);
131 buffer_event_register(L);
132 lua_settop(L, 0);
133 /* Setup metatable */
134 luaL_newmetatable(L, EVENT_BASE_MT);
135 lua_newtable(L);
136 luaL_register(L, NULL, base_funcs);
137 lua_setfield(L, -2, "__index");
138 lua_pushcfunction(L, luaevent_base_gc);
139 lua_setfield(L, -2, "__gc");
140 lua_pop(L, 1);
142 luaL_register(L, "luaevent.core", funcs);
143 setNamedIntegers(L, consts);
144 return 1;