diff --git a/docs/CodeDoc/Atc.Network/Atc.Network.Tcp.md b/docs/CodeDoc/Atc.Network/Atc.Network.Tcp.md index 351ba5c..19dab81 100644 --- a/docs/CodeDoc/Atc.Network/Atc.Network.Tcp.md +++ b/docs/CodeDoc/Atc.Network/Atc.Network.Tcp.md @@ -499,7 +499,12 @@ This class contains default constant for `Atc.Network.Tcp.TcpClient` and `Atc.Ne >```csharp >int DefaultSendReceiveTimeout >``` ->Summary: The send/receive time-out value, in milliseconds. +>Summary: The send/receive time-out value, in milliseconds (5 min.). +#### GracePeriodTimeout +>```csharp +>int GracePeriodTimeout +>``` +>Summary: The grace period timeout, in milliseconds (1 sec).
diff --git a/docs/CodeDoc/Atc.Network/Atc.Network.Udp.md b/docs/CodeDoc/Atc.Network/Atc.Network.Udp.md index 182b388..6726116 100644 --- a/docs/CodeDoc/Atc.Network/Atc.Network.Udp.md +++ b/docs/CodeDoc/Atc.Network/Atc.Network.Udp.md @@ -365,11 +365,21 @@ This class contains default constant for `Atc.Network.Udp.UdpClient` and `Atc.Ne >int DefaultBufferSize >``` >Summary: The send/receive buffer value, in bytes. The default is 8192 (8 Kb); +#### DefaultConnectTimeout +>```csharp +>int DefaultConnectTimeout +>``` +>Summary: The connect time-out value, in milliseconds (10 sec). #### DefaultSendReceiveTimeout >```csharp >int DefaultSendReceiveTimeout >``` ->Summary: The send/receive time-out value, in milliseconds. +>Summary: The send/receive time-out value, in milliseconds (5 min.). +#### GracePeriodTimeout +>```csharp +>int GracePeriodTimeout +>``` +>Summary: The grace period timeout, in milliseconds (1 sec).
diff --git a/docs/CodeDoc/Atc.Network/Atc.Network.md b/docs/CodeDoc/Atc.Network/Atc.Network.md index 39622cc..4ad00b5 100644 --- a/docs/CodeDoc/Atc.Network/Atc.Network.md +++ b/docs/CodeDoc/Atc.Network/Atc.Network.md @@ -283,7 +283,7 @@ Enumeration: NetworkQualityCategoryType. >     `tcpClient`  -  The TCP client.
#### SetBufferSizeAndTimeouts >```csharp ->void SetBufferSizeAndTimeouts(this TcpClient tcpClient, int sendTimeout = 0, int sendBufferSize = 8192, int receiveTimeout = 0, int receiveBufferSize = 8192) +>void SetBufferSizeAndTimeouts(this TcpClient tcpClient, int sendTimeout = 600000, int sendBufferSize = 8192, int receiveTimeout = 600000, int receiveBufferSize = 8192) >``` >Summary: Sets the buffer size and timeouts. > diff --git a/docs/CodeDoc/Atc.Network/IndexExtended.md b/docs/CodeDoc/Atc.Network/IndexExtended.md index dc812d8..81bbc20 100644 --- a/docs/CodeDoc/Atc.Network/IndexExtended.md +++ b/docs/CodeDoc/Atc.Network/IndexExtended.md @@ -38,7 +38,7 @@ - [TcpClientExtensions](Atc.Network.md#tcpclientextensions) - Static Methods - DisableKeepAlive(this TcpClient tcpClient) - - SetBufferSizeAndTimeouts(this TcpClient tcpClient, int sendTimeout = 0, int sendBufferSize = 8192, int receiveTimeout = 0, int receiveBufferSize = 8192) + - SetBufferSizeAndTimeouts(this TcpClient tcpClient, int sendTimeout = 600000, int sendBufferSize = 8192, int receiveTimeout = 600000, int receiveBufferSize = 8192) - SetKeepAlive(this TcpClient tcpClient, int tcpKeepAliveTime = 2, int tcpKeepAliveInterval = 2, int tcpKeepAliveRetryCount = 5) - [TerminationType](Atc.Network.md#terminationtype) - [TransportProtocolType](Atc.Network.md#transportprotocoltype) @@ -335,6 +335,7 @@ - int DefaultReconnectRetryInterval - int DefaultReconnectRetryMaxAttempts - int DefaultSendReceiveTimeout + - int GracePeriodTimeout - [TcpServer](Atc.Network.Tcp.md#tcpserver) - Properties - IpAddress @@ -412,7 +413,9 @@ - [UdpConstants](Atc.Network.Udp.md#udpconstants) - Static Fields - int DefaultBufferSize + - int DefaultConnectTimeout - int DefaultSendReceiveTimeout + - int GracePeriodTimeout - [UdpServer](Atc.Network.Udp.md#udpserver) - Properties - IsRunning diff --git a/src/Atc.Network/Tcp/TcpClient.cs b/src/Atc.Network/Tcp/TcpClient.cs index 5345e51..86bdae5 100644 --- a/src/Atc.Network/Tcp/TcpClient.cs +++ b/src/Atc.Network/Tcp/TcpClient.cs @@ -21,6 +21,8 @@ public partial class TcpClient : ITcpClient private readonly TcpClientKeepAliveConfig clientKeepAliveConfig; private readonly byte[] receiveBuffer; + private readonly int syncLockConnectTimeoutInMs; + private readonly int syncLockSendTimeoutInMs; private int reconnectRetryCounter; private CancellationTokenSource? cancellationTokenSource; private CancellationTokenRegistration? cancellationTokenRegistration; @@ -61,10 +63,18 @@ private TcpClient( { this.logger = logger; this.clientConfig = clientConfig ?? new TcpClientConfig(); - this.clientReconnectConfig = reconnectConfig ?? new TcpClientReconnectConfig(); - this.clientKeepAliveConfig = keepAliveConfig ?? new TcpClientKeepAliveConfig(); + clientReconnectConfig = reconnectConfig ?? new TcpClientReconnectConfig(); + clientKeepAliveConfig = keepAliveConfig ?? new TcpClientKeepAliveConfig(); receiveBuffer = new byte[this.clientConfig.ReceiveBufferSize]; + + syncLockConnectTimeoutInMs = this.clientConfig.ConnectTimeout <= 0 + ? TcpConstants.DefaultConnectTimeout + : this.clientConfig.ConnectTimeout + TcpConstants.GracePeriodTimeout; + + syncLockSendTimeoutInMs = this.clientConfig.SendTimeout <= 0 + ? TcpConstants.DefaultSendReceiveTimeout + : this.clientConfig.SendTimeout + TcpConstants.GracePeriodTimeout; } public TcpClient( @@ -279,7 +289,8 @@ public async Task Send( LogDataSendingByteLength(IPAddressOrHostname, Port, data.Length); - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(syncLockSendTimeoutInMs, cancellationToken); + var disconnectedDueToException = false; try @@ -523,7 +534,7 @@ private async Task SetConnected( bool raiseEvents, CancellationToken cancellationToken = default) { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken); try { @@ -549,7 +560,7 @@ private async Task SetDisconnected( bool dispose, CancellationToken cancellationToken = default) { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken); try { diff --git a/src/Atc.Network/Tcp/TcpConstants.cs b/src/Atc.Network/Tcp/TcpConstants.cs index 5893117..9d853bf 100644 --- a/src/Atc.Network/Tcp/TcpConstants.cs +++ b/src/Atc.Network/Tcp/TcpConstants.cs @@ -9,12 +9,12 @@ public static class TcpConstants /// /// The connect time-out value, in milliseconds (10 sec). /// - public const int DefaultConnectTimeout = 10000; + public const int DefaultConnectTimeout = 10_000; /// - /// The send/receive time-out value, in milliseconds. + /// The send/receive time-out value, in milliseconds (5 min.). /// - public const int DefaultSendReceiveTimeout = 0; + public const int DefaultSendReceiveTimeout = 600_000; /// /// The send/receive buffer value, in bytes (8 Kb). @@ -24,10 +24,15 @@ public static class TcpConstants /// /// The reconnect retry interval value, in milliseconds (1 sec). /// - public const int DefaultReconnectRetryInterval = 1000; + public const int DefaultReconnectRetryInterval = 1_000; /// /// The reconnect retry max attempts value. /// public const int DefaultReconnectRetryMaxAttempts = 3600; + + /// + /// The grace period timeout, in milliseconds (1 sec). + /// + public const int GracePeriodTimeout = 1_000; } \ No newline at end of file diff --git a/src/Atc.Network/Udp/UdpClient.cs b/src/Atc.Network/Udp/UdpClient.cs index 6e95b40..3f634ca 100644 --- a/src/Atc.Network/Udp/UdpClient.cs +++ b/src/Atc.Network/Udp/UdpClient.cs @@ -11,6 +11,8 @@ public partial class UdpClient : IUdpClient private const int TimeToWaitForDisposeDisconnectionInMs = 50; private const int TimeToWaitForDataReceiverInMs = 150; + private readonly int syncLockConnectTimeoutInMs; + private readonly int syncLockSendTimeoutInMs; private readonly SemaphoreSlim syncLock = new(1, 1); private readonly UdpClientConfig clientConfig; private readonly ArraySegment receiveBufferSegment; @@ -51,6 +53,14 @@ private UdpClient( var receiveBuffer = new byte[this.clientConfig.ReceiveBufferSize]; receiveBufferSegment = new ArraySegment(receiveBuffer); + + syncLockConnectTimeoutInMs = this.clientConfig.ConnectTimeout <= 0 + ? UdpConstants.DefaultConnectTimeout + : this.clientConfig.ConnectTimeout + UdpConstants.GracePeriodTimeout; + + syncLockSendTimeoutInMs = this.clientConfig.SendTimeout <= 0 + ? UdpConstants.DefaultSendReceiveTimeout + : this.clientConfig.SendTimeout + UdpConstants.GracePeriodTimeout; } public UdpClient( @@ -179,8 +189,9 @@ public async Task Send( TerminationHelper.AppendTerminationBytesIfNeeded(ref data, terminationType); + await syncLock.WaitAsync(syncLockSendTimeoutInMs, cancellationToken); + var buffer = new ArraySegment(data); - await syncLock.WaitAsync(cancellationToken); var disconnectedDueToException = false; try @@ -377,7 +388,7 @@ private async Task SetConnected( bool raiseEvents, CancellationToken cancellationToken = default) { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken); try { @@ -402,7 +413,7 @@ private async Task SetDisconnected( bool raiseEvents = true, CancellationToken cancellationToken = default) { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken); try { diff --git a/src/Atc.Network/Udp/UdpConstants.cs b/src/Atc.Network/Udp/UdpConstants.cs index e8b50b3..f44e22b 100644 --- a/src/Atc.Network/Udp/UdpConstants.cs +++ b/src/Atc.Network/Udp/UdpConstants.cs @@ -7,12 +7,22 @@ namespace Atc.Network.Udp; public static class UdpConstants { /// - /// The send/receive time-out value, in milliseconds. + /// The connect time-out value, in milliseconds (10 sec). /// - public const int DefaultSendReceiveTimeout = 0; + public const int DefaultConnectTimeout = 10_000; + + /// + /// The send/receive time-out value, in milliseconds (5 min.). + /// + public const int DefaultSendReceiveTimeout = 600_000; /// /// The send/receive buffer value, in bytes. The default is 8192 (8 Kb); /// public const int DefaultBufferSize = 8192; + + /// + /// The grace period timeout, in milliseconds (1 sec). + /// + public const int GracePeriodTimeout = 1_000; } \ No newline at end of file