moonbridge
diff moonbridge_io.c @ 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 | |
children | 1a0346580e6d |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/moonbridge_io.c Sun Apr 05 18:01:31 2015 +0200 1.3 @@ -0,0 +1,98 @@ 1.4 + 1.5 +#include <stdlib.h> 1.6 +#include <unistd.h> 1.7 +#include <stdint.h> 1.8 +#include <errno.h> 1.9 +#include <string.h> 1.10 +#include <stdio.h> 1.11 +#include <time.h> 1.12 +#include <sys/time.h> 1.13 +#include <sys/socket.h> 1.14 +#include <sys/select.h> 1.15 + 1.16 +#include <lua.h> 1.17 +#include <lauxlib.h> 1.18 +#include <lualib.h> 1.19 + 1.20 +#define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle" 1.21 +#define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" 1.22 + 1.23 +typedef struct { 1.24 + int fd; 1.25 +} moonbr_io_handle_t; 1.26 + 1.27 +void moonbr_io_pushhandle(lua_State *L, int fd) { 1.28 + moonbr_io_handle_t *handle; 1.29 + handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t)); 1.30 + handle->fd = fd; 1.31 + luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY); 1.32 + lua_setmetatable(L, -2); 1.33 + lua_newtable(L); // uservalue 1.34 + lua_newtable(L); // public 1.35 + luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); 1.36 + lua_setmetatable(L, -2); 1.37 + lua_setfield(L, -2, "public"); 1.38 + lua_setuservalue(L, -2); 1.39 +} 1.40 + 1.41 +static int moonbr_io_handleindex(lua_State *L) { 1.42 + lua_getuservalue(L, 1); 1.43 + lua_getfield(L, -1, "public"); 1.44 + lua_pushvalue(L, 2); 1.45 + lua_gettable(L, -2); 1.46 + return 1; 1.47 +} 1.48 + 1.49 +static int moonbr_io_handlenewindex(lua_State *L) { 1.50 + lua_getuservalue(L, 1); 1.51 + lua_getfield(L, -1, "public"); 1.52 + lua_pushvalue(L, 2); 1.53 + lua_pushvalue(L, 3); 1.54 + lua_settable(L, -3); 1.55 + return 0; 1.56 +} 1.57 + 1.58 +static int moonbr_io_getdummy(lua_State *L) { 1.59 + moonbr_io_pushhandle(L, 1); 1.60 + return 1; 1.61 +} 1.62 + 1.63 +static int moonbr_io_testmethod(lua_State *L) { 1.64 + fprintf(stderr, "DEBUG\n"); 1.65 + return 0; 1.66 +} 1.67 + 1.68 +static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.69 + {"testmethod", moonbr_io_testmethod}, 1.70 + {NULL, NULL} 1.71 +}; 1.72 + 1.73 +static const struct luaL_Reg moonbr_io_handle_metamethods[] = { 1.74 + {"__index", moonbr_io_handleindex}, 1.75 + {"__newindex", moonbr_io_handlenewindex}, 1.76 + {NULL, NULL} 1.77 +}; 1.78 + 1.79 +static const struct luaL_Reg moonbr_io_module_funcs[] = { 1.80 + {"getdummy", moonbr_io_getdummy}, 1.81 + {NULL, NULL} 1.82 +}; 1.83 + 1.84 +int luaopen_moonbridge_io(lua_State *L) { 1.85 + 1.86 + lua_newtable(L); // public metatable 1.87 + lua_newtable(L); // handle methods 1.88 + luaL_setfuncs(L, moonbr_io_handle_methods, 0); 1.89 + lua_setfield(L, -2, "__index"); 1.90 + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); 1.91 + 1.92 + lua_newtable(L); // handle metatable 1.93 + luaL_setfuncs(L, moonbr_io_handle_metamethods, 0); 1.94 + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY); 1.95 + 1.96 + lua_newtable(L); // module 1.97 + luaL_setfuncs(L, moonbr_io_module_funcs, 0); 1.98 + return 1; 1.99 + 1.100 +} 1.101 +