Skip to content

Commit

Permalink
Retry connection on failure, based on Configuration.RetryMode
Browse files Browse the repository at this point in the history
  • Loading branch information
carlst99 committed Apr 16, 2018
1 parent fd67e2c commit 8ed3b42
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/Ether.Network/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,42 @@ public void Connect()

SocketError errorCode = connectSocket.SocketError;

//Did the connection attempt fail?
if (!IsConnected)
{
this.OnSocketError(errorCode);
return;
//Should we reconnect more than once?
if (this.Configuration.RetryMode == ClientRetryOptions.ToLimit)
{
int count = 0;

//Loop until the retry count
while (!IsConnected && count < this.Configuration.MaxRetryAttempts)
{
if (this.Socket.ConnectAsync(connectSocket))
this._autoConnectEvent.WaitOne(Configuration.TimeOut);

errorCode = connectSocket.SocketError;
count++;
}
}
else if (this.Configuration.RetryMode == ClientRetryOptions.Infinite)
{
//Loop infinitely while we haven't made a connection
while (!IsConnected)
{
if (this.Socket.ConnectAsync(connectSocket))
this._autoConnectEvent.WaitOne(Configuration.TimeOut);

errorCode = connectSocket.SocketError;
}
}

//Are we still not connected after possible retry attempts?
if (!IsConnected)
{
this.OnSocketError(errorCode);
return;
}
}

this._sendingQueueWorker.Start();
Expand Down

0 comments on commit 8ed3b42

Please sign in to comment.