moonbridge
changeset 319:8c1aacea1210
Methods to manipulate socket buffer sizes
author | jbe |
---|---|
date | Thu Oct 04 14:30:20 2018 +0200 (2018-10-04) |
parents | 1c012ef2a22a |
children | 5fe68ba5fe0e |
files | moonbridge_io.c |
line diff
1.1 --- a/moonbridge_io.c Thu Jul 26 21:43:22 2018 +0200 1.2 +++ b/moonbridge_io.c Thu Oct 04 14:30:20 2018 +0200 1.3 @@ -187,6 +187,63 @@ 1.4 return 0; 1.5 } 1.6 1.7 +static int moonbr_io_get_rcvbuf(lua_State *L) { 1.8 + moonbr_io_handle_t *handle; 1.9 + int value; 1.10 + socklen_t valsz = sizeof(value); 1.11 + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.12 + /* TODO: handle closed and finished I/O handles differently? */ 1.13 + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); 1.14 + if (getsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &value, &valsz)) { 1.15 + moonbr_io_prepare_errmsg(); 1.16 + moonbr_io_return_prepared_errmsg(); 1.17 + } 1.18 + lua_pushinteger(L, value); 1.19 + return 1; 1.20 +} 1.21 + 1.22 +static int moonbr_io_get_sndbuf(lua_State *L) { 1.23 + moonbr_io_handle_t *handle; 1.24 + int value; 1.25 + socklen_t valsz = sizeof(value); 1.26 + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.27 + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); 1.28 + if (getsockopt(handle->fd, SOL_SOCKET, SO_SNDBUF, &value, &valsz)) { 1.29 + moonbr_io_prepare_errmsg(); 1.30 + moonbr_io_return_prepared_errmsg(); 1.31 + } 1.32 + lua_pushinteger(L, value); 1.33 + return 1; 1.34 +} 1.35 + 1.36 +static int moonbr_io_set_rcvbuf(lua_State *L) { 1.37 + moonbr_io_handle_t *handle; 1.38 + int value; 1.39 + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.40 + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); 1.41 + value = luaL_checkinteger(L, 2); 1.42 + if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value))) { 1.43 + moonbr_io_prepare_errmsg(); 1.44 + moonbr_io_return_prepared_errmsg(); 1.45 + } 1.46 + lua_pushvalue(L, 1); 1.47 + return 1; 1.48 +} 1.49 + 1.50 +static int moonbr_io_set_sndbuf(lua_State *L) { 1.51 + moonbr_io_handle_t *handle; 1.52 + int value; 1.53 + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.54 + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); 1.55 + value = luaL_checkinteger(L, 2); 1.56 + if (setsockopt(handle->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value))) { 1.57 + moonbr_io_prepare_errmsg(); 1.58 + moonbr_io_return_prepared_errmsg(); 1.59 + } 1.60 + lua_pushvalue(L, 1); 1.61 + return 1; 1.62 +} 1.63 + 1.64 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) { 1.65 moonbr_io_handle_t *handle; 1.66 lua_Integer maxread; 1.67 @@ -2057,6 +2114,10 @@ 1.68 #endif /* MOONBR_IO_USE_TLS */ 1.69 1.70 static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.71 + {"get_rcvbuf", moonbr_io_get_rcvbuf}, 1.72 + {"get_sndbuf", moonbr_io_get_sndbuf}, 1.73 + {"set_rcvbuf", moonbr_io_set_rcvbuf}, 1.74 + {"set_sndbuf", moonbr_io_set_sndbuf}, 1.75 {"read", moonbr_io_read}, 1.76 {"read_nb", moonbr_io_read_nb}, 1.77 {"read_call", moonbr_io_read_call},