buffer_event is born, albeit w/ deformities. Can construct instances but not use...
[luaevent.git] / src / luaevent.c
blobbac728b2333c9e9249fb8839bd74d11e0beae6e8
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 {NULL, 0}
112 void setNamedIntegers(lua_State* L, namedInteger* p) {
113 while(p->name) {
114 lua_pushinteger(L, p->value);
115 lua_setfield(L, -2, p->name);
116 p++;
120 /* Verified ok */
121 int luaopen_luaevent_core(lua_State* L) {
122 /* Register external items */
123 event_callback_register(L);
124 event_buffer_register(L);
125 buffer_event_register(L);
126 lua_settop(L, 0);
127 /* Setup metatable */
128 luaL_newmetatable(L, EVENT_BASE_MT);
129 lua_newtable(L);
130 luaL_register(L, NULL, base_funcs);
131 lua_setfield(L, -2, "__index");
132 lua_pushcfunction(L, luaevent_base_gc);
133 lua_setfield(L, -2, "__gc");
134 lua_pop(L, 1);
136 luaL_register(L, "luaevent.core", funcs);
137 setNamedIntegers(L, consts);
138 return 1;