Commit 4b5c12ec authored by Andreas Müller's avatar Andreas Müller

Developer tests performed and small fixes.

- Tested the connect, disconnect and reconnect behaviour
- Client has events Connected and Disconnected
- Improved error handling
parent d7e444ce
......@@ -28,6 +28,20 @@ namespace Modbus.Tcp.Client
#endregion Fields
#region Events
/// <summary>
/// Raised when the client has the connection successfully established.
/// </summary>
public event EventHandler Connected;
/// <summary>
/// Raised when the client has closed the connection.
/// </summary>
public event EventHandler Disconnected;
#endregion
#region Constructors
/// <summary>
......@@ -122,7 +136,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request);
if (response.IsTimeout)
{
throw new ModbusException("Response timed out. Device id invalid?");
throw new SocketException((int)SocketError.TimedOut);
}
if (response.IsError)
{
......@@ -199,7 +213,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request);
if (response.IsTimeout)
{
throw new ModbusException("Response timed out. Device id invalid?");
throw new SocketException((int)SocketError.TimedOut);
}
if (response.IsError)
{
......@@ -276,7 +290,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request);
if (response.IsTimeout)
{
throw new ModbusException("Response timed out. Device id invalid?");
throw new SocketException((int)SocketError.TimedOut);
}
if (response.IsError)
{
......@@ -349,7 +363,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request);
if (response.IsTimeout)
{
throw new ModbusException("Response timed out. Device id invalid?");
throw new SocketException((int)SocketError.TimedOut);
}
if (response.IsError)
{
......@@ -720,6 +734,11 @@ namespace Modbus.Tcp.Client
return;
}
if (wasConnected)
{
Task.Run(() => Disconnected?.Invoke(this, EventArgs.Empty));
}
var timeout = 4;
var maxTimeout = 20;
var startTime = DateTime.UtcNow;
......@@ -771,6 +790,7 @@ namespace Modbus.Tcp.Client
wasConnected = true;
}
Task.Run(() => Connected?.Invoke(this, EventArgs.Empty));
break;
}
}
......@@ -831,7 +851,6 @@ namespace Modbus.Tcp.Client
{
if (disposing)
{
tcpClient?.GetStream()?.Dispose();
tcpClient?.Dispose();
tcpClient = null;
}
......
......@@ -207,8 +207,7 @@ namespace Modbus.Tcp.Protocol
/// <inheritdoc/>
public override bool Equals(object obj)
{
var req = obj as Request;
if (req == null)
if (!(obj is Request req))
{
return false;
}
......
......@@ -24,7 +24,7 @@ namespace Modbus.Tcp.Server
private TcpListener tcpListener;
private List<TcpClient> tcpClients = new List<TcpClient>();
private FunctionCode[] availableFunctionCodes = Enum.GetValues(typeof(FunctionCode))
private readonly FunctionCode[] availableFunctionCodes = Enum.GetValues(typeof(FunctionCode))
.Cast<FunctionCode>()
.ToArray();
......@@ -424,10 +424,19 @@ namespace Modbus.Tcp.Server
}
catch (EndOfStreamException)
{
// client closed connection
// client closed connection (connecting)
}
catch (IOException)
{
// server stopped
}
catch (ArgumentOutOfRangeException)
{
// client closed connection (request parsing)
}
ClientDisconnected?.Invoke(this, new ClientEventArgs((IPEndPoint)client.Client.RemoteEndPoint));
if (!isDisposed)
ClientDisconnected?.Invoke(this, new ClientEventArgs((IPEndPoint)client.Client.RemoteEndPoint));
lock (tcpClients)
{
......
......@@ -791,15 +791,18 @@ namespace Modbus.Tcp.Utils
/// <inheritdoc/>
public override bool Equals(object obj)
{
var block = obj as DataBuffer;
if (block == null)
if (!(obj is DataBuffer block))
{
return false;
}
if (block.IsLittleEndian != IsLittleEndian)
{
return false;
}
if (block.Length != Length)
{
return false;
}
return IsEqual(0, block.Buffer);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment