Skip to content

Commit 9c89ed4

Browse files
committed
[增加]1. 增加网络发送消息给客户端的时候导致其他消息卡住的问题
1 parent 965af59 commit 9c89ed4

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

GameFrameX.NetWork.Abstractions/INetWorkChannel.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ public interface INetWorkChannel
1818
IRpcSession RpcSession { get; }
1919

2020
/// <summary>
21-
/// 异步写入消息
21+
/// 异步写入消息到网络通道
2222
/// </summary>
23-
/// <param name="msg">消息对象</param>
24-
/// <param name="errorCode">错误码</param>
25-
/// <returns></returns>
23+
/// <param name="msg">要发送的网络消息对象,包含消息内容和相关元数据</param>
24+
/// <param name="errorCode">错误码,默认为0表示无错误。当发生错误时,可以通过此参数传递错误码</param>
25+
/// <returns>表示异步操作的Task对象。当消息成功写入时完成,如果发生错误则抛出异常</returns>
26+
/// <remarks>
27+
/// 此方法用于将消息异步发送到网络通道。
28+
/// 如果errorCode不为0,接收方可以根据错误码进行相应的错误处理。
29+
/// 调用此方法时需要确保网络通道处于打开状态。
30+
/// </remarks>
2631
Task WriteAsync(INetworkMessage msg, int errorCode = 0);
2732

2833
/// <summary>

GameFrameX.NetWork/BaseNetWorkChannel.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class BaseNetWorkChannel : INetWorkChannel
2323
/// </summary>
2424
protected readonly CancellationTokenSource CancellationTokenSource = new();
2525

26+
/// <summary>
27+
/// 网络发送超时时间,单位秒
28+
/// </summary>
29+
protected readonly TimeSpan NetWorkSendTimeOutSecondsTimeSpan;
30+
2631
/// <summary>
2732
/// 初始化
2833
/// </summary>
@@ -37,6 +42,7 @@ public BaseNetWorkChannel(IGameAppSession session, AppSetting setting, IRpcSessi
3742
IsWebSocket = isWebSocket;
3843
Setting = setting;
3944
RpcSession = rpcSession;
45+
NetWorkSendTimeOutSecondsTimeSpan = TimeSpan.FromSeconds(Setting.NetWorkSendTimeOutSeconds);
4046
if (isWebSocket)
4147
{
4248
_webSocketSession = (WebSocketSession)session;
@@ -101,22 +107,22 @@ public virtual async Task WriteAsync(INetworkMessage messageObject, int errorCod
101107
return;
102108
}
103109

104-
if (IsWebSocket)
110+
using (var cts = new CancellationTokenSource(NetWorkSendTimeOutSecondsTimeSpan))
105111
{
106112
try
107113
{
108-
await _webSocketSession.SendAsync(messageData);
109-
}
110-
catch (Exception e)
111-
{
112-
LogHelper.Error(e);
114+
if (IsWebSocket)
115+
{
116+
await _webSocketSession.SendAsync(messageData, cts.Token);
117+
}
118+
else
119+
{
120+
await GameAppSession.SendAsync(messageData, cts.Token);
121+
}
113122
}
114-
}
115-
else
116-
{
117-
try
123+
catch (OperationCanceledException exception)
118124
{
119-
await GameAppSession.SendAsync(messageData);
125+
LogHelper.Error($"消息发送超时被取消:{exception.Message}");
120126
}
121127
catch (Exception e)
122128
{

0 commit comments

Comments
 (0)