Skip to content

Commit

Permalink
Move socket connection logic to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
carlst99 committed Apr 16, 2018
1 parent 424b752 commit 045bbaa
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Ether.Network/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ 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)
{
Expand All @@ -96,21 +93,15 @@ public void Connect()

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

errorCode = connectSocket.SocketError;
errorCode = ConnectSocketToServer(connectSocket);
count++;
}
}
else if (this.Configuration.RetryMode == ClientRetryOptions.Infinite)
{
while (!IsConnected)
{
if (this.Socket.ConnectAsync(connectSocket))
this._autoConnectEvent.WaitOne(Configuration.TimeOut);

errorCode = connectSocket.SocketError;
errorCode = ConnectSocketToServer(connectSocket);
}
}

Expand Down Expand Up @@ -340,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;
}
}
}

0 comments on commit 045bbaa

Please sign in to comment.