feature: added changing ReadTimeout and WriteTimeout for connected tcpClient.

This commit is contained in:
Смирнов Виталий
2019-05-28 08:56:06 +03:00
parent 0dbe401ce9
commit 800a790b89
3 changed files with 31 additions and 11 deletions

View File

@@ -15,6 +15,9 @@ namespace S7.Net
private TcpClient tcpClient;
private NetworkStream stream;
private int _readTimeout = System.Threading.Timeout.Infinite;
private int _writeTimeout = System.Threading.Timeout.Infinite;
/// <summary>
/// IP address of the PLC
/// </summary>
@@ -44,17 +47,25 @@ namespace S7.Net
/// <returns>A <see cref="T:System.Int32" /> that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, <see cref="F:System.Threading.Timeout.Infinite" />, specifies that the read operation does not time out.</returns>
public int ReadTimeout
{
get;
set;
} = System.Threading.Timeout.Infinite;
get => _readTimeout;
set
{
_readTimeout = value;
ConfigureConnection();
}
}
/// <summary>Gets or sets the amount of time that a write operation blocks waiting for data to PLC. </summary>
/// <returns>A <see cref="T:System.Int32" /> that specifies the amount of time, in milliseconds, that will elapse before a write operation fails. The default value, <see cref="F:System.Threading.Timeout.Infinite" />, specifies that the write operation does not time out.</returns>
public int WriteTimeout
{
get;
set;
} = System.Threading.Timeout.Infinite;
get => _writeTimeout;
set
{
_writeTimeout = value;
ConfigureConnection();
}
}
/// <summary>
/// Returns true if a connection to the PLC can be established
@@ -122,6 +133,7 @@ namespace S7.Net
Slot = slot;
MaxPDUSize = 240;
}
/// <summary>
/// Close connection to PLC
/// </summary>
@@ -133,10 +145,18 @@ namespace S7.Net
}
}
private void ConfigureNetworkStream(NetworkStream networkStream)
private void ConfigureConnection()
{
networkStream.ReadTimeout = ReadTimeout;
networkStream.WriteTimeout = WriteTimeout;
if (tcpClient != null)
{
ConfigureConnection(tcpClient);
}
}
private void ConfigureConnection(TcpClient client)
{
client.ReceiveTimeout = ReadTimeout;
client.SendTimeout = WriteTimeout;
}
#region IDisposable Support

View File

@@ -44,9 +44,9 @@ namespace S7.Net
private async Task ConnectAsync()
{
tcpClient = new TcpClient();
ConfigureConnection(tcpClient);
await tcpClient.ConnectAsync(IP, 102);
stream = tcpClient.GetStream();
ConfigureNetworkStream(stream);
}
/// <summary>

View File

@@ -50,9 +50,9 @@ namespace S7.Net
try
{
tcpClient = new TcpClient();
ConfigureConnection(tcpClient);
tcpClient.Connect(IP, 102);
stream = tcpClient.GetStream();
ConfigureNetworkStream(stream);
}
catch (SocketException sex)
{