Skip to content

Commit

Permalink
Merge pull request #94 from FalconEye36/retryOnConnectionFailure
Browse files Browse the repository at this point in the history
Retry on connection failure
  • Loading branch information
Filipe GP authored Apr 19, 2018
2 parents 913b2c4 + edab07f commit a67caf8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/Ether.Network/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,33 @@ public void Connect()
SocketAsyncEventArgs connectSocket = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
connectSocket.RemoteEndPoint = NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port);

if (this.Socket.ConnectAsync(connectSocket))
this._autoConnectEvent.WaitOne(Configuration.TimeOut);

SocketError errorCode = connectSocket.SocketError;

SocketError errorCode = ConnectSocketToServer(connectSocket);

if (!IsConnected)
{
this.OnSocketError(errorCode);
return;
if (this.Configuration.RetryMode == NetClientRetryOptions.Limited)
{
int count = 0;

while (!IsConnected && count < this.Configuration.MaxRetryAttempts)
{
errorCode = ConnectSocketToServer(connectSocket);
count++;
}
}
else if (this.Configuration.RetryMode == NetClientRetryOptions.Infinite)
{
while (!IsConnected)
{
errorCode = ConnectSocketToServer(connectSocket);
}
}

if (!IsConnected)
{
this.OnSocketError(errorCode);
return;
}
}

this._sendingQueueWorker.Start();
Expand Down Expand Up @@ -313,5 +331,18 @@ protected override void Dispose(bool disposing)

base.Dispose(disposing);
}

/// <summary>
/// Connects a socket to a server
/// </summary>
/// <param name="connectSocket">The socket to connect</param>
/// <returns>The socket error</returns>
private SocketError ConnectSocketToServer(SocketAsyncEventArgs connectSocket)
{
if (this.Socket.ConnectAsync(connectSocket))
this._autoConnectEvent.WaitOne(Configuration.TimeOut);

return connectSocket.SocketError;
}
}
}
22 changes: 22 additions & 0 deletions src/Ether.Network/Client/NetClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public sealed class NetClientConfiguration
private int _bufferSize;
private string _host;
private int _timeOut;
private NetClientRetryOptions _retryMode;
private int _maxRetryAttempts;

/// <summary>
/// Gets or sets the port.
Expand Down Expand Up @@ -51,6 +53,25 @@ public int TimeOut
set => this.SetValue(ref this._timeOut, value);
}

/// <summary>
/// Gets or sets how the client handles failed connections.
/// When using <see cref="NetClientRetryOptions.Limited"/> set <see cref="MaxRetryAttempts"/>
/// </summary>
public NetClientRetryOptions RetryMode
{
get => this._retryMode;
set => this.SetValue(ref this._retryMode, value);
}

/// <summary>
/// Gets or sets the maximum number of times the client will try to reconnect to the server
/// </summary>
public int MaxRetryAttempts
{
get => this._maxRetryAttempts;
set => this.SetValue(ref this._maxRetryAttempts, value);
}

/// <summary>
/// Gets the listening address.
/// </summary>
Expand All @@ -67,6 +88,7 @@ internal NetClientConfiguration(INetClient client)
this._port = 0;
this._host = null;
this._timeOut = 5000;
this.RetryMode = NetClientRetryOptions.OneTime;
}

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions src/Ether.Network/Client/NetClientRetryOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Ether.Network.Client
{
/// <summary>
/// Options defining how a <see cref="NetClient"/> will handle a failed connection attempt
/// </summary>
public enum NetClientRetryOptions
{
/// <summary>
/// The client will only try to connect one time
/// </summary>
OneTime = 0,

/// <summary>
/// The client will try to connect a specific amount of times
/// </summary>
Limited = 1,

/// <summary>
/// The client will try infinitely to connect to the server
/// </summary>
Infinite = 2
}
}

0 comments on commit a67caf8

Please sign in to comment.