moonbridge
changeset 79:22dbb9d09f02
Stub for new non-blocking I/O library
author | jbe |
---|---|
date | Sun Apr 05 18:01:31 2015 +0200 (2015-04-05) |
parents | 0ec070d6f5d9 |
children | 1a0346580e6d |
files | Makefile moonbridge.c moonbridge_io.c moonbridge_io.h |
line diff
1.1 --- a/Makefile Sun Apr 05 15:15:06 2015 +0200 1.2 +++ b/Makefile Sun Apr 05 18:01:31 2015 +0200 1.3 @@ -50,11 +50,17 @@ 1.4 MOONBR_LUA_CPATH_DEFINE = "-DMOONBR_LUA_CPATH=\"$(MOONBR_LUA_CPATH)\"" 1.5 .endif 1.6 1.7 -all:: moonbridge 1.8 +all:: moonbridge moonbridge_io.so 1.9 + 1.10 +moonbridge: moonbridge.c moonbridge_io.h moonbridge_io.o 1.11 + cc -Wall -Wno-unused-result -O2 -Wl,-E -I $(LUA_INCLUDE) -L $(LUA_LIBDIR) -o moonbridge $(MOONBR_LUA_PATH_DEFINE) $(MOONBR_LUA_CPATH_DEFINE) moonbridge.c -lm -l$(LUA_LIBRARY) $(UTIL_FLAGS) moonbridge_io.o 1.12 1.13 -moonbridge: moonbridge.c 1.14 - cc -Wall -Wno-unused-result -O2 -Wl,-E -I $(LUA_INCLUDE) -L $(LUA_LIBDIR) -o moonbridge $(MOONBR_LUA_PATH_DEFINE) $(MOONBR_LUA_CPATH_DEFINE) moonbridge.c -lm -l$(LUA_LIBRARY) $(UTIL_FLAGS) 1.15 +moonbridge_io.o: moonbridge_io.c 1.16 + cc -c -Wall -O2 -fPIC -I $(LUA_INCLUDE) -o moonbridge_io.o moonbridge_io.c 1.17 + 1.18 +moonbridge_io.so: moonbridge_io.o 1.19 + ld -shared -o moonbridge_io.so moonbridge_io.o 1.20 1.21 clean:: 1.22 - rm -f moonbridge 1.23 + rm -f moonbridge moonbridge_io.o moonbridge_io.so 1.24
2.1 --- a/moonbridge.c Sun Apr 05 15:15:06 2015 +0200 2.2 +++ b/moonbridge.c Sun Apr 05 18:01:31 2015 +0200 2.3 @@ -67,6 +67,11 @@ 2.4 #include <lualib.h> 2.5 2.6 2.7 +/*** Include directive for moonbridge_io library ***/ 2.8 + 2.9 +#include "moonbridge_io.h" 2.10 + 2.11 + 2.12 /*** Constants ***/ 2.13 2.14 /* Backlog option for listen() call */ 2.15 @@ -2758,6 +2763,14 @@ 2.16 lua_pushliteral(L, MOONBR_VERSION_STRING); 2.17 lua_setglobal(L, "_MOONBRIDGE_VERSION"); 2.18 luaL_openlibs(L); 2.19 + luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 0); 2.20 + lua_getglobal(L, "io"); 2.21 + for (lua_pushnil(L); lua_next(L, -3); lua_pop(L, 1)) { 2.22 + lua_pushvalue(L, -2); 2.23 + lua_pushvalue(L, -2); 2.24 + lua_settable(L, -5); 2.25 + } 2.26 + lua_pop(L, 2); 2.27 #ifdef MOONBR_LUA_PATH 2.28 moonbr_modify_path(L, "path", MOONBR_LUA_PATH); 2.29 #endif
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/moonbridge_io.c Sun Apr 05 18:01:31 2015 +0200 3.3 @@ -0,0 +1,98 @@ 3.4 + 3.5 +#include <stdlib.h> 3.6 +#include <unistd.h> 3.7 +#include <stdint.h> 3.8 +#include <errno.h> 3.9 +#include <string.h> 3.10 +#include <stdio.h> 3.11 +#include <time.h> 3.12 +#include <sys/time.h> 3.13 +#include <sys/socket.h> 3.14 +#include <sys/select.h> 3.15 + 3.16 +#include <lua.h> 3.17 +#include <lauxlib.h> 3.18 +#include <lualib.h> 3.19 + 3.20 +#define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle" 3.21 +#define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" 3.22 + 3.23 +typedef struct { 3.24 + int fd; 3.25 +} moonbr_io_handle_t; 3.26 + 3.27 +void moonbr_io_pushhandle(lua_State *L, int fd) { 3.28 + moonbr_io_handle_t *handle; 3.29 + handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t)); 3.30 + handle->fd = fd; 3.31 + luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY); 3.32 + lua_setmetatable(L, -2); 3.33 + lua_newtable(L); // uservalue 3.34 + lua_newtable(L); // public 3.35 + luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); 3.36 + lua_setmetatable(L, -2); 3.37 + lua_setfield(L, -2, "public"); 3.38 + lua_setuservalue(L, -2); 3.39 +} 3.40 + 3.41 +static int moonbr_io_handleindex(lua_State *L) { 3.42 + lua_getuservalue(L, 1); 3.43 + lua_getfield(L, -1, "public"); 3.44 + lua_pushvalue(L, 2); 3.45 + lua_gettable(L, -2); 3.46 + return 1; 3.47 +} 3.48 + 3.49 +static int moonbr_io_handlenewindex(lua_State *L) { 3.50 + lua_getuservalue(L, 1); 3.51 + lua_getfield(L, -1, "public"); 3.52 + lua_pushvalue(L, 2); 3.53 + lua_pushvalue(L, 3); 3.54 + lua_settable(L, -3); 3.55 + return 0; 3.56 +} 3.57 + 3.58 +static int moonbr_io_getdummy(lua_State *L) { 3.59 + moonbr_io_pushhandle(L, 1); 3.60 + return 1; 3.61 +} 3.62 + 3.63 +static int moonbr_io_testmethod(lua_State *L) { 3.64 + fprintf(stderr, "DEBUG\n"); 3.65 + return 0; 3.66 +} 3.67 + 3.68 +static const struct luaL_Reg moonbr_io_handle_methods[] = { 3.69 + {"testmethod", moonbr_io_testmethod}, 3.70 + {NULL, NULL} 3.71 +}; 3.72 + 3.73 +static const struct luaL_Reg moonbr_io_handle_metamethods[] = { 3.74 + {"__index", moonbr_io_handleindex}, 3.75 + {"__newindex", moonbr_io_handlenewindex}, 3.76 + {NULL, NULL} 3.77 +}; 3.78 + 3.79 +static const struct luaL_Reg moonbr_io_module_funcs[] = { 3.80 + {"getdummy", moonbr_io_getdummy}, 3.81 + {NULL, NULL} 3.82 +}; 3.83 + 3.84 +int luaopen_moonbridge_io(lua_State *L) { 3.85 + 3.86 + lua_newtable(L); // public metatable 3.87 + lua_newtable(L); // handle methods 3.88 + luaL_setfuncs(L, moonbr_io_handle_methods, 0); 3.89 + lua_setfield(L, -2, "__index"); 3.90 + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); 3.91 + 3.92 + lua_newtable(L); // handle metatable 3.93 + luaL_setfuncs(L, moonbr_io_handle_metamethods, 0); 3.94 + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY); 3.95 + 3.96 + lua_newtable(L); // module 3.97 + luaL_setfuncs(L, moonbr_io_module_funcs, 0); 3.98 + return 1; 3.99 + 3.100 +} 3.101 +