-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additionally to disabling the Nagle algorithm, disable delayed ACKs.
- Loading branch information
Showing
2 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Net.Sockets; | ||
|
||
namespace SimpleSocketClient | ||
{ | ||
internal static class SocketConfigurator | ||
{ | ||
private static readonly byte[] IntOneAsBytes = BitConverter.GetBytes(1); | ||
|
||
/// <summary> | ||
/// Disables the Nagle algorithm and delayed ACKs for TCP sockets | ||
/// in order to improve response time. | ||
/// </summary> | ||
/// <param name="socket"></param> | ||
public static void ConfigureSocket(Socket socket) | ||
{ | ||
// Disable the Nagle algorithm (so that we don't delay new packets to be sent | ||
// when the remote party didn't ACK our previous packet(s) yet). | ||
socket.NoDelay = true; | ||
|
||
// Disable delayed ACK (so that if the remote party uses the Nagle algorithm, | ||
// we can reduce the time it has to wait until it can send us new packets). | ||
DisableTcpDelayedAck(socket); | ||
} | ||
|
||
private static void DisableTcpDelayedAck(Socket socket) | ||
{ | ||
// See: https://github.com/dotnet/runtime/issues/798 for plans to integrate this | ||
// into a future .NET version. | ||
// Note: On Debian-based Linux OSes, it seems TCP_QUICKACK is already enabled | ||
// by default (according to the man page of socket_quickack(3). | ||
if (socket.ProtocolType == ProtocolType.Tcp && OperatingSystem.IsWindows()) | ||
{ | ||
try | ||
{ | ||
const int SIO_TCP_SET_ACK_FREQUENCY = unchecked((int)0x98000017); | ||
socket.IOControl(SIO_TCP_SET_ACK_FREQUENCY, IntOneAsBytes, Array.Empty<byte>()); | ||
} | ||
catch (SocketException) | ||
{ | ||
// Ignore; we don't want to fail. | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters