# HG changeset patch # User jbe # Date 1428327697 -7200 # Node ID 8a6e2a80fcada6806ff331b15f83a1a82c84b4a5 # Parent 697cdf8e20003b704dc90a4f6f79769fe79b7edb Refined interface of I/O library to directly support (previously opened) sockets diff -r 697cdf8e2000 -r 8a6e2a80fcad moonbridge_io.c --- a/moonbridge_io.c Mon Apr 06 15:02:15 2015 +0200 +++ b/moonbridge_io.c Mon Apr 06 15:41:37 2015 +0200 @@ -28,6 +28,7 @@ typedef struct { int fd; + int issocket; int nonblocking; int writeerr; size_t writeleft; @@ -43,18 +44,6 @@ char writebuf[MOONBR_IO_WRITEBUFLEN]; } moonbr_io_handle_t; -int moonbr_io_isclosed(lua_State *L, int idx) { - moonbr_io_handle_t *handle; - handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY); - return handle->fd < 0; -} - -void moonbr_io_markclosed(lua_State *L, int idx) { - moonbr_io_handle_t *handle; - handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY); - handle->fd = -1; -} - static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) { if (handle->nonblocking != nonblocking) { int flags; @@ -87,16 +76,14 @@ return 2; } moonbr_io_handle_set_nonblocking(L, handle, nonblocking); - if (!flush) top = lua_gettop(L); + top = lua_gettop(L); lua_getuservalue(L, 1); lua_getfield(L, -1, "writebuf"); - if (!flush) { - for (i=2; i<=top; i++) { - luaL_checklstring(L, i, &strlen); - lua_pushvalue(L, i); - lua_rawseti(L, -2, handle->writeqin++); - handle->writeleft += strlen; - } + for (i=2; i<=top; i++) { + luaL_checklstring(L, i, &strlen); + lua_pushvalue(L, i); + lua_rawseti(L, -2, handle->writeqin++); + handle->writeleft += strlen; } while (handle->writeqout != handle->writeqin) { lua_rawgeti(L, -1, handle->writeqout); @@ -193,11 +180,11 @@ return moonbr_io_write_impl(L, 1, 1); } -static int moonbr_io_close(lua_State *L) { +static int moonbr_io_close_impl(lua_State *L, int clean) { moonbr_io_handle_t *handle; handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle"); - if (handle->writeleft) { + if (clean && handle->writeleft) { lua_pushcfunction(L, moonbr_io_flush); lua_pushvalue(L, 1); lua_call(L, 1, 2); @@ -217,12 +204,22 @@ handle->fd = -1; lua_pushboolean(L, 1); return 1; + } -void moonbr_io_pushhandle(lua_State *L, int fd, int gc_idx) { +static int moonbr_io_reset(lua_State *L) { + return moonbr_io_close_impl(L, 0); +} + +static int moonbr_io_close(lua_State *L) { + return moonbr_io_close_impl(L, 1); +} + +void moonbr_io_pushhandle(lua_State *L, int fd, int issocket) { moonbr_io_handle_t *handle; handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t)); handle->fd = fd; + handle->issocket = issocket; handle->nonblocking = -1; handle->writeerr = 0; handle->writeleft = 0; @@ -235,9 +232,6 @@ lua_newtable(L); // uservalue lua_newtable(L); lua_setfield(L, -2, "writebuf"); - if (gc_idx) lua_pushvalue(L, gc_idx); - else lua_pushcfunction(L, moonbr_io_close); - lua_setfield(L, -2, "gc"); lua_newtable(L); // public luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); lua_setmetatable(L, -2); @@ -282,6 +276,7 @@ } static const struct luaL_Reg moonbr_io_handle_methods[] = { + {"reset", moonbr_io_reset}, {"close", moonbr_io_close}, {"write", moonbr_io_write}, {"write_nb", moonbr_io_write_nb}, diff -r 697cdf8e2000 -r 8a6e2a80fcad moonbridge_io.h --- a/moonbridge_io.h Mon Apr 06 15:02:15 2015 +0200 +++ b/moonbridge_io.h Mon Apr 06 15:41:37 2015 +0200 @@ -1,6 +1,4 @@ -void moonbr_io_pushhandle(lua_State *L, int fd, int gc_idx); -int moonbr_io_isclosed(lua_State *L, int idx); -void moonbr_io_markclosed(lua_State *L, int idx); +void moonbr_io_pushhandle(lua_State *L, int fd, int issocket); int luaopen_moonbridge_io(lua_State *L);