Skip to content

Commit ac8c66e

Browse files
committed
chore: 统一处理警告
1 parent 5963809 commit ac8c66e

18 files changed

Lines changed: 91 additions & 50 deletions

File tree

Cyaim.WebSocketServer/Cyaim.WebSocketServer.Client/Cyaim.WebSocketServer.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
78
</PropertyGroup>
89

910
<ItemGroup>

Cyaim.WebSocketServer/Cyaim.WebSocketServer.Cluster.RabbitMQ/RabbitMQClusterTransport.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public class RabbitMQClusterTransport : IClusterTransport
5757
/// <summary>
5858
/// Event triggered when node disconnected / 节点断开连接时触发的事件
5959
/// </summary>
60+
#pragma warning disable CS0067 // 事件从未使用,但可能是接口要求的一部分
6061
public event EventHandler<ClusterNodeEventArgs> NodeDisconnected;
62+
#pragma warning restore CS0067
6163

6264
/// <summary>
6365
/// Constructor / 构造函数

Cyaim.WebSocketServer/Cyaim.WebSocketServer.Cluster.StackExchangeRedis/RedisClusterTransport.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ namespace Cyaim.WebSocketServer.Cluster.StackExchangeRedis
1818
/// </summary>
1919
public class RedisClusterTransport : IClusterTransport
2020
{
21+
/// <summary>
22+
/// Redis channel prefix for node-specific messages / Redis 节点特定消息的通道前缀
23+
/// </summary>
24+
private const string ClusterNodeChannelPrefix = "cluster:node:";
25+
26+
/// <summary>
27+
/// Redis channel name for broadcast messages / Redis 广播消息的通道名称
28+
/// </summary>
29+
private const string ClusterBroadcastChannel = "cluster:broadcast";
30+
2131
private readonly ILogger<RedisClusterTransport> _logger;
2232
private readonly string _nodeId;
2333
private readonly string _connectionString;
@@ -45,7 +55,9 @@ public class RedisClusterTransport : IClusterTransport
4555
/// <summary>
4656
/// Event triggered when node disconnected / 节点断开连接时触发的事件
4757
/// </summary>
58+
#pragma warning disable CS0067 // 事件从未使用,但可能是接口要求的一部分
4859
public event EventHandler<ClusterNodeEventArgs> NodeDisconnected;
60+
#pragma warning restore CS0067
4961

5062
/// <summary>
5163
/// Constructor / 构造函数
@@ -75,7 +87,7 @@ public async Task StartAsync()
7587
_subscriber = _redis.GetSubscriber();
7688

7789
// Subscribe to node-specific channel / 订阅节点特定通道
78-
await _subscriber.SubscribeAsync($"cluster:node:{_nodeId}", (channel, message) =>
90+
await _subscriber.SubscribeAsync(RedisChannel.Literal($"{ClusterNodeChannelPrefix}{_nodeId}"), (channel, message) =>
7991
{
8092
try
8193
{
@@ -107,7 +119,7 @@ await _subscriber.SubscribeAsync($"cluster:node:{_nodeId}", (channel, message) =
107119
});
108120

109121
// Subscribe to broadcast channel / 订阅广播通道
110-
await _subscriber.SubscribeAsync("cluster:broadcast", (channel, message) =>
122+
await _subscriber.SubscribeAsync(RedisChannel.Literal(ClusterBroadcastChannel), (channel, message) =>
111123
{
112124
try
113125
{
@@ -162,8 +174,8 @@ public async Task StopAsync()
162174
{
163175
if (_subscriber != null)
164176
{
165-
await _subscriber.UnsubscribeAsync($"cluster:node:{_nodeId}");
166-
await _subscriber.UnsubscribeAsync("cluster:broadcast");
177+
await _subscriber.UnsubscribeAsync(RedisChannel.Literal($"{ClusterNodeChannelPrefix}{_nodeId}"));
178+
await _subscriber.UnsubscribeAsync(RedisChannel.Literal(ClusterBroadcastChannel));
167179
}
168180

169181
_redis?.Close();
@@ -201,7 +213,7 @@ public async Task SendAsync(string nodeId, ClusterMessage message)
201213
return;
202214
}
203215

204-
await _subscriber.PublishAsync($"cluster:node:{nodeId}", messageJson);
216+
await _subscriber.PublishAsync(RedisChannel.Literal($"{ClusterNodeChannelPrefix}{nodeId}"), messageJson);
205217

206218
_logger.LogDebug($"Sent message to node {nodeId} via Redis");
207219
}
@@ -229,7 +241,7 @@ public async Task BroadcastAsync(ClusterMessage message)
229241
return;
230242
}
231243

232-
await _subscriber.PublishAsync("cluster:broadcast", messageJson);
244+
await _subscriber.PublishAsync(RedisChannel.Literal(ClusterBroadcastChannel), messageJson);
233245

234246
_logger.LogDebug("Broadcasted message via Redis");
235247
}

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/AccessControl/PriorityListService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Concurrent;
34
using System.Collections.Generic;

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/AccessControl/QpsPriorityPolicy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System.Collections.Generic;
23

34
namespace Cyaim.WebSocketServer.Infrastructure.AccessControl

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/WebSocketManager.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,17 @@ private static async Task SendStreamDataAsync(WebSocket webSocket, WebSocketMess
267267
var buffer = ArrayPool<byte>.Shared.Rent((int)stream.Length);
268268
try
269269
{
270-
await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
271-
await webSocket.SendAsync(buffer, messageType, endOfMessage: true, cancellationToken);
270+
int totalBytesRead = 0;
271+
int bytesRead;
272+
while (totalBytesRead < buffer.Length && (bytesRead = await stream.ReadAsync(buffer, totalBytesRead, buffer.Length - totalBytesRead, cancellationToken)) > 0)
273+
{
274+
totalBytesRead += bytesRead;
275+
}
276+
277+
if (totalBytesRead > 0)
278+
{
279+
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, totalBytesRead), messageType, endOfMessage: true, cancellationToken);
280+
}
272281
}
273282
catch (Exception)
274283
{

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Middlewares/WebSocketRouteMiddlewareExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ public static void GetWebSocketAddress()
139139
/// <returns></returns>
140140
public static IApplicationBuilder UseWebSocketServer(this IApplicationBuilder app, IServiceProvider serviceProvider, Action<ClusterOption> clusterOption)
141141
{
142-
throw new NotImplementedException();
143-
144142
if (app == null)
145143
{
146144
throw new ArgumentNullException(nameof(app));
374 KB
Binary file not shown.

Cyaim.WebSocketServer/Dashboard/Cyaim.WebSocketServer.Dashboard/Controllers/ClientController.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34
using System.Linq;
@@ -64,11 +65,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetAll([FromQuery]
6465
}
6566

6667
// Get connection info / 获取连接信息
67-
ClientConnectionInfo clientInfo = null;
68+
ClientConnectionInfo? clientInfo = null;
6869

6970
// Get connection metadata from cluster manager / 从集群管理器获取连接元数据
70-
ConnectionMetadata metadata = null;
71-
string endpoint = null;
71+
ConnectionMetadata? metadata = null;
72+
string? endpoint = null;
7273
if (clusterManager != null)
7374
{
7475
metadata = clusterManager.GetConnectionMetadata(connectionId);
@@ -85,11 +86,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetAll([FromQuery]
8586
{
8687
ConnectionId = connectionId,
8788
NodeId = targetNodeId,
88-
RemoteIpAddress = metadata?.RemoteIpAddress,
89+
RemoteIpAddress = metadata?.RemoteIpAddress ?? string.Empty,
8990
RemotePort = metadata?.RemotePort ?? 0,
9091
State = webSocket.State.ToString(),
9192
ConnectedAt = metadata?.ConnectedAt,
92-
Endpoint = endpoint,
93+
Endpoint = endpoint ?? string.Empty,
9394
BytesSent = stats?.BytesSent ?? 0,
9495
BytesReceived = stats?.BytesReceived ?? 0,
9596
MessagesSent = stats?.MessagesSent ?? 0,
@@ -109,11 +110,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetAll([FromQuery]
109110
{
110111
ConnectionId = connectionId,
111112
NodeId = targetNodeId,
112-
RemoteIpAddress = metadata?.RemoteIpAddress,
113+
RemoteIpAddress = metadata?.RemoteIpAddress ?? string.Empty,
113114
RemotePort = metadata?.RemotePort ?? 0,
114115
State = state,
115116
ConnectedAt = metadata?.ConnectedAt,
116-
Endpoint = endpoint,
117+
Endpoint = endpoint ?? string.Empty,
117118
BytesSent = 0,
118119
BytesReceived = 0,
119120
MessagesSent = 0,
@@ -183,7 +184,7 @@ public ActionResult<ApiResponse<ClientConnectionInfo>> Get(string connectionId)
183184
});
184185
}
185186

186-
var client = clientsResult.Value.Data.FirstOrDefault(c => c.ConnectionId == connectionId);
187+
var client = clientsResult.Value!.Data!.FirstOrDefault(c => c.ConnectionId == connectionId);
187188
if (client == null)
188189
{
189190
return NotFound(new ApiResponse<ClientConnectionInfo>

Cyaim.WebSocketServer/Dashboard/Cyaim.WebSocketServer.Dashboard/Controllers/ClusterController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34
using System.Linq;

0 commit comments

Comments
 (0)