Skip to content

Commit

Permalink
Merge pull request #89 from SheppeR/develop
Browse files Browse the repository at this point in the history
Basic Time out for NetClient (trigger OnSocketError on fail to connect)
  • Loading branch information
Filipe GP authored Mar 20, 2018
2 parents 3063516 + 7ae3100 commit 913b2c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion samples/SampleClient/MyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ internal sealed class MyClient : NetClient
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="bufferSize"></param>
public MyClient(string host, int port, int bufferSize)
public MyClient(string host, int port, int bufferSize, int timeOut)
{
this.Configuration.Host = host;
this.Configuration.Port = port;
this.Configuration.BufferSize = bufferSize;
this.Configuration.TimeOut = timeOut;
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion samples/SampleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ internal class Program
{
private static void Main()
{
var client = new MyClient("127.0.0.1", 4444, 512);
var client = new MyClient("127.0.0.1", 4444, 512, 5000);
client.Connect();

if (!client.IsConnected)
{
Console.WriteLine("Can't connect to server!");
Console.ReadLine();
return;
}

Console.WriteLine("Enter a message and press enter...");
int i = 0;
var random = new Random();
Expand Down
9 changes: 6 additions & 3 deletions src/Ether.Network/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ public void Connect()
connectSocket.RemoteEndPoint = NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port);

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

SocketError errorCode = connectSocket.SocketError;

if (errorCode != SocketError.Success)
throw new SocketException((int) errorCode);
if (!IsConnected)
{
this.OnSocketError(errorCode);
return;
}

this._sendingQueueWorker.Start();
this._receivingQueueWorker.Start();
Expand Down
11 changes: 11 additions & 0 deletions src/Ether.Network/Client/NetClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class NetClientConfiguration
private int _port;
private int _bufferSize;
private string _host;
private int _timeOut;

/// <summary>
/// Gets or sets the port.
Expand Down Expand Up @@ -41,6 +42,15 @@ public string Host
set => this.SetValue(ref this._host, value);
}

/// <summary>
/// Gets or sets the connecting time out.
/// </summary>
public int TimeOut
{
get => this._timeOut;
set => this.SetValue(ref this._timeOut, value);
}

/// <summary>
/// Gets the listening address.
/// </summary>
Expand All @@ -56,6 +66,7 @@ internal NetClientConfiguration(INetClient client)
this._bufferSize = 1024;
this._port = 0;
this._host = null;
this._timeOut = 5000;
}

/// <summary>
Expand Down

0 comments on commit 913b2c4

Please sign in to comment.