moonbridge
changeset 87:1d91c6eedf18
Close and reset method for I/O library
author | jbe |
---|---|
date | Tue Apr 07 01:17:55 2015 +0200 (2015-04-07) |
parents | 9fa3a36733ff |
children | fca51922b708 |
files | moonbridge_io.c |
line diff
1.1 --- a/moonbridge_io.c Tue Apr 07 00:47:30 2015 +0200 1.2 +++ b/moonbridge_io.c Tue Apr 07 01:17:55 2015 +0200 1.3 @@ -61,6 +61,19 @@ 1.4 } 1.5 } 1.6 1.7 +static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) { 1.8 + struct linger lingerval = { 0, }; 1.9 + if (!handle->issocket) return; 1.10 + if (timeout >= 0) { 1.11 + lingerval.l_onoff = 1; 1.12 + lingerval.l_linger = timeout; 1.13 + } 1.14 + if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) { 1.15 + moonbr_io_errmsg(); 1.16 + luaL_error(L, "Unexpected error in setsockopt call: %s", errmsg); 1.17 + } 1.18 +} 1.19 + 1.20 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) { 1.21 moonbr_io_handle_t *handle; 1.22 lua_Integer maxread; 1.23 @@ -264,19 +277,24 @@ 1.24 return moonbr_io_write_impl(L, 1, 1); 1.25 } 1.26 1.27 -static int moonbr_io_close_impl(lua_State *L, int clean) { 1.28 +static int moonbr_io_close(lua_State *L) { 1.29 moonbr_io_handle_t *handle; 1.30 + int timeout; 1.31 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.32 + timeout = luaL_optinteger(L, 2, -1); 1.33 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle"); 1.34 - if (clean && handle->writeleft) { 1.35 - lua_pushcfunction(L, moonbr_io_flush); 1.36 - lua_pushvalue(L, 1); 1.37 - lua_call(L, 1, 2); 1.38 - if (!lua_toboolean(L, -2)) { 1.39 - close(handle->fd); 1.40 - handle->fd = -1; 1.41 - return 2; 1.42 + if (timeout != 0) { 1.43 + if (handle->writeleft) { 1.44 + lua_pushcfunction(L, moonbr_io_flush); 1.45 + lua_pushvalue(L, 1); 1.46 + lua_call(L, 1, 2); 1.47 + if (!lua_toboolean(L, -2)) { 1.48 + close(handle->fd); 1.49 + handle->fd = -1; 1.50 + return 2; 1.51 + } 1.52 } 1.53 + moonbr_io_handle_set_linger(L, handle, timeout); 1.54 } 1.55 if (close(handle->fd)) { 1.56 moonbr_io_errmsg(); 1.57 @@ -292,11 +310,13 @@ 1.58 } 1.59 1.60 static int moonbr_io_reset(lua_State *L) { 1.61 - return moonbr_io_close_impl(L, 0); 1.62 -} 1.63 - 1.64 -static int moonbr_io_close(lua_State *L) { 1.65 - return moonbr_io_close_impl(L, 1); 1.66 + luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.67 + lua_settop(L, 1); 1.68 + lua_pushcfunction(L, moonbr_io_close); 1.69 + lua_insert(L, 1); 1.70 + lua_pushinteger(L, 0); 1.71 + lua_call(L, 2, LUA_MULTRET); 1.72 + return lua_gettop(L); 1.73 } 1.74 1.75 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket) { 1.76 @@ -313,6 +333,7 @@ 1.77 handle->writeqout = 0; 1.78 handle->writeqoff = 0; 1.79 handle->writebufcnt = 0; 1.80 + moonbr_io_handle_set_linger(L, handle, 0); 1.81 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY); 1.82 lua_setmetatable(L, -2); 1.83 lua_newtable(L); // uservalue 1.84 @@ -344,23 +365,6 @@ 1.85 return 0; 1.86 } 1.87 1.88 -static int moonbr_io_handlegc(lua_State *L) { 1.89 - moonbr_io_handle_t *handle; 1.90 - handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.91 - if (handle->fd >= 0) { 1.92 - lua_getuservalue(L, 1); 1.93 - lua_getfield(L, -1, "gc"); 1.94 - lua_pushvalue(L, 1); 1.95 - lua_call(L, 1, 0); 1.96 - } 1.97 - return 0; 1.98 -} 1.99 - 1.100 -static int moonbr_io_getdummy(lua_State *L) { 1.101 - moonbr_io_pushhandle(L, 0, 0); 1.102 - return 1; 1.103 -} 1.104 - 1.105 static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.106 {"read", moonbr_io_read}, 1.107 {"read_nb", moonbr_io_read_nb}, 1.108 @@ -370,20 +374,19 @@ 1.109 {"write_nb", moonbr_io_write_nb}, 1.110 {"flush", moonbr_io_flush}, 1.111 {"flush_nb", moonbr_io_flush_nb}, 1.112 + {"close", moonbr_io_close}, 1.113 {"reset", moonbr_io_reset}, 1.114 - {"close", moonbr_io_close}, 1.115 {NULL, NULL} 1.116 }; 1.117 1.118 static const struct luaL_Reg moonbr_io_handle_metamethods[] = { 1.119 {"__index", moonbr_io_handleindex}, 1.120 {"__newindex", moonbr_io_handlenewindex}, 1.121 - {"__gc", moonbr_io_handlegc}, 1.122 + {"__gc", moonbr_io_reset}, 1.123 {NULL, NULL} 1.124 }; 1.125 1.126 static const struct luaL_Reg moonbr_io_module_funcs[] = { 1.127 - {"getdummy", moonbr_io_getdummy}, 1.128 {NULL, NULL} 1.129 }; 1.130