Skip to content

Commit 8092479

Browse files
committed
StandardSocketsHttpHandler: Added SocketCreated Event
The subscriber will be able to configure the socket to send TCP Keepalive
1 parent 0176699 commit 8092479

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ConnectHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace System.Net.Http
1616
{
17+
internal delegate void ConfigureSocket(Socket socket);
18+
1719
internal static class ConnectHelper
1820
{
1921
/// <summary>Pool of event args to use to establish connections.</summary>
@@ -37,7 +39,7 @@ public CertificateCallbackMapper(Func<HttpRequestMessage, X509Certificate2, X509
3739
}
3840
}
3941

40-
public static async Task<(Socket, Stream)> ConnectAsync(string host, int port, CancellationToken cancellationToken)
42+
public static async Task<(Socket, Stream)> ConnectAsync(string host, int port, ConfigureSocket configureSocket, CancellationToken cancellationToken)
4143
{
4244
// Rather than creating a new Socket and calling ConnectAsync on it, we use the static
4345
// Socket.ConnectAsync with a SocketAsyncEventArgs, as we can then use Socket.CancelConnectAsync
@@ -76,6 +78,7 @@ public CertificateCallbackMapper(Func<HttpRequestMessage, X509Certificate2, X509
7678
// Configure the socket and return a stream for it.
7779
Socket socket = saea.ConnectSocket;
7880
socket.NoDelay = true;
81+
configureSocket(socket);
7982
return (socket, new NetworkStream(socket, ownsSocket: true));
8083
}
8184
catch (Exception error)

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,11 @@ public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool doRe
359359
case HttpConnectionKind.Http:
360360
case HttpConnectionKind.Https:
361361
case HttpConnectionKind.ProxyConnect:
362-
(socket, stream) = await ConnectHelper.ConnectAsync(_host, _port, cancellationToken).ConfigureAwait(false);
362+
(socket, stream) = await ConnectHelper.ConnectAsync(_host, _port, _poolManager.Settings._configureSocket, cancellationToken).ConfigureAwait(false);
363363
break;
364364

365365
case HttpConnectionKind.Proxy:
366-
(socket, stream) = await ConnectHelper.ConnectAsync(_proxyUri.IdnHost, _proxyUri.Port, cancellationToken).ConfigureAwait(false);
366+
(socket, stream) = await ConnectHelper.ConnectAsync(_proxyUri.IdnHost, _proxyUri.Port, _poolManager.Settings._configureSocket, cancellationToken).ConfigureAwait(false);
367367
break;
368368

369369
case HttpConnectionKind.ProxyTunnel:

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ internal sealed class HttpConnectionSettings
3838
internal SslClientAuthenticationOptions _sslOptions;
3939

4040
internal IDictionary<string, object> _properties;
41+
internal ConfigureSocket _configureSocket = (_) => { };
4142

4243
public HttpConnectionSettings Clone()
4344
{
@@ -67,6 +68,7 @@ public HttpConnectionSettings Clone()
6768
_properties = _properties,
6869
_proxy = _proxy,
6970
_sslOptions = _sslOptions?.ShallowClone(), // shallow clone the options for basic prevention of mutation issues while processing
71+
_configureSocket = _configureSocket,
7072
_useCookies = _useCookies,
7173
_useProxy = _useProxy,
7274
};

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/StandardSocketsHttpHandler.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Generic;
66
using System.Net.Security;
7+
using System.Net.Sockets;
78
using System.Threading;
89
using System.Threading.Tasks;
910

@@ -14,6 +15,16 @@ public sealed class StandardSocketsHttpHandler : HttpMessageHandler
1415
private readonly HttpConnectionSettings _settings = new HttpConnectionSettings();
1516
private StandardHttpMessageHandler _handler;
1617
private bool _disposed;
18+
19+
public event EventHandler<Socket> SocketCreated;
20+
21+
public StandardSocketsHttpHandler()
22+
{
23+
_settings._configureSocket = (Socket socket) =>
24+
{
25+
SocketCreated?.Invoke(this, socket);
26+
};
27+
}
1728

1829
private void CheckDisposed()
1930
{

0 commit comments

Comments
 (0)