make Nagle's algorithm configurable

This commit is contained in:
Xertis 2025-09-30 23:45:00 +03:00
parent e3da256c5d
commit b6ca7cf918
5 changed files with 50 additions and 0 deletions

View File

@ -72,6 +72,12 @@ socket:is_connected() --> bool
-- Возвращает адрес и порт соединения.
socket:get_address() --> str, int
-- Возвращает состояние NoDelay
socket:get_nodelay() --> bool
-- Устанавливает состояние NoDelay
socket:set_nodelay(state: bool)
```
```lua

View File

@ -44,6 +44,8 @@ local Socket = {__index={
is_alive=function(self) return network.__is_alive(self.id) end,
is_connected=function(self) return network.__is_connected(self.id) end,
get_address=function(self) return network.__get_address(self.id) end,
set_nodelay=function(self, nodelay) return network.__set_nodelay(self.id, nodelay or false) end,
get_nodelay=function(self) return network.__get_nodelay(self.id) end,
}}
local WriteableSocket = {__index={

View File

@ -382,6 +382,28 @@ static int l_get_total_download(lua::State* L, network::Network& network) {
return lua::pushinteger(L, network.getTotalDownload());
}
static int l_set_nodelay(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
bool noDelay = lua::toboolean(L, 2);
if (auto connection = network.getConnection(id)) {
if (connection->getTransportType() == network::TransportType::TCP) {
dynamic_cast<network::TcpConnection*>(connection)->setNoDelay(noDelay);
}
}
return 0;
}
static int l_get_nodelay(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = network.getConnection(id)) {
if (connection->getTransportType() == network::TransportType::TCP) {
bool noDelay = dynamic_cast<network::TcpConnection*>(connection)->getNoDelay();
return lua::pushboolean(L, noDelay);
}
}
return lua::pushboolean(L, false);
}
static int l_pull_events(lua::State* L, network::Network& network) {
lua::createtable(L, events_queue.size(), 0);
@ -458,5 +480,7 @@ const luaL_Reg networklib[] = {
{"__get_address", wrap<l_get_address>},
{"__is_serveropen", wrap<l_is_serveropen>},
{"__get_serverport", wrap<l_get_serverport>},
{"__set_nodelay", wrap<l_set_nodelay>},
{"__get_nodelay", wrap<l_get_nodelay>},
{NULL, NULL}
};

View File

@ -329,6 +329,22 @@ public:
}
}
void setNoDelay(bool noDelay) override {
int opt = noDelay ? 1 : 0;
if (setsockopt(descriptor, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(opt)) < 0) {
throw handle_socket_error("setsockopt(TCP_NODELAY) failed");
}
}
bool getNoDelay() const override {
int opt = 0;
socklen_t len = sizeof(opt);
if (getsockopt(descriptor, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, &len) < 0) {
throw handle_socket_error("getsockopt(TCP_NODELAY) failed");
}
return opt != 0;
}
void startListen() {
while (state == ConnectionState::CONNECTED) {
int size = recvsocket(descriptor, buffer.data(), buffer.size());

View File

@ -76,6 +76,8 @@ namespace network {
virtual void connect(runnable callback) = 0;
virtual int recv(char* buffer, size_t length) = 0;
virtual int available() = 0;
virtual void setNoDelay(bool noDelay) = 0;
[[nodiscard]] virtual bool getNoDelay() const = 0;
[[nodiscard]] TransportType getTransportType() const noexcept override {
return TransportType::TCP;