This commit is contained in:
parallelbgls
2018-01-04 11:30:35 +08:00
parent 5c156e8150
commit e9b41eb06a
3 changed files with 17 additions and 12 deletions

View File

@@ -456,6 +456,7 @@ namespace Modbus.Net
catch (Exception err)
{
Log.Error(err, "Com client {ConnectionToken} send msg error", ConnectionToken);
Dispose();
}
RefreshSendCount();
}

View File

@@ -199,7 +199,7 @@ namespace Modbus.Net
catch (Exception err)
{
Log.Error(err, "Tcp client {ConnectionToken} send exception", ConnectionToken);
CloseClientSocket();
Dispose();
}
}
@@ -225,6 +225,7 @@ namespace Modbus.Net
{
while (!_taskCancel)
{
if (_socketClient == null) break;
NetworkStream stream = _socketClient.GetStream();
var len = await stream.ReadAsync(_receiveBuffer, 0, _receiveBuffer.Length);
stream.Flush();
@@ -298,12 +299,15 @@ namespace Modbus.Net
Controller.SendStop();
Controller.Clear();
ReceiveMsgThreadStop();
if (_socketClient != null)
{
if (_socketClient.Connected)
{
_socketClient?.GetStream().Dispose();
}
_socketClient?.Close();
}
}
catch (Exception ex)
{
Log.Error(ex, "Tcp client {ConnectionToken} client close exception", ConnectionToken);

View File

@@ -180,7 +180,7 @@ namespace Modbus.Net
catch (Exception err)
{
Log.Error(err, "Udp client {ConnectionToken} send exception", ConnectionToken);
CloseClientSocket();
Dispose();
}
}
@@ -206,6 +206,7 @@ namespace Modbus.Net
{
while (!_taskCancel)
{
if (_socketClient == null) break;
var receive = await _socketClient.ReceiveAsync();
var len = receive.Buffer.Length;
@@ -265,20 +266,19 @@ namespace Modbus.Net
Controller.SendStop();
Controller.Clear();
ReceiveMsgThreadStop();
if (_socketClient.Client.Connected)
if (_socketClient != null)
{
_socketClient?.Client.Disconnect(false);
if (_socketClient.Client?.Connected == true)
{
_socketClient.Client.Disconnect(false);
}
_socketClient.Close();
}
_socketClient?.Close();
}
catch (Exception ex)
{
Log.Error(ex, "Udp client {ConnectionToken} client close exception", ConnectionToken);
}
finally
{
_socketClient = null;
}
}
}
}