Skip to content

Commit

Permalink
Removed dirty workaround to enable NoDelay on WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
theRainbird committed Nov 22, 2024
1 parent 7aa010c commit ad8f8ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
24 changes: 2 additions & 22 deletions CoreRemoting/Channels/Websocket/WebsocketClientChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public void Init(IRemotingClient client)
Convert.ToString(client.Config.ServerPort) +
"/rpc";

_webSocket = new WebSocket(url);

TryToSetNoDelayFlagOnUnderlyingTcpClient();
_webSocket = new WebSocket(url) { NoDelay = true };

_webSocket.SetCookie(new Cookie(
name: "MessageEncryption",
Expand All @@ -56,25 +54,7 @@ public void Init(IRemotingClient client)
_webSocket.Log.Output = (timestamp, text) => Console.WriteLine("{0}: {1}", timestamp, text);
_webSocket.Log.Level = LogLevel.Debug;
}

/// <summary>
/// Try to set NoDelay flag on the underlying TcpClient to enhance performance on Linux.
/// </summary>
private void TryToSetNoDelayFlagOnUnderlyingTcpClient()
{
var webSocketType = _webSocket.GetType();
var tcpClientPrivateField =
webSocketType.GetField(
name: "_tcpClient",
bindingAttr: BindingFlags.NonPublic | BindingFlags.GetField);

if (tcpClientPrivateField != null)
{
if (tcpClientPrivateField.GetValue(_webSocket) is TcpClient tcpClient)
tcpClient.NoDelay = true;
}
}


/// <summary>
/// Establish a websocket connection with the server.
/// </summary>
Expand Down
25 changes: 4 additions & 21 deletions CoreRemoting/Channels/Websocket/WebsocketServerChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,16 @@ public void Init(IRemotingServer server)
{
_server = server ?? throw new ArgumentNullException(nameof(server));

_webSocketServer = new WebSocketServer(_server.Config.NetworkPort, secure: false);

TryToSetNoDelayFlagOnUnderlyingTcpListener();
_webSocketServer = new WebSocketServer(_server.Config.NetworkPort, secure: false)
{
NoDelay = true
};

_webSocketServer.AddWebSocketService(
path: "/rpc",
initializer: () => new RpcWebsocketSharpBehavior(_server));
}

/// <summary>
/// Try to set NoDelay flag on the underlying TcpListener to enhance performance on Linux.
/// </summary>
private void TryToSetNoDelayFlagOnUnderlyingTcpListener()
{
var webSocketServerType = _webSocketServer.GetType();
var listenerPrivateField =
webSocketServerType.GetField(
name: "_listener",
bindingAttr: BindingFlags.NonPublic | BindingFlags.GetField);

if (listenerPrivateField != null)
{
if (listenerPrivateField.GetValue(_webSocketServer) is TcpListener tcpListener)
tcpListener.Server.NoDelay = true;
}
}

/// <summary>
/// Start listening for client requests.
/// </summary>
Expand Down

0 comments on commit ad8f8ef

Please sign in to comment.