Skip to content

Commit 688de90

Browse files
committed
feat(network): 为网络关闭添加原因和错误码参数
扩展网络关闭功能,增加关闭原因和错误码参数,便于调试和日志记录 修改相关接口和实现,统一关闭时的信息传递 添加网络关闭原因和错误码的枚举定义
1 parent 084d11b commit 688de90

File tree

10 files changed

+105
-30
lines changed

10 files changed

+105
-30
lines changed

Editor/Inspector/NetworkComponentInspector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void DrawNetworkChannel(INetworkChannel networkChannel)
105105
{
106106
if (GUILayout.Button("Disconnect"))
107107
{
108-
networkChannel.Close();
108+
networkChannel.Close(NetworkCloseReason.ConnectClose, (ushort)NetworkErrorCode.DisposeError);
109109
}
110110
}
111111
EditorGUI.EndDisabledGroup();

Runtime/EventArgs/NetworkClosedEventArgs.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,29 @@ public NetworkClosedEventArgs()
7070
/// 创建网络连接关闭事件。
7171
/// </summary>
7272
/// <param name="networkChannel">网络频道。</param>
73+
/// <param name="reason">关闭原因。</param>
74+
/// <param name="errorCode">错误码。</param>
7375
/// <returns>创建的网络连接关闭事件。</returns>
74-
public static NetworkClosedEventArgs Create(INetworkChannel networkChannel)
76+
public static NetworkClosedEventArgs Create(INetworkChannel networkChannel, string reason, ushort errorCode)
7577
{
7678
NetworkClosedEventArgs networkClosedEventArgs = ReferencePool.Acquire<NetworkClosedEventArgs>();
7779
networkClosedEventArgs.NetworkChannel = networkChannel;
80+
networkClosedEventArgs.Reason = reason;
81+
networkClosedEventArgs.ErrorCode = errorCode;
7882
return networkClosedEventArgs;
7983
}
8084

85+
/// <summary>
86+
/// 获取关闭原因。
87+
/// </summary>
88+
public string Reason { get; private set; }
89+
90+
/// <summary>
91+
/// 获取错误码。
92+
/// </summary>
93+
public ushort ErrorCode { get; private set; }
94+
95+
8196
/// <summary>
8297
/// 清理网络连接关闭事件。
8398
/// </summary>

Runtime/Network/Base/NetworkErrorCode.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,48 @@
3131

3232
namespace GameFrameX.Network.Runtime
3333
{
34+
/// <summary>
35+
/// 网络关闭原因。
36+
/// </summary>
37+
[UnityEngine.Scripting.Preserve]
38+
public static class NetworkCloseReason
39+
{
40+
/// <summary>
41+
/// 正常关闭。
42+
/// </summary>
43+
public const string Normal = "Normal";
44+
45+
/// <summary>
46+
/// 超时关闭。
47+
/// </summary>
48+
public const string Timeout = "Timeout";
49+
50+
/// <summary>
51+
/// 资源释放关闭。
52+
/// </summary>
53+
public const string Dispose = "Dispose";
54+
55+
/// <summary>
56+
/// 连接关闭。
57+
/// </summary>
58+
public const string ConnectClose = "ConnectClose";
59+
60+
/// <summary>
61+
/// 连接地址错误关闭。
62+
/// </summary>
63+
public const string ConnectAddressError = "ConnectAddressError";
64+
65+
/// <summary>
66+
/// 连接地址异常错误关闭。
67+
/// </summary>
68+
public const string ConnectAddressExceptionError = "ConnectAddressExceptionError";
69+
70+
/// <summary>
71+
/// 缺失心跳关闭。
72+
/// </summary>
73+
public const string MissHeartBeat = "MissHeartBeat";
74+
}
75+
3476
/// <summary>
3577
/// 网络错误码。
3678
/// </summary>
@@ -80,6 +122,16 @@ public enum NetworkErrorCode : byte
80122
/// <summary>
81123
/// 反序列化消息包错误。
82124
/// </summary>
83-
DeserializePacketError
125+
DeserializePacketError,
126+
127+
/// <summary>
128+
/// 缺失心跳错误。
129+
/// </summary>
130+
MissHeartBeatError,
131+
132+
/// <summary>
133+
/// 资源释放错误。
134+
/// </summary>
135+
DisposeError,
84136
}
85137
}

Runtime/Network/Helper/DefaultNetworkChannelHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public bool DeserializePacketBody(byte[] source, int messageId, out MessageObjec
165165

166166
public void Clear()
167167
{
168-
m_NetworkChannel?.Close();
168+
m_NetworkChannel?.Close(NetworkCloseReason.Dispose, (ushort)NetworkErrorCode.DisposeError);
169169
m_NetworkChannel = null;
170170
}
171171

@@ -186,7 +186,7 @@ private void OnNetworkClosedEventArgs(object sender, GameEventArgs e)
186186
return;
187187
}
188188

189-
Log.Debug($"网络连接关闭......{ne.NetworkChannel.Name}");
189+
Log.Debug($"网络连接关闭......{ne.NetworkChannel.Name}, 关闭原因: {ne.Reason}, 错误码: {ne.ErrorCode}");
190190
}
191191

192192
private void OnNetworkMissHeartBeatEventArgs(object sender, GameEventArgs e)
@@ -207,7 +207,7 @@ private void OnNetworkErrorEventArgs(object sender, GameEventArgs e)
207207
}
208208

209209
Log.Error(Utility.Text.Format("Network channel '{0}' error, error code is '{1}', error message is '{2}'.", ne.NetworkChannel.Name, ne.ErrorCode, ne.ErrorMessage));
210-
ne.NetworkChannel.Close();
210+
ne.NetworkChannel.Close(ne.ErrorMessage, (ushort)ne.ErrorCode);
211211
}
212212
}
213213
}

Runtime/Network/Interface/INetworkChannel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ public interface INetworkChannel
221221
/// <summary>
222222
/// 关闭网络频道。
223223
/// </summary>
224-
void Close();
224+
/// <param name="reason">关闭原因</param>
225+
/// <param name="code">关闭错误码</param>
226+
void Close(string reason, ushort code = 0);
225227

226228
/// <summary>
227229
/// 向远程主机发送消息包。

Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected bool PActive
150150
protected readonly GameFrameworkLinkedList<MessageObject> m_ExecutionMessageLinkedList = new GameFrameworkLinkedList<MessageObject>();
151151

152152
public Action<NetworkChannelBase, object> NetworkChannelConnected;
153-
public Action<NetworkChannelBase> NetworkChannelClosed;
153+
public Action<NetworkChannelBase, string, ushort> NetworkChannelClosed;
154154
public Action<NetworkChannelBase, bool> NetworkChannelActiveChanged;
155155
public Action<NetworkChannelBase, int> NetworkChannelMissHeartBeat;
156156
public Action<NetworkChannelBase, NetworkErrorCode, SocketError, string> NetworkChannelError;
@@ -463,7 +463,7 @@ private void ProcessHeartBeat(float realElapseSeconds)
463463
if (PHeartBeatState.MissHeartBeatCount > MissHeartBeatCountByClose)
464464
{
465465
// 心跳丢失达到上线。触发断开
466-
Close();
466+
Close(NetworkCloseReason.MissHeartBeat, (ushort)NetworkErrorCode.MissHeartBeatError);
467467
}
468468
}
469469
}
@@ -474,7 +474,7 @@ private void ProcessHeartBeat(float realElapseSeconds)
474474
/// </summary>
475475
public virtual void Shutdown()
476476
{
477-
Close();
477+
Close(NetworkCloseReason.Normal);
478478
PSendState.Reset();
479479
PNetworkChannelHelper.Shutdown();
480480
}
@@ -630,7 +630,7 @@ public virtual void Connect(Uri address, object userData = null)
630630
{
631631
if (PSocket != null)
632632
{
633-
Close();
633+
Close(NetworkCloseReason.ConnectClose, (ushort)NetworkErrorCode.DisposeError);
634634
PSocket = null;
635635
}
636636

@@ -654,15 +654,15 @@ public virtual void Connect(Uri address, object userData = null)
654654
// 获取IP失败
655655
Log.Error($"IP address is invalid.{address.Host}");
656656
IsVerifyAddress = false;
657-
Close();
657+
Close(NetworkCloseReason.ConnectAddressError, (ushort)NetworkErrorCode.ConnectError);
658658
PSocket = null;
659659
}
660660
}
661661
catch (Exception e)
662662
{
663663
Log.Error($"IP address is invalid.{address.Host} {e.Message}");
664664
IsVerifyAddress = false;
665-
Close();
665+
Close(NetworkCloseReason.ConnectAddressExceptionError, (ushort)NetworkErrorCode.ConnectError);
666666
PSocket = null;
667667
}
668668
}
@@ -699,8 +699,10 @@ public virtual void Connect(Uri address, object userData = null)
699699
/// <summary>
700700
/// 关闭连接并释放所有相关资源。
701701
/// </summary>
702+
/// <param name="reason">关闭原因。</param>
703+
/// <param name="code">关闭错误码。</param>
702704
[UnityEngine.Scripting.Preserve]
703-
public virtual void Close()
705+
public virtual void Close(string reason, ushort code = 0)
704706
{
705707
lock (this)
706708
{
@@ -723,7 +725,7 @@ public virtual void Close()
723725
{
724726
PSocket.Close();
725727
PSocket = null;
726-
NetworkChannelClosed?.Invoke(this);
728+
NetworkChannelClosed?.Invoke(this, reason, code);
727729
}
728730

729731
PSentPacketCount = 0;
@@ -758,6 +760,11 @@ public async Task<TResult> Call<TResult>(MessageObject messageObject) where TRes
758760
return result as TResult;
759761
}
760762

763+
public void Close()
764+
{
765+
throw new NotImplementedException();
766+
}
767+
761768
/// <summary>
762769
/// 向远程主机发送消息包。
763770
/// </summary>
@@ -829,14 +836,13 @@ private void Dispose(bool disposing)
829836
return;
830837
}
831838

839+
m_Disposed = true;
832840
if (disposing)
833841
{
834-
Close();
842+
Close(NetworkCloseReason.Dispose, (ushort)NetworkErrorCode.DisposeError);
835843
PSendState.Dispose();
836844
PReceiveState.Dispose();
837845
}
838-
839-
m_Disposed = true;
840846
}
841847

842848
/// <summary>

Runtime/Network/Network/NetworkManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@ private void OnNetworkChannelConnected(NetworkChannelBase networkChannel, object
259259
}
260260
}
261261

262-
private void OnNetworkChannelClosed(NetworkChannelBase networkChannel)
262+
private void OnNetworkChannelClosed(NetworkChannelBase networkChannel, string reason, ushort errorCode)
263263
{
264264
if (m_NetworkClosedEventHandler != null)
265265
{
266266
lock (m_NetworkClosedEventHandler)
267267
{
268-
NetworkClosedEventArgs networkClosedEventArgs = NetworkClosedEventArgs.Create(networkChannel);
268+
NetworkClosedEventArgs networkClosedEventArgs = NetworkClosedEventArgs.Create(networkChannel, reason, errorCode);
269269
m_NetworkClosedEventHandler(this, networkClosedEventArgs);
270270
// ReferencePool.Release(networkClosedEventArgs);
271271
}

Runtime/Network/Network/SystemSocket/NetworkManager.SystemTcpNetworkChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void ReceiveCallback(IAsyncResult asyncResult)
156156

157157
if (bytesReceived <= 0)
158158
{
159-
Close();
159+
Close("Receive error.", (ushort)NetworkErrorCode.ReceiveError);
160160
return;
161161
}
162162

Runtime/Network/Network/WebSocket/NetworkManager.WebSocketNetSocket.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ private sealed class WebSocketNetSocket : INetworkSocket
1919
/// </summary>
2020
private bool _isConnecting = false;
2121

22-
TaskCompletionSource<bool> _connectTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
22+
private TaskCompletionSource<bool> _connectTask = new(TaskCreationOptions.RunContinuationsAsynchronously);
2323
private readonly Action<byte[]> _onReceiveAction;
24-
private readonly Action<string> _onCloseAction;
24+
private readonly Action<string, ushort> _onCloseAction;
2525

26-
public WebSocketNetSocket(string url, Action<byte[]> onReceiveAction, Action<string> onCloseAction)
26+
public WebSocketNetSocket(string url, Action<byte[]> onReceiveAction, Action<string, ushort> onCloseAction)
2727
{
28-
_client = new UnityWebSocket.WebSocket(url);
28+
_client = new WebSocket(url);
2929
_onReceiveAction = onReceiveAction;
3030
_onCloseAction = onCloseAction;
3131
_client.OnOpen += OnOpen;
@@ -44,7 +44,7 @@ private void OnMessage(object sender, MessageEventArgs e)
4444

4545
private void OnClose(object sender, CloseEventArgs e)
4646
{
47-
_onCloseAction?.Invoke(e.Reason + " " + e.Code);
47+
_onCloseAction?.Invoke(e.Reason, e.Code);
4848
}
4949

5050
private void OnError(object sender, ErrorEventArgs e)

Runtime/Network/Network/WebSocket/NetworkManager.WebSocketNetworkChannel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ public override void Connect(Uri address, object userData = null)
8989
ConnectAsync(userData);
9090
}
9191

92-
private void CloseCallback(string errorMessage)
92+
private void CloseCallback(string reason, ushort code)
9393
{
94-
Close();
94+
Close(reason, code);
9595
}
9696

97-
public override void Close()
97+
public override void Close(string reason, ushort code = 0)
9898
{
99-
base.Close();
99+
base.Close(reason, code);
100100
m_CancellationTokenSource.Cancel();
101101
}
102102

0 commit comments

Comments
 (0)