base: add patch to get luaevent working on mingw
[luaevent.git] / src / luaevent.c
blobd352cdc489e6a3e247beb8111f0922932c4c87c4
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"
8 #include <lua.h>
9 #include <lauxlib.h>
10 #include <assert.h>
12 #define EVENT_BASE_MT "EVENT_BASE_MT"
14 #ifdef _WIN32
15 #include <winsock2.h>
16 #endif
18 le_base* event_base_get(lua_State* L, int idx) {
19 return (le_base*)luaL_checkudata(L, idx, EVENT_BASE_MT);
22 int luaevent_newbase(lua_State* L) {
23 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
24 base->loop_L = NULL; /* No running loop */
25 base->base = event_init();
26 luaL_getmetatable(L, EVENT_BASE_MT);
27 lua_setmetatable(L, -2);
28 return 1;
31 static int luaevent_base_gc(lua_State* L) {
32 le_base *base = event_base_get(L, 1);
33 if(base->base) {
34 event_base_free(base->base);
35 base->base = NULL;
37 return 0;
40 int getSocketFd(lua_State* L, int idx) {
41 int fd;
42 luaL_checktype(L, idx, LUA_TUSERDATA);
43 lua_getfield(L, idx, "getfd");
44 if(lua_isnil(L, -1))
45 return luaL_error(L, "Socket type missing 'getfd' method");
46 lua_pushvalue(L, idx);
47 lua_call(L, 1, 1);
48 fd = lua_tointeger(L, -1);
49 lua_pop(L, 1);
50 return fd;
53 void load_timeval(double time, struct timeval *tv) {
54 tv->tv_sec = (int)time;
55 tv->tv_usec = (int)(time * 1000000) % 1000000;
58 /* sock, event, callback, timeout */
59 static int luaevent_addevent(lua_State* L) {
60 int fd, event;
61 le_callback* arg = event_callback_push(L, 1, 4);
62 struct timeval *tv = &arg->timeout;
63 if(lua_isnil(L, 2) && lua_isnumber(L, 5)) {
64 fd = -1; /* Per event_timer_set.... */
65 } else {
66 fd = getSocketFd(L, 2);
68 event = luaL_checkinteger(L, 3);
69 if(lua_isnumber(L, 5)) {
70 double time = lua_tonumber(L, 5);
71 load_timeval(time, tv);
72 } else {
73 tv = NULL;
76 /* Setup event... */
77 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
78 event_base_set(arg->base->base, &arg->ev);
79 event_add(&arg->ev, tv);
80 return 1;
83 static int luaevent_loop(lua_State* L) {
84 le_base *base = event_base_get(L, 1);
85 base->loop_L = L;
86 int ret = event_base_loop(base->base, 0);
87 lua_pushinteger(L, ret);
88 return 1;
91 static luaL_Reg base_funcs[] = {
92 { "addevent", luaevent_addevent },
93 { "loop", luaevent_loop },
94 { NULL, NULL }
97 static luaL_Reg funcs[] = {
98 { "new", luaevent_newbase },
99 { NULL, NULL }
102 typedef struct {
103 const char* name;
104 int value;
105 } namedInteger;
107 static namedInteger consts[] = {
108 {"LEAVE", -1},
109 {"EV_READ", EV_READ},
110 {"EV_WRITE", EV_WRITE},
111 {"EV_TIMEOUT", EV_TIMEOUT},
112 {NULL, 0}
115 void setNamedIntegers(lua_State* L, namedInteger* p) {
116 while(p->name) {
117 lua_pushinteger(L, p->value);
118 lua_setfield(L, -2, p->name);
119 p++;
123 /* Verified ok */
124 int luaopen_luaevent_core(lua_State* L) {
125 #ifdef _WIN32
126 WORD wVersionRequested = MAKEWORD(2, 2);
127 WSADATA wsaData;
128 WSAStartup(wVersionRequested, &wsaData);
129 #endif
130 event_init( );
131 /* Register external items */
132 event_callback_register(L);
133 event_buffer_register(L);
134 /* Setup metatable */
135 luaL_newmetatable(L, EVENT_BASE_MT);
136 lua_newtable(L);
137 luaL_register(L, NULL, base_funcs);
138 lua_setfield(L, -2, "__index");
139 lua_pushcfunction(L, luaevent_base_gc);
140 lua_setfield(L, -2, "__gc");
141 lua_pop(L, 1);
143 luaL_register(L, "luaevent.core", funcs);
144 setNamedIntegers(L, consts);
145 return 1;