diff --git a/src/openrct2/network/Socket.cpp b/src/openrct2/network/Socket.cpp index ba9d56fda9..cbbcfdf96c 100644 --- a/src/openrct2/network/Socket.cpp +++ b/src/openrct2/network/Socket.cpp @@ -233,6 +233,14 @@ public: return _error.empty() ? nullptr : _error.c_str(); } + void SetNoDelay(bool noDelay) override + { + if (_socket != INVALID_SOCKET) + { + SetOption(_socket, IPPROTO_TCP, TCP_NODELAY, noDelay); + } + } + void Listen(uint16_t port) override { Listen("", port); @@ -334,7 +342,7 @@ public: int32_t rc = getnameinfo( reinterpret_cast(&client_addr), client_len, hostName, sizeof(hostName), nullptr, 0, NI_NUMERICHOST | NI_NUMERICSERV); - SetOption(socket, IPPROTO_TCP, TCP_NODELAY, true); + SetNoDelay(true); if (rc == 0) { @@ -375,7 +383,7 @@ public: throw SocketException("Unable to create socket."); } - SetOption(_socket, IPPROTO_TCP, TCP_NODELAY, true); + SetNoDelay(true); if (!SetNonBlocking(_socket, true)) { throw SocketException("Failed to set non-blocking mode."); diff --git a/src/openrct2/network/Socket.h b/src/openrct2/network/Socket.h index 9a652264ca..5d9b9a89ad 100644 --- a/src/openrct2/network/Socket.h +++ b/src/openrct2/network/Socket.h @@ -67,6 +67,8 @@ public: virtual size_t SendData(const void* buffer, size_t size) abstract; virtual NetworkReadPacket ReceiveData(void* buffer, size_t size, size_t* sizeReceived) abstract; + virtual void SetNoDelay(bool noDelay) abstract; + virtual void Finish() abstract; virtual void Disconnect() abstract; virtual void Close() abstract; diff --git a/src/openrct2/scripting/ScSocketServer.hpp b/src/openrct2/scripting/ScSocketServer.hpp index 537a95e8c7..6bff92ebdb 100644 --- a/src/openrct2/scripting/ScSocketServer.hpp +++ b/src/openrct2/scripting/ScSocketServer.hpp @@ -133,6 +133,15 @@ namespace OpenRCT2::Scripting return this; } + ScSocket* setNoDelay(bool noDelay) + { + if (_socket != nullptr) + { + _socket->SetNoDelay(noDelay); + } + return this; + } + ScSocket* end(const DukValue& data) { if (_disposed) @@ -268,6 +277,7 @@ namespace OpenRCT2::Scripting static void Register(duk_context* ctx) { dukglue_register_method(ctx, &ScSocket::destroy, "destroy"); + dukglue_register_method(ctx, &ScSocket::setNoDelay, "setNoDelay"); dukglue_register_method(ctx, &ScSocket::end, "end"); dukglue_register_method(ctx, &ScSocket::write, "write"); dukglue_register_method(ctx, &ScSocket::on, "on"); @@ -365,6 +375,9 @@ namespace OpenRCT2::Scripting auto client = _socket->Accept(); if (client != nullptr) { + // Default to using Nagle's algorithm like node.js does + client->SetNoDelay(false); + auto& scriptEngine = GetContext()->GetScriptEngine(); auto clientSocket = std::make_shared(GetPlugin(), std::move(client)); scriptEngine.AddSocket(clientSocket);