diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Actor/DmtpActor.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Actor/DmtpActor.cs
index 392f2aee9..50046660a 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Actor/DmtpActor.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Actor/DmtpActor.cs
@@ -123,25 +123,6 @@ namespace ThingsGateway.Foundation.Dmtp
{
}
- ///
- public void Close(string msg)
- {
- this.OnClosed(true, msg);
- }
-
- ///
- public bool SendClose(string msg)
- {
- try
- {
- this.SendString(0, msg);
- return true;
- }
- catch
- {
- return false;
- }
- }
///
/// 建立对点
@@ -1051,7 +1032,7 @@ namespace ThingsGateway.Foundation.Dmtp
}
}
- #region 重写
+ #region 断开
///
///
@@ -1070,7 +1051,25 @@ namespace ThingsGateway.Foundation.Dmtp
this.WaitHandlePool.SafeDispose();
base.Dispose(disposing);
}
+ ///
+ public void Close(string msg)
+ {
+ this.OnClosed(true, msg);
+ }
+ ///
+ public bool SendClose(string msg)
+ {
+ try
+ {
+ this.SendString(0, msg);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
#endregion 重写
#region 协议同步发送
diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/Http/HttpDmtpClient.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/Http/HttpDmtpClient.cs
index 67f512865..98aa235be 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/Http/HttpDmtpClient.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/Http/HttpDmtpClient.cs
@@ -39,10 +39,10 @@ namespace ThingsGateway.Foundation.Dmtp
#endregion 字段
///
- public string Id => this.DmtpActor.Id;
+ public string Id => this.m_dmtpActor.Id;
///
- public bool IsHandshaked => this.DmtpActor != null && this.DmtpActor.IsHandshaked;
+ public bool IsHandshaked => this.m_dmtpActor != null && this.m_dmtpActor.IsHandshaked;
///
public IDmtpActor DmtpActor { get => this.m_dmtpActor; }
@@ -219,7 +219,10 @@ namespace ThingsGateway.Foundation.Dmtp
///
protected override void Dispose(bool disposing)
{
- this.m_dmtpActor?.SafeDispose();
+ if (this.IsHandshaked)
+ {
+ this.m_dmtpActor?.SafeDispose();
+ }
base.Dispose(disposing);
}
@@ -230,7 +233,7 @@ namespace ThingsGateway.Foundation.Dmtp
///
public override void Close(string msg = "")
{
- if (this.m_dmtpActor != null)
+ if (this.IsHandshaked)
{
this.m_dmtpActor.SendClose(msg);
this.m_dmtpActor.Close(msg);
@@ -303,7 +306,7 @@ namespace ThingsGateway.Foundation.Dmtp
Routing = this.OnDmtpActorRouting,
Handshaking = this.OnDmtpActorHandshaking,
Handshaked = this.OnDmtpActorHandshaked,
- Closed = OnDmtpActorClose,
+ Closed = this.OnDmtpActorClose,
CreatedChannel = this.OnDmtpActorCreateChannel,
Logger = this.Logger,
Client = this,
diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpClient.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpClient.cs
index 89dcde9bc..5057d247d 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpClient.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpClient.cs
@@ -42,7 +42,7 @@ namespace ThingsGateway.Foundation.Dmtp
public IDmtpActor DmtpActor { get => this.m_dmtpActor; }
///
- public string Id => this.DmtpActor.Id;
+ public string Id => this.m_dmtpActor.Id;
#region 字段
@@ -54,7 +54,7 @@ namespace ThingsGateway.Foundation.Dmtp
#endregion 字段
///
- public bool IsHandshaked => this.DmtpActor != null && this.DmtpActor.IsHandshaked;
+ public bool IsHandshaked => this.m_dmtpActor != null && this.m_dmtpActor.IsHandshaked;
#region 断开
@@ -65,15 +65,21 @@ namespace ThingsGateway.Foundation.Dmtp
///
public override void Close(string msg = "")
{
- this.m_dmtpActor?.SendClose(msg);
- this.m_dmtpActor?.Close(msg);
+ if (this.IsHandshaked)
+ {
+ this.m_dmtpActor?.SendClose(msg);
+ this.m_dmtpActor?.Close(msg);
+ }
base.Close(msg);
}
///
protected override void Dispose(bool disposing)
{
- this.m_dmtpActor?.SafeDispose();
+ if (this.IsHandshaked)
+ {
+ this.m_dmtpActor?.SafeDispose();
+ }
base.Dispose(disposing);
}
diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpSocketClient.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpSocketClient.cs
index d58db2308..5fffcedac 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpSocketClient.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Components/TCP/TcpDmtpSocketClient.cs
@@ -161,22 +161,31 @@ namespace ThingsGateway.Foundation.Dmtp
///
public override void Close(string msg = "")
{
- this.m_dmtpActor?.SendClose(msg);
- this.m_dmtpActor?.Close(msg);
+ if (this.IsHandshaked)
+ {
+ this.m_dmtpActor?.SendClose(msg);
+ this.m_dmtpActor?.Close(msg);
+ }
base.Close(msg);
}
///
protected override void Dispose(bool disposing)
{
- this.m_dmtpActor?.SafeDispose();
+ if (this.IsHandshaked)
+ {
+ this.m_dmtpActor?.SafeDispose();
+ }
base.Dispose(disposing);
}
///
protected override async Task OnDisconnected(DisconnectEventArgs e)
{
- this.m_dmtpActor?.Close(e.Message);
+ if (this.IsHandshaked)
+ {
+ this.m_dmtpActor?.Close(e.Message);
+ }
await base.OnDisconnected(e).ConfigureFalseAwait();
}
diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/SerialPort/SerialPort/SerialCore.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/SerialPort/SerialPort/SerialCore.cs
index 2b6572163..f7ceedb02 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/SerialPort/SerialPort/SerialCore.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/SerialPort/SerialPort/SerialCore.cs
@@ -215,6 +215,17 @@ public class SerialCore : IDisposable, ISender
this.m_receiveBufferSize = this.MinBufferSize;
this.m_sendBufferSize = this.MinBufferSize;
}
+ ///
+ /// 判断,当不在连接状态时触发异常。
+ ///
+ ///
+ protected void ThrowIfNotConnected()
+ {
+ if (!this.m_online)
+ {
+ throw new NotConnectedException();
+ }
+ }
///
/// 发送数据。
@@ -227,6 +238,7 @@ public class SerialCore : IDisposable, ISender
///
public virtual void Send(byte[] buffer, int offset, int length)
{
+ this.ThrowIfNotConnected();
try
{
this.m_semaphoreForSend.Wait();
@@ -249,6 +261,7 @@ public class SerialCore : IDisposable, ISender
///
public virtual async Task SendAsync(byte[] buffer, int offset, int length)
{
+ this.ThrowIfNotConnected();
try
{
await this.m_semaphoreForSend.WaitAsync();
diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Components/Core/TcpCore.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Components/Core/TcpCore.cs
index 50014dbd7..ccdb133b3 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Components/Core/TcpCore.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Components/Core/TcpCore.cs
@@ -296,7 +296,17 @@ public class TcpCore : SocketAsyncEventArgs, IDisposable, ISender
this.m_sendBufferSize = this.MinBufferSize;
this.m_online = false;
}
-
+ ///
+ /// 判断,当不在连接状态时触发异常。
+ ///
+ ///
+ protected void ThrowIfNotConnected()
+ {
+ if (!this.m_online)
+ {
+ throw new NotConnectedException();
+ }
+ }
///
/// 发送数据。
///
@@ -308,6 +318,7 @@ public class TcpCore : SocketAsyncEventArgs, IDisposable, ISender
///
public virtual void Send(byte[] buffer, int offset, int length)
{
+ this.ThrowIfNotConnected();
if (this.UseSsl)
{
this.SslStream.Write(buffer, offset, length);
@@ -349,6 +360,7 @@ public class TcpCore : SocketAsyncEventArgs, IDisposable, ISender
///
public virtual async Task SendAsync(byte[] buffer, int offset, int length)
{
+ this.ThrowIfNotConnected();
#if NET6_0_OR_GREATER
if (this.UseSsl)
{
diff --git a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Exceptions/NotConnectedException.cs b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Exceptions/NotConnectedException.cs
index 65389c0eb..0d8e0819f 100644
--- a/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Exceptions/NotConnectedException.cs
+++ b/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Exceptions/NotConnectedException.cs
@@ -34,7 +34,7 @@ namespace ThingsGateway.Foundation.Sockets
///
/// 构造函数
///
- public NotConnectedException()
+ public NotConnectedException() : this(TouchSocketResource.NotConnected.GetDescription())
{ }
///