Skip to content

Commit 1c36012

Browse files
.
1 parent bf4c723 commit 1c36012

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Sockets;
4+
using System.Threading.Tasks;
5+
using Halibut.Diagnostics;
6+
7+
namespace Halibut.Transport.Protocol
8+
{
9+
public static class TcpClientTimeoutExtensionMethods
10+
{
11+
public static async Task WithTimeout(this TcpClient stream, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits, MessageExchangeStreamTimeout timeout, Func<Task> func)
12+
{
13+
var currentReadTimeout = stream.Client.ReceiveTimeout;
14+
var currentWriteTimeout = stream.Client.SendTimeout;
15+
16+
try
17+
{
18+
stream.SetReadAndWriteTimeouts(timeout, halibutTimeoutsAndLimits);
19+
await func();
20+
}
21+
finally
22+
{
23+
stream.ReceiveTimeout = currentReadTimeout;
24+
stream.SendTimeout = currentWriteTimeout;
25+
}
26+
}
27+
28+
public static void SetReadAndWriteTimeouts(this TcpClient stream, MessageExchangeStreamTimeout timeout, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits)
29+
{
30+
switch (timeout)
31+
{
32+
case MessageExchangeStreamTimeout.NormalTimeout:
33+
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientSendTimeout.TotalMilliseconds;
34+
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientReceiveTimeout.TotalMilliseconds;
35+
break;
36+
case MessageExchangeStreamTimeout.ControlMessageExchangeShortTimeout:
37+
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientHeartbeatSendTimeout.TotalMilliseconds;
38+
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientHeartbeatReceiveTimeout.TotalMilliseconds;
39+
break;
40+
case MessageExchangeStreamTimeout.AuthenticationShortTimeout:
41+
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientAuthenticationSendTimeout.TotalMilliseconds;
42+
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientAuthenticationReceiveTimeout.TotalMilliseconds;
43+
break;
44+
case MessageExchangeStreamTimeout.PollingForNextRequestShortTimeout:
45+
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientPollingForNextRequestSendTimeout.TotalMilliseconds;
46+
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientPollingForNextRequestReceiveTimeout.TotalMilliseconds;
47+
break;
48+
default:
49+
throw new ArgumentOutOfRangeException(nameof(timeout), timeout, null);
50+
}
51+
}
52+
}
53+
}

source/Halibut/Transport/TcpConnectionFactory.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public async Task<IConnection> EstablishNewConnectionAsync(ExchangeProtocolBuild
4242
var ssl = new SslStream(networkTimeoutStream, false, certificateValidator.Validate, UserCertificateSelectionCallback);
4343

4444
log.Write(EventType.SecurityNegotiation, "Performing TLS handshake");
45-
46-
try
45+
46+
await client.WithTimeout(halibutTimeoutsAndLimits, MessageExchangeStreamTimeout.AuthenticationShortTimeout, async () =>
4747
{
4848
#if NETFRAMEWORK
4949
// TODO: ASYNC ME UP!
@@ -55,14 +55,7 @@ public async Task<IConnection> EstablishNewConnectionAsync(ExchangeProtocolBuild
5555

5656
await ssl.WriteAsync(MxLine, 0, MxLine.Length, cancellationToken);
5757
await ssl.FlushAsync(cancellationToken);
58-
}
59-
finally
60-
{
61-
62-
}
63-
64-
// TODO - SHORT TIMEOUT FOR AUTH
65-
// TODO - REVERT TO LONG TIMEOUT
58+
});
6659

6760
log.Write(EventType.Security, "Secure connection established. Server at {0} identified by thumbprint: {1}, using protocol {2}", client.Client.RemoteEndPoint, serviceEndpoint.RemoteThumbprint, ssl.SslProtocol.ToString());
6861

0 commit comments

Comments
 (0)