Disable Nagle algorithm on a websocket #81175
-
Hi, as far as I understood, to disable the Nagle algorithm on a websocket, I need to reach the field NoDelay and set it to true. To my surprised, this field is actually unreachable from the interface and I had to use reflection to set it. I am now wondering why I have to hack this option. Any thought? Type wstype = _ws.GetType();
FieldInfo clientSocketField = wstype.GetField("m_Socket", BindingFlags.NonPublic | BindingFlags.Instance);
object websocket = clientSocketField.GetValue(_ws);
Type type = websocket.GetType();
FieldInfo innerWebSocketField = type.GetField("_innerWebSocket", BindingFlags.NonPublic | BindingFlags.Instance);
object innerWebSocket = innerWebSocketField.GetValue(websocket);
type = innerWebSocket.GetType();
FieldInfo webSocketField = type.GetField("_webSocket", BindingFlags.NonPublic | BindingFlags.Instance);
object webSocket = webSocketField.GetValue(innerWebSocket);
type = webSocket.GetType();
FieldInfo streamField = type.GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance);
object stream = streamField.GetValue(webSocket);
if (stream is SslStream sstream)
{
type = sstream.GetType();
PropertyInfo sstreamField = type.GetProperty("InnerStream", BindingFlags.NonPublic | BindingFlags.Instance);
stream = sstreamField.GetValue(sstream);
}
type = stream.GetType();
PropertyInfo socketField = type.GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
Socket socket = socketField.GetValue(stream) as Socket;
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 21 replies
-
This would be a request for the runtime repository that manages the base class libraries (BCL) and APIs for .NET. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Nagle is disabled by default for HttpClient / WebSockets on .NET Core. I can't speak to the implementation Unity currently uses, but there's also nothing we can do here about the implementation Unity currently uses. Unity's roadmap on getting to use the core libraries from here is described at https://blog.unity.com/technology/unity-and-net-whats-next. With regards to APIs in .NET Core, if you actually want to enable it, how you do that depends on where you're getting your WebSocket from. WebSocket.CreateFromStream let's you create a WebSocket that wraps an already opened Stream, so wherever you're getting that Stream from is where you'd need to configure Nagle; from WebSocket's perspective, it's agnostic to the underlying connection... it could be created on top of something that's not even a socket-based connection. If you're talking about a ClientWebSocket, .NET 7 includes a ConnectAsync overload that takes an HttpMessageInvoker, so you can pass in an HttpClient configured however you like, and SocketsHttpHandler let's you configure how a connection is established, so you could create a SocketsHttpHandler with a ConnectCallback that enables nagle, construct your HttpClient from that, and pass that HttpClient to ClientWebSocket's ConnectAsync. |
Beta Was this translation helpful? Give feedback.
Nagle is disabled by default for HttpClient / WebSockets on .NET Core. I can't speak to the implementation Unity currently uses, but there's also nothing we can do here about the implementation Unity currently uses. Unity's roadmap on getting to use the core libraries from here is described at https://blog.unity.com/technology/unity-and-net-whats-next.
With regards to APIs in .NET Core, if you actually want to enable it, how you do that depends on where you're getting your WebSocket from. WebSocket.CreateFromStream let's you create a WebSocket that wraps an already opened Stream, so wherever you're getting that Stream from is where you'd need to configure Nagle; from WebSocket's perspecti…