Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwoctopusdeploy committed Nov 16, 2023
1 parent bf4c723 commit 1c36012
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using Halibut.Diagnostics;

namespace Halibut.Transport.Protocol
{
public static class TcpClientTimeoutExtensionMethods
{
public static async Task WithTimeout(this TcpClient stream, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits, MessageExchangeStreamTimeout timeout, Func<Task> func)
{
var currentReadTimeout = stream.Client.ReceiveTimeout;
var currentWriteTimeout = stream.Client.SendTimeout;

try
{
stream.SetReadAndWriteTimeouts(timeout, halibutTimeoutsAndLimits);
await func();
}
finally
{
stream.ReceiveTimeout = currentReadTimeout;
stream.SendTimeout = currentWriteTimeout;
}
}

public static void SetReadAndWriteTimeouts(this TcpClient stream, MessageExchangeStreamTimeout timeout, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits)
{
switch (timeout)
{
case MessageExchangeStreamTimeout.NormalTimeout:
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientSendTimeout.TotalMilliseconds;
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientReceiveTimeout.TotalMilliseconds;
break;
case MessageExchangeStreamTimeout.ControlMessageExchangeShortTimeout:
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientHeartbeatSendTimeout.TotalMilliseconds;
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientHeartbeatReceiveTimeout.TotalMilliseconds;
break;
case MessageExchangeStreamTimeout.AuthenticationShortTimeout:
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientAuthenticationSendTimeout.TotalMilliseconds;
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientAuthenticationReceiveTimeout.TotalMilliseconds;
break;
case MessageExchangeStreamTimeout.PollingForNextRequestShortTimeout:
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientPollingForNextRequestSendTimeout.TotalMilliseconds;
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientPollingForNextRequestReceiveTimeout.TotalMilliseconds;
break;
default:
throw new ArgumentOutOfRangeException(nameof(timeout), timeout, null);
}
}
}
}
13 changes: 3 additions & 10 deletions source/Halibut/Transport/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public async Task<IConnection> EstablishNewConnectionAsync(ExchangeProtocolBuild
var ssl = new SslStream(networkTimeoutStream, false, certificateValidator.Validate, UserCertificateSelectionCallback);

log.Write(EventType.SecurityNegotiation, "Performing TLS handshake");
try

await client.WithTimeout(halibutTimeoutsAndLimits, MessageExchangeStreamTimeout.AuthenticationShortTimeout, async () =>
{
#if NETFRAMEWORK
// TODO: ASYNC ME UP!
Expand All @@ -55,14 +55,7 @@ public async Task<IConnection> EstablishNewConnectionAsync(ExchangeProtocolBuild

await ssl.WriteAsync(MxLine, 0, MxLine.Length, cancellationToken);
await ssl.FlushAsync(cancellationToken);
}
finally
{

}

// TODO - SHORT TIMEOUT FOR AUTH
// TODO - REVERT TO LONG TIMEOUT
});

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());

Expand Down

0 comments on commit 1c36012

Please sign in to comment.