Skip to content

Commit 0b588e8

Browse files
committed
set NoDelay by default and make client more extensible
1 parent aee3544 commit 0b588e8

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

src/SuperSocket.Client.Proxy/ProxyConnectorBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public ProxyConnectorBase(EndPoint proxyEndPoint)
3636
/// <returns>A task representing the asynchronous connection operation.</returns>
3737
protected abstract ValueTask<ConnectState> ConnectProxyAsync(EndPoint remoteEndPoint, ConnectState state, CancellationToken cancellationToken);
3838

39+
/// <summary>
40+
/// Creates the default connector for the proxy connection.
41+
/// </summary>
42+
protected virtual IConnector CreateDefaultConnector()
43+
{
44+
return new SocketConnector();
45+
}
46+
3947
/// <summary>
4048
/// Establishes a connection to the specified remote endpoint through the proxy.
4149
/// </summary>
@@ -49,7 +57,7 @@ public ProxyConnectorBase(EndPoint proxyEndPoint)
4957
/// </remarks>
5058
protected override async ValueTask<ConnectState> ConnectAsync(EndPoint remoteEndPoint, ConnectState state, CancellationToken cancellationToken)
5159
{
52-
var socketConnector = new SocketConnector() as IConnector;
60+
var socketConnector = CreateDefaultConnector();
5361
var proxyEndPoint = _proxyEndPoint;
5462

5563
ConnectState result;

src/SuperSocket.Client/EasyClient.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ public EasyClient(ConnectionOptions options)
9797
Logger = options.Logger;
9898
}
9999

100+
/// <summary>
101+
/// Creates the default connector for the client.
102+
/// </summary>
103+
/// <returns>The default connector.</returns>
104+
protected IConnector CreateDefaultConnector()
105+
{
106+
return new SocketConnector(LocalEndPoint);
107+
}
108+
100109
/// <summary>
101110
/// Gets the connector for the client.
102111
/// </summary>
@@ -111,7 +120,7 @@ protected virtual IConnector GetConnector()
111120
}
112121
else
113122
{
114-
connectors.Add(new SocketConnector(LocalEndPoint));
123+
connectors.Add(CreateDefaultConnector());
115124
}
116125

117126
var security = Security;

src/SuperSocket.Client/SocketConnector.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public class SocketConnector : ConnectorBase
1616
/// </summary>
1717
public IPEndPoint LocalEndPoint { get; private set; }
1818

19+
/// <summary>
20+
/// Gets or sets a value indicating whether the Nagle algorithm is disabled for the socket.
21+
/// When set to true, the socket will send data immediately without waiting to accumulate more data.
22+
/// This can improve performance for certain applications that require low latency, but may increase network traffic.
23+
/// The default value is true.
24+
/// </summary>
25+
public bool NoDelay { get; set; } = true;
26+
1927
/// <summary>
2028
/// Initializes a new instance of the <see cref="SocketConnector"/> class with default settings.
2129
/// </summary>
@@ -34,6 +42,26 @@ public SocketConnector(IPEndPoint localEndPoint)
3442
LocalEndPoint = localEndPoint;
3543
}
3644

45+
/// <summary>
46+
/// Configures the socket with the specified settings before connecting.
47+
/// This method can be overridden to customize the socket configuration, such as setting socket options or binding to a local endpoint.
48+
/// By default, it sets the NoDelay property and binds to the LocalEndPoint if it is specified.
49+
/// </summary>
50+
/// <param name="socket">The socket to configure.</param>
51+
protected virtual void ConfigureSocket(Socket socket)
52+
{
53+
socket.NoDelay = NoDelay;
54+
55+
var localEndPoint = LocalEndPoint;
56+
57+
if (localEndPoint != null)
58+
{
59+
socket.ExclusiveAddressUse = false;
60+
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
61+
socket.Bind(localEndPoint);
62+
}
63+
}
64+
3765
/// <summary>
3866
/// Asynchronously connects to a remote endpoint using a TCP socket.
3967
/// </summary>
@@ -52,14 +80,8 @@ protected override async ValueTask<ConnectState> ConnectAsync(EndPoint remoteEnd
5280

5381
try
5482
{
55-
var localEndPoint = LocalEndPoint;
56-
57-
if (localEndPoint != null)
58-
{
59-
socket.ExclusiveAddressUse = false;
60-
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
61-
socket.Bind(localEndPoint);
62-
}
83+
ConfigureSocket(socket);
84+
6385
#if NET5_0_OR_GREATER
6486
await socket.ConnectAsync(remoteEndPoint, cancellationToken);
6587
#else

0 commit comments

Comments
 (0)