Skip to content

Commit

Permalink
Merge pull request #36 from atc-net/feature/Add-syncLock-imeout-for-T…
Browse files Browse the repository at this point in the history
…cpClient-and-UdpClient

Ad sync-lock-timeout for TcpClient and UdpClient
  • Loading branch information
davidkallesen authored Feb 15, 2024
2 parents 4fc749d + d21e56d commit 575bf8b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 18 deletions.
7 changes: 6 additions & 1 deletion docs/CodeDoc/Atc.Network/Atc.Network.Tcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,12 @@ This class contains default constant for `Atc.Network.Tcp.TcpClient` and `Atc.Ne
>```csharp
>int DefaultSendReceiveTimeout
>```
><b>Summary:</b> The send/receive time-out value, in milliseconds.
><b>Summary:</b> The send/receive time-out value, in milliseconds (5 min.).
#### GracePeriodTimeout
>```csharp
>int GracePeriodTimeout
>```
><b>Summary:</b> The grace period timeout, in milliseconds (1 sec).
<br />
Expand Down
12 changes: 11 additions & 1 deletion docs/CodeDoc/Atc.Network/Atc.Network.Udp.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,21 @@ This class contains default constant for `Atc.Network.Udp.UdpClient` and `Atc.Ne
>int DefaultBufferSize
>```
><b>Summary:</b> The send/receive buffer value, in bytes. The default is 8192 (8 Kb);
#### DefaultConnectTimeout
>```csharp
>int DefaultConnectTimeout
>```
><b>Summary:</b> The connect time-out value, in milliseconds (10 sec).
#### DefaultSendReceiveTimeout
>```csharp
>int DefaultSendReceiveTimeout
>```
><b>Summary:</b> The send/receive time-out value, in milliseconds.
><b>Summary:</b> The send/receive time-out value, in milliseconds (5 min.).
#### GracePeriodTimeout
>```csharp
>int GracePeriodTimeout
>```
><b>Summary:</b> The grace period timeout, in milliseconds (1 sec).
<br />
Expand Down
2 changes: 1 addition & 1 deletion docs/CodeDoc/Atc.Network/Atc.Network.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ Enumeration: NetworkQualityCategoryType.
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`tcpClient`&nbsp;&nbsp;-&nbsp;&nbsp;The TCP client.<br />
#### 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)
>```
><b>Summary:</b> Sets the buffer size and timeouts.
>
Expand Down
5 changes: 4 additions & 1 deletion docs/CodeDoc/Atc.Network/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -335,6 +335,7 @@
- int DefaultReconnectRetryInterval
- int DefaultReconnectRetryMaxAttempts
- int DefaultSendReceiveTimeout
- int GracePeriodTimeout
- [TcpServer](Atc.Network.Tcp.md#tcpserver)
- Properties
- IpAddress
Expand Down Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/Atc.Network/Tcp/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -523,7 +534,7 @@ private async Task SetConnected(
bool raiseEvents,
CancellationToken cancellationToken = default)
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken);

try
{
Expand All @@ -549,7 +560,7 @@ private async Task SetDisconnected(
bool dispose,
CancellationToken cancellationToken = default)
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken);

try
{
Expand Down
13 changes: 9 additions & 4 deletions src/Atc.Network/Tcp/TcpConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static class TcpConstants
/// <summary>
/// The connect time-out value, in milliseconds (10 sec).
/// </summary>
public const int DefaultConnectTimeout = 10000;
public const int DefaultConnectTimeout = 10_000;

/// <summary>
/// The send/receive time-out value, in milliseconds.
/// The send/receive time-out value, in milliseconds (5 min.).
/// </summary>
public const int DefaultSendReceiveTimeout = 0;
public const int DefaultSendReceiveTimeout = 600_000;

/// <summary>
/// The send/receive buffer value, in bytes (8 Kb).
Expand All @@ -24,10 +24,15 @@ public static class TcpConstants
/// <summary>
/// The reconnect retry interval value, in milliseconds (1 sec).
/// </summary>
public const int DefaultReconnectRetryInterval = 1000;
public const int DefaultReconnectRetryInterval = 1_000;

/// <summary>
/// The reconnect retry max attempts value.
/// </summary>
public const int DefaultReconnectRetryMaxAttempts = 3600;

/// <summary>
/// The grace period timeout, in milliseconds (1 sec).
/// </summary>
public const int GracePeriodTimeout = 1_000;
}
17 changes: 14 additions & 3 deletions src/Atc.Network/Udp/UdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> receiveBufferSegment;
Expand Down Expand Up @@ -51,6 +53,14 @@ private UdpClient(

var receiveBuffer = new byte[this.clientConfig.ReceiveBufferSize];
receiveBufferSegment = new ArraySegment<byte>(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(
Expand Down Expand Up @@ -179,8 +189,9 @@ public async Task Send(

TerminationHelper.AppendTerminationBytesIfNeeded(ref data, terminationType);

await syncLock.WaitAsync(syncLockSendTimeoutInMs, cancellationToken);

var buffer = new ArraySegment<byte>(data);
await syncLock.WaitAsync(cancellationToken);
var disconnectedDueToException = false;

try
Expand Down Expand Up @@ -377,7 +388,7 @@ private async Task SetConnected(
bool raiseEvents,
CancellationToken cancellationToken = default)
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken);

try
{
Expand All @@ -402,7 +413,7 @@ private async Task SetDisconnected(
bool raiseEvents = true,
CancellationToken cancellationToken = default)
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(syncLockConnectTimeoutInMs, cancellationToken);

try
{
Expand Down
14 changes: 12 additions & 2 deletions src/Atc.Network/Udp/UdpConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ namespace Atc.Network.Udp;
public static class UdpConstants
{
/// <summary>
/// The send/receive time-out value, in milliseconds.
/// The connect time-out value, in milliseconds (10 sec).
/// </summary>
public const int DefaultSendReceiveTimeout = 0;
public const int DefaultConnectTimeout = 10_000;

/// <summary>
/// The send/receive time-out value, in milliseconds (5 min.).
/// </summary>
public const int DefaultSendReceiveTimeout = 600_000;

/// <summary>
/// The send/receive buffer value, in bytes. The default is 8192 (8 Kb);
/// </summary>
public const int DefaultBufferSize = 8192;

/// <summary>
/// The grace period timeout, in milliseconds (1 sec).
/// </summary>
public const int GracePeriodTimeout = 1_000;
}

0 comments on commit 575bf8b

Please sign in to comment.