# HG changeset patch # User jbe # Date 1538656220 -7200 # Node ID 8c1aacea12104ee4aeea5154b8c9492a8a4bbe27 # Parent 1c012ef2a22a2e158af50c3d546d0a5cee322d4b Methods to manipulate socket buffer sizes diff -r 1c012ef2a22a -r 8c1aacea1210 moonbridge_io.c --- a/moonbridge_io.c Thu Jul 26 21:43:22 2018 +0200 +++ b/moonbridge_io.c Thu Oct 04 14:30:20 2018 +0200 @@ -187,6 +187,63 @@ return 0; } +static int moonbr_io_get_rcvbuf(lua_State *L) { + moonbr_io_handle_t *handle; + int value; + socklen_t valsz = sizeof(value); + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); + /* TODO: handle closed and finished I/O handles differently? */ + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); + if (getsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &value, &valsz)) { + moonbr_io_prepare_errmsg(); + moonbr_io_return_prepared_errmsg(); + } + lua_pushinteger(L, value); + return 1; +} + +static int moonbr_io_get_sndbuf(lua_State *L) { + moonbr_io_handle_t *handle; + int value; + socklen_t valsz = sizeof(value); + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); + if (getsockopt(handle->fd, SOL_SOCKET, SO_SNDBUF, &value, &valsz)) { + moonbr_io_prepare_errmsg(); + moonbr_io_return_prepared_errmsg(); + } + lua_pushinteger(L, value); + return 1; +} + +static int moonbr_io_set_rcvbuf(lua_State *L) { + moonbr_io_handle_t *handle; + int value; + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); + value = luaL_checkinteger(L, 2); + if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value))) { + moonbr_io_prepare_errmsg(); + moonbr_io_return_prepared_errmsg(); + } + lua_pushvalue(L, 1); + return 1; +} + +static int moonbr_io_set_sndbuf(lua_State *L) { + moonbr_io_handle_t *handle; + int value; + handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); + if (handle->fd < 0) luaL_error(L, "Attempt to operate on closed or finished I/O handle"); + value = luaL_checkinteger(L, 2); + if (setsockopt(handle->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value))) { + moonbr_io_prepare_errmsg(); + moonbr_io_return_prepared_errmsg(); + } + lua_pushvalue(L, 1); + return 1; +} + static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) { moonbr_io_handle_t *handle; lua_Integer maxread; @@ -2057,6 +2114,10 @@ #endif /* MOONBR_IO_USE_TLS */ static const struct luaL_Reg moonbr_io_handle_methods[] = { + {"get_rcvbuf", moonbr_io_get_rcvbuf}, + {"get_sndbuf", moonbr_io_get_sndbuf}, + {"set_rcvbuf", moonbr_io_set_rcvbuf}, + {"set_sndbuf", moonbr_io_set_sndbuf}, {"read", moonbr_io_read}, {"read_nb", moonbr_io_read_nb}, {"read_call", moonbr_io_read_call},