1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 19:13:07 +01:00

Implement setNoDelay

This commit is contained in:
Ted John
2020-08-17 13:10:36 +01:00
parent 7a5cb8a5b6
commit 182bcaf21a
3 changed files with 25 additions and 2 deletions

View File

@@ -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<struct sockaddr*>(&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.");

View File

@@ -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;

View File

@@ -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<ScSocket>(GetPlugin(), std::move(client));
scriptEngine.AddSocket(clientSocket);