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 ...@@ -28,6 +28,20 @@ namespace Modbus.Tcp.Client
#endregion Fields #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 #region Constructors
/// <summary> /// <summary>
...@@ -122,7 +136,7 @@ namespace Modbus.Tcp.Client ...@@ -122,7 +136,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request); var response = await SendRequest(request);
if (response.IsTimeout) if (response.IsTimeout)
{ {
throw new ModbusException("Response timed out. Device id invalid?"); throw new SocketException((int)SocketError.TimedOut);
} }
if (response.IsError) if (response.IsError)
{ {
...@@ -199,7 +213,7 @@ namespace Modbus.Tcp.Client ...@@ -199,7 +213,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request); var response = await SendRequest(request);
if (response.IsTimeout) if (response.IsTimeout)
{ {
throw new ModbusException("Response timed out. Device id invalid?"); throw new SocketException((int)SocketError.TimedOut);
} }
if (response.IsError) if (response.IsError)
{ {
...@@ -276,7 +290,7 @@ namespace Modbus.Tcp.Client ...@@ -276,7 +290,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request); var response = await SendRequest(request);
if (response.IsTimeout) if (response.IsTimeout)
{ {
throw new ModbusException("Response timed out. Device id invalid?"); throw new SocketException((int)SocketError.TimedOut);
} }
if (response.IsError) if (response.IsError)
{ {
...@@ -349,7 +363,7 @@ namespace Modbus.Tcp.Client ...@@ -349,7 +363,7 @@ namespace Modbus.Tcp.Client
var response = await SendRequest(request); var response = await SendRequest(request);
if (response.IsTimeout) if (response.IsTimeout)
{ {
throw new ModbusException("Response timed out. Device id invalid?"); throw new SocketException((int)SocketError.TimedOut);
} }
if (response.IsError) if (response.IsError)
{ {
...@@ -720,6 +734,11 @@ namespace Modbus.Tcp.Client ...@@ -720,6 +734,11 @@ namespace Modbus.Tcp.Client
return; return;
} }
if (wasConnected)
{
Task.Run(() => Disconnected?.Invoke(this, EventArgs.Empty));
}
var timeout = 4; var timeout = 4;
var maxTimeout = 20; var maxTimeout = 20;
var startTime = DateTime.UtcNow; var startTime = DateTime.UtcNow;
...@@ -771,6 +790,7 @@ namespace Modbus.Tcp.Client ...@@ -771,6 +790,7 @@ namespace Modbus.Tcp.Client
wasConnected = true; wasConnected = true;
} }
Task.Run(() => Connected?.Invoke(this, EventArgs.Empty));
break; break;
} }
} }
...@@ -831,7 +851,6 @@ namespace Modbus.Tcp.Client ...@@ -831,7 +851,6 @@ namespace Modbus.Tcp.Client
{ {
if (disposing) if (disposing)
{ {
tcpClient?.GetStream()?.Dispose();
tcpClient?.Dispose(); tcpClient?.Dispose();
tcpClient = null; tcpClient = null;
} }
......
...@@ -207,8 +207,7 @@ namespace Modbus.Tcp.Protocol ...@@ -207,8 +207,7 @@ namespace Modbus.Tcp.Protocol
/// <inheritdoc/> /// <inheritdoc/>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var req = obj as Request; if (!(obj is Request req))
if (req == null)
{ {
return false; return false;
} }
......
...@@ -24,7 +24,7 @@ namespace Modbus.Tcp.Server ...@@ -24,7 +24,7 @@ namespace Modbus.Tcp.Server
private TcpListener tcpListener; private TcpListener tcpListener;
private List<TcpClient> tcpClients = new List<TcpClient>(); private List<TcpClient> tcpClients = new List<TcpClient>();
private FunctionCode[] availableFunctionCodes = Enum.GetValues(typeof(FunctionCode)) private readonly FunctionCode[] availableFunctionCodes = Enum.GetValues(typeof(FunctionCode))
.Cast<FunctionCode>() .Cast<FunctionCode>()
.ToArray(); .ToArray();
...@@ -424,10 +424,19 @@ namespace Modbus.Tcp.Server ...@@ -424,10 +424,19 @@ namespace Modbus.Tcp.Server
} }
catch (EndOfStreamException) 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) lock (tcpClients)
{ {
......
...@@ -791,15 +791,18 @@ namespace Modbus.Tcp.Utils ...@@ -791,15 +791,18 @@ namespace Modbus.Tcp.Utils
/// <inheritdoc/> /// <inheritdoc/>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var block = obj as DataBuffer; if (!(obj is DataBuffer block))
if (block == null) {
return false; return false;
}
if (block.IsLittleEndian != IsLittleEndian) if (block.IsLittleEndian != IsLittleEndian)
{
return false; return false;
}
if (block.Length != Length) if (block.Length != Length)
{
return false; return false;
}
return IsEqual(0, block.Buffer); 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