moonbridge
changeset 84:8a6e2a80fcad
Refined interface of I/O library to directly support (previously opened) sockets
author | jbe |
---|---|
date | Mon Apr 06 15:41:37 2015 +0200 (2015-04-06) |
parents | 697cdf8e2000 |
children | ef552870f1b6 |
files | moonbridge_io.c moonbridge_io.h |
line diff
1.1 --- a/moonbridge_io.c Mon Apr 06 15:02:15 2015 +0200 1.2 +++ b/moonbridge_io.c Mon Apr 06 15:41:37 2015 +0200 1.3 @@ -28,6 +28,7 @@ 1.4 1.5 typedef struct { 1.6 int fd; 1.7 + int issocket; 1.8 int nonblocking; 1.9 int writeerr; 1.10 size_t writeleft; 1.11 @@ -43,18 +44,6 @@ 1.12 char writebuf[MOONBR_IO_WRITEBUFLEN]; 1.13 } moonbr_io_handle_t; 1.14 1.15 -int moonbr_io_isclosed(lua_State *L, int idx) { 1.16 - moonbr_io_handle_t *handle; 1.17 - handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY); 1.18 - return handle->fd < 0; 1.19 -} 1.20 - 1.21 -void moonbr_io_markclosed(lua_State *L, int idx) { 1.22 - moonbr_io_handle_t *handle; 1.23 - handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY); 1.24 - handle->fd = -1; 1.25 -} 1.26 - 1.27 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) { 1.28 if (handle->nonblocking != nonblocking) { 1.29 int flags; 1.30 @@ -87,16 +76,14 @@ 1.31 return 2; 1.32 } 1.33 moonbr_io_handle_set_nonblocking(L, handle, nonblocking); 1.34 - if (!flush) top = lua_gettop(L); 1.35 + top = lua_gettop(L); 1.36 lua_getuservalue(L, 1); 1.37 lua_getfield(L, -1, "writebuf"); 1.38 - if (!flush) { 1.39 - for (i=2; i<=top; i++) { 1.40 - luaL_checklstring(L, i, &strlen); 1.41 - lua_pushvalue(L, i); 1.42 - lua_rawseti(L, -2, handle->writeqin++); 1.43 - handle->writeleft += strlen; 1.44 - } 1.45 + for (i=2; i<=top; i++) { 1.46 + luaL_checklstring(L, i, &strlen); 1.47 + lua_pushvalue(L, i); 1.48 + lua_rawseti(L, -2, handle->writeqin++); 1.49 + handle->writeleft += strlen; 1.50 } 1.51 while (handle->writeqout != handle->writeqin) { 1.52 lua_rawgeti(L, -1, handle->writeqout); 1.53 @@ -193,11 +180,11 @@ 1.54 return moonbr_io_write_impl(L, 1, 1); 1.55 } 1.56 1.57 -static int moonbr_io_close(lua_State *L) { 1.58 +static int moonbr_io_close_impl(lua_State *L, int clean) { 1.59 moonbr_io_handle_t *handle; 1.60 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.61 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle"); 1.62 - if (handle->writeleft) { 1.63 + if (clean && handle->writeleft) { 1.64 lua_pushcfunction(L, moonbr_io_flush); 1.65 lua_pushvalue(L, 1); 1.66 lua_call(L, 1, 2); 1.67 @@ -217,12 +204,22 @@ 1.68 handle->fd = -1; 1.69 lua_pushboolean(L, 1); 1.70 return 1; 1.71 + 1.72 } 1.73 1.74 -void moonbr_io_pushhandle(lua_State *L, int fd, int gc_idx) { 1.75 +static int moonbr_io_reset(lua_State *L) { 1.76 + return moonbr_io_close_impl(L, 0); 1.77 +} 1.78 + 1.79 +static int moonbr_io_close(lua_State *L) { 1.80 + return moonbr_io_close_impl(L, 1); 1.81 +} 1.82 + 1.83 +void moonbr_io_pushhandle(lua_State *L, int fd, int issocket) { 1.84 moonbr_io_handle_t *handle; 1.85 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t)); 1.86 handle->fd = fd; 1.87 + handle->issocket = issocket; 1.88 handle->nonblocking = -1; 1.89 handle->writeerr = 0; 1.90 handle->writeleft = 0; 1.91 @@ -235,9 +232,6 @@ 1.92 lua_newtable(L); // uservalue 1.93 lua_newtable(L); 1.94 lua_setfield(L, -2, "writebuf"); 1.95 - if (gc_idx) lua_pushvalue(L, gc_idx); 1.96 - else lua_pushcfunction(L, moonbr_io_close); 1.97 - lua_setfield(L, -2, "gc"); 1.98 lua_newtable(L); // public 1.99 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); 1.100 lua_setmetatable(L, -2); 1.101 @@ -282,6 +276,7 @@ 1.102 } 1.103 1.104 static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.105 + {"reset", moonbr_io_reset}, 1.106 {"close", moonbr_io_close}, 1.107 {"write", moonbr_io_write}, 1.108 {"write_nb", moonbr_io_write_nb},
2.1 --- a/moonbridge_io.h Mon Apr 06 15:02:15 2015 +0200 2.2 +++ b/moonbridge_io.h Mon Apr 06 15:41:37 2015 +0200 2.3 @@ -1,6 +1,4 @@ 2.4 2.5 -void moonbr_io_pushhandle(lua_State *L, int fd, int gc_idx); 2.6 -int moonbr_io_isclosed(lua_State *L, int idx); 2.7 -void moonbr_io_markclosed(lua_State *L, int idx); 2.8 +void moonbr_io_pushhandle(lua_State *L, int fd, int issocket); 2.9 int luaopen_moonbridge_io(lua_State *L); 2.10