Merge pull request #633 from Xertis/dev

Make Nagle's algorithm configurable
This commit is contained in:
MihailRis 2025-10-01 01:29:49 +03:00 committed by GitHub
commit fbba0e1415
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 50 additions and 0 deletions

View File

@ -93,6 +93,12 @@ socket:is_connected() --> bool
-- Возвращает адрес и порт соединения.
socket:get_address() --> str, int
-- Возвращает состояние NoDelay
socket:is_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,
is_nodelay=function(self) return network.__is_nodelay(self.id) end,
}}
local WriteableSocket = {__index={

View File

@ -402,6 +402,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_is_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)->isNoDelay();
return lua::pushboolean(L, noDelay);
}
}
return lua::pushboolean(L, false);
}
static int l_pull_events(lua::State* L, network::Network& network) {
std::vector<NetworkEvent> local_queue;
{
@ -517,5 +539,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>},
{"__is_nodelay", wrap<l_is_nodelay>},
{NULL, NULL}
};

View File

@ -355,6 +355,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 isNoDelay() 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

@ -77,6 +77,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 isNoDelay() const = 0;
[[nodiscard]] TransportType getTransportType() const noexcept override {
return TransportType::TCP;