From ac3acb48ae932cfdbb6dba90f6f58439fa859ef5 Mon Sep 17 00:00:00 2001
From: root <295172551@qq.com>
Date: Sat, 2 Aug 2025 17:48:02 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96WebSocket=E4=BC=A0=E8=BE=93?=
=?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=92=8C=E9=80=9A=E9=81=93=E7=AE=A1=E7=90=86?=
=?UTF-8?q?=20-=20=E9=87=8D=E6=9E=84ConnectInternalAsync=E6=96=B9=E6=B3=95?=
=?UTF-8?q?=EF=BC=8C=E6=8F=90=E5=8F=96=E7=8B=AC=E7=AB=8B=E6=96=B9=E6=B3=95?=
=?UTF-8?q?=20-=20=E4=BC=98=E5=8C=96=E9=80=9A=E9=81=93=E5=88=9B=E5=BB=BA?=
=?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A7=BB=E9=99=A4ResetChannels?=
=?UTF-8?q?=E6=96=B9=E6=B3=95=20-=20=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8?=
=?UTF-8?q?=E9=87=8D=E8=BF=9E=E9=85=8D=E7=BD=AE=E9=80=89=E9=A1=B9EnableAut?=
=?UTF-8?q?oReconnect=20-=20=E4=BF=AE=E5=A4=8DWebSocket=E9=85=8D=E7=BD=AE?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=89=80=E6=9C=89?=
=?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E9=A1=B9=20-=20=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=E6=96=B9=E6=B3=95=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?=
=?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84?=
=?UTF-8?q?async/await?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Configurations/websocket.Development.json | 8 +-
CoreAgent.API/Configurations/websocket.json | 8 +-
.../Network/CellularNetworkService.cs | 2 +-
.../WebSocketTransportExtensions.cs | 6 +-
.../Interfaces/IMessageChannelManager.cs | 16 +-
.../Models/WebSocketConfig.cs | 25 ++-
.../Services/MessageChannelManager.cs | 160 +++++++++++---
.../Services/WebSocketTransport.cs | 106 ++++++++--
modify.md | 196 ++++++++++++++++++
9 files changed, 474 insertions(+), 53 deletions(-)
diff --git a/CoreAgent.API/Configurations/websocket.Development.json b/CoreAgent.API/Configurations/websocket.Development.json
index 64b4f7a..d9c1ae4 100644
--- a/CoreAgent.API/Configurations/websocket.Development.json
+++ b/CoreAgent.API/Configurations/websocket.Development.json
@@ -4,8 +4,14 @@
"TimeoutMs": 15000,
"BatchTimeoutMs": 50,
"MaxBatchSize": 50,
+ "EnableAutoReconnect": true,
"MaxReconnectAttempts": 3,
"QueueCapacity": 500,
- "CacheTtlMinutes": 5
+ "SendChannelCapacity": 500,
+ "ReceiveChannelCapacity": 500,
+ "PriorityChannelCapacity": 50,
+ "CacheTtlMinutes": 5,
+ "MaxChunkSize": 32768,
+ "ChunkDelayMs": 1
}
}
\ No newline at end of file
diff --git a/CoreAgent.API/Configurations/websocket.json b/CoreAgent.API/Configurations/websocket.json
index 3f3662a..e0c67eb 100644
--- a/CoreAgent.API/Configurations/websocket.json
+++ b/CoreAgent.API/Configurations/websocket.json
@@ -4,8 +4,14 @@
"TimeoutMs": 30000,
"BatchTimeoutMs": 100,
"MaxBatchSize": 100,
+ "EnableAutoReconnect": true,
"MaxReconnectAttempts": 5,
"QueueCapacity": 10000,
- "CacheTtlMinutes": 30
+ "SendChannelCapacity": 1000,
+ "ReceiveChannelCapacity": 1000,
+ "PriorityChannelCapacity": 100,
+ "CacheTtlMinutes": 30,
+ "MaxChunkSize": 65536,
+ "ChunkDelayMs": 1
}
}
\ No newline at end of file
diff --git a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
index ac43610..31e2920 100644
--- a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
+++ b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
@@ -157,7 +157,7 @@ public class CellularNetworkService : ICellularNetworkService
if (!isRanQuit)
{
_logger.LogWarning("RAN 退出状态检查失败");
- return CellularNetworkOperationResult.Failure("RAN 退出状态检查失败");
+ //return CellularNetworkOperationResult.Failure("RAN 退出状态检查失败");
}
}
diff --git a/CoreAgent.WebSocketTransport/Extensions/WebSocketTransportExtensions.cs b/CoreAgent.WebSocketTransport/Extensions/WebSocketTransportExtensions.cs
index 6d19886..df6c240 100644
--- a/CoreAgent.WebSocketTransport/Extensions/WebSocketTransportExtensions.cs
+++ b/CoreAgent.WebSocketTransport/Extensions/WebSocketTransportExtensions.cs
@@ -81,9 +81,9 @@ namespace CoreAgent.WebSocketTransport.Extensions
return new MessageChannelManager(
logger,
- config.QueueCapacity,
- config.QueueCapacity,
- 100);
+ config.SendChannelCapacity,
+ config.ReceiveChannelCapacity,
+ config.PriorityChannelCapacity);
});
// 注册 WebSocket 传输
diff --git a/CoreAgent.WebSocketTransport/Interfaces/IMessageChannelManager.cs b/CoreAgent.WebSocketTransport/Interfaces/IMessageChannelManager.cs
index efddc8f..048bd29 100644
--- a/CoreAgent.WebSocketTransport/Interfaces/IMessageChannelManager.cs
+++ b/CoreAgent.WebSocketTransport/Interfaces/IMessageChannelManager.cs
@@ -30,14 +30,26 @@ public interface IMessageChannelManager : IDisposable
ChannelStatusInfo GetStatusInfo();
///
- /// 清空所有通道
+ /// 创建所有通道(如果已存在则跳过)
+ ///
+ void CreateChannels();
+
+ ///
+ /// 清空所有通道中的消息(保持通道可用)
///
void ClearAllChannels();
///
- /// 完成所有通道
+ /// 完成所有通道(标记不再接受新消息,但保持可读)
///
void CompleteAllChannels();
+
+ ///
+ /// 释放所有通道(完全释放资源)
+ ///
+ void ReleaseChannels();
+
+
}
///
diff --git a/CoreAgent.WebSocketTransport/Models/WebSocketConfig.cs b/CoreAgent.WebSocketTransport/Models/WebSocketConfig.cs
index 78baf87..aa10843 100644
--- a/CoreAgent.WebSocketTransport/Models/WebSocketConfig.cs
+++ b/CoreAgent.WebSocketTransport/Models/WebSocketConfig.cs
@@ -33,6 +33,11 @@ public class WebSocketConfig
[Range(1, 10000, ErrorMessage = "最大批量大小必须在 1-10000 之间")]
public int MaxBatchSize { get; set; } = 100;
+ ///
+ /// 是否启用自动重连功能
+ ///
+ public bool EnableAutoReconnect { get; set; } = true;
+
///
/// 最大重连尝试次数
///
@@ -40,11 +45,29 @@ public class WebSocketConfig
public int MaxReconnectAttempts { get; set; } = 5;
///
- /// 队列容量
+ /// 队列容量(已废弃,请使用 SendChannelCapacity、ReceiveChannelCapacity、PriorityChannelCapacity)
///
[Range(10, 100000, ErrorMessage = "队列容量必须在 10-100000 之间")]
public int QueueCapacity { get; set; } = 10000;
+ ///
+ /// 发送通道容量
+ ///
+ [Range(10, 100000, ErrorMessage = "发送通道容量必须在 10-100000 之间")]
+ public int SendChannelCapacity { get; set; } = 1000;
+
+ ///
+ /// 接收通道容量
+ ///
+ [Range(10, 100000, ErrorMessage = "接收通道容量必须在 10-100000 之间")]
+ public int ReceiveChannelCapacity { get; set; } = 1000;
+
+ ///
+ /// 优先级通道容量
+ ///
+ [Range(10, 10000, ErrorMessage = "优先级通道容量必须在 10-10000 之间")]
+ public int PriorityChannelCapacity { get; set; } = 100;
+
///
/// 缓存消息 TTL(分钟)
///
diff --git a/CoreAgent.WebSocketTransport/Services/MessageChannelManager.cs b/CoreAgent.WebSocketTransport/Services/MessageChannelManager.cs
index b9f57f3..39d815e 100644
--- a/CoreAgent.WebSocketTransport/Services/MessageChannelManager.cs
+++ b/CoreAgent.WebSocketTransport/Services/MessageChannelManager.cs
@@ -14,9 +14,17 @@ public class MessageChannelManager : IMessageChannelManager
private volatile bool _disposed;
private readonly object _disposeLock = new object();
- public IMessageChannel SendChannel { get; }
- public IMessageChannel