Skip to content

Commit cf1d665

Browse files
committed
[增加]1. 增加网络是否关闭的属性
1 parent 5fda5ce commit cf1d665

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Runtime/Network/Interface/INetworkSocket.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public interface INetworkSocket
1313
/// </summary>
1414
bool IsConnected { get; }
1515

16+
/// <summary>
17+
/// 获取是否已关闭。
18+
/// </summary>
19+
bool IsClosed { get; }
20+
1621
/// <summary>
1722
/// 获取本地终结点。
1823
/// </summary>

Runtime/Network/Network/SystemSocket/NetworkManager.SystemNetSocket.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public bool IsConnected
2121
get { return m_Socket.Connected; }
2222
}
2323

24+
public bool IsClosed { get; private set; }
25+
2426
public Socket Socket
2527
{
2628
get { return m_Socket; }
@@ -71,12 +73,24 @@ public int SendBufferSize
7173

7274
public void Shutdown()
7375
{
76+
if (IsClosed)
77+
{
78+
return;
79+
}
80+
7481
m_Socket.Shutdown(SocketShutdown.Both);
7582
}
7683

7784
public void Close()
7885
{
86+
if (IsClosed)
87+
{
88+
return;
89+
}
90+
7991
m_Socket.Close();
92+
m_Socket.Dispose();
93+
IsClosed = true;
8094
}
8195

8296

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ private sealed class WebSocketNetSocket : INetworkSocket
2020
private bool _isConnecting = false;
2121

2222
TaskCompletionSource<bool> _connectTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
23-
private Action<byte[]> _action;
24-
private Action<string> _onCloseAction;
23+
private readonly Action<byte[]> _action;
24+
private readonly Action<string> _onCloseAction;
2525

2626
public WebSocketNetSocket(string url, Action<byte[]> action, Action<string> onCloseAction)
2727
{
@@ -88,6 +88,8 @@ public bool IsConnected
8888
get { return _client.IsConnected; }
8989
}
9090

91+
public bool IsClosed { get; private set; }
92+
9193
public EndPoint LocalEndPoint
9294
{
9395
get { return null; }
@@ -103,12 +105,23 @@ public EndPoint RemoteEndPoint
103105

104106
public void Shutdown()
105107
{
108+
if (IsClosed)
109+
{
110+
return;
111+
}
112+
106113
_client.CloseAsync();
107114
}
108115

109116
public void Close()
110117
{
118+
if (IsClosed)
119+
{
120+
return;
121+
}
122+
111123
_client.CloseAsync();
124+
IsClosed = true;
112125
}
113126
}
114127
}

0 commit comments

Comments
 (0)