From 4330dd92a529a0c3154fb6202e2a6e19e0dbc72c Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Mon, 22 Jan 2024 13:04:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84ProcessAccept?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SourceMode/TcpSaeaServer_SourceMode.cs | 50 ++++++++++--------- .../NetWork/TcpSaeaServer.cs | 49 ++++++++++-------- 2 files changed, 54 insertions(+), 45 deletions(-) diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs index 95cfff2..a3247b5 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs @@ -321,30 +321,34 @@ namespace HaoYueNet.ServerNetwork try { Interlocked.Increment(ref m_clientCount); - // Get the socket for the accepted client connection and put it into the - //ReadEventArg object user token - SocketAsyncEventArgs readEventArgs = m_Receivepool.Pop(); - //TODO readEventArgs.UserToken这里的 UserToken 有可能是空 - AsyncUserToken userToken; - if (readEventArgs.UserToken == null) - readEventArgs.UserToken = new AsyncUserToken(); - - userToken = (AsyncUserToken)readEventArgs.UserToken; - userToken.Socket = e.AcceptSocket; - userToken.ConnectTime = DateTime.Now; - userToken.Remote = e.AcceptSocket.RemoteEndPoint; - userToken.IPAddress = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address; - - - userToken.RevIndex = MaxRevIndexNum; - userToken.SendIndex = MaxSendIndexNum; - - AddUserToken(userToken); - - OnClientNumberChange?.Invoke(1, userToken); - if (!e.AcceptSocket.ReceiveAsync(readEventArgs)) + //确保监听结束时,有连接才抛给数据接收 + if (e.AcceptSocket.RemoteEndPoint != null) { - ProcessReceive(readEventArgs); + // Get the socket for the accepted client connection and put it into the + //ReadEventArg object user token + SocketAsyncEventArgs readEventArgs = m_Receivepool.Pop(); + //TODO readEventArgs.UserToken这里的 UserToken 有可能是空 + AsyncUserToken userToken; + if (readEventArgs.UserToken == null) + readEventArgs.UserToken = new AsyncUserToken(); + + userToken = (AsyncUserToken)readEventArgs.UserToken; + userToken.Socket = e.AcceptSocket; + userToken.ConnectTime = DateTime.Now; + userToken.Remote = e.AcceptSocket.RemoteEndPoint; + userToken.IPAddress = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address; + + + userToken.RevIndex = MaxRevIndexNum; + userToken.SendIndex = MaxSendIndexNum; + + AddUserToken(userToken); + + OnClientNumberChange?.Invoke(1, userToken); + if (!e.AcceptSocket.ReceiveAsync(readEventArgs)) + { + ProcessReceive(readEventArgs); + } } } catch (Exception me) diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs index bd3b354..901cc91 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs @@ -321,30 +321,35 @@ namespace HaoYueNet.ServerNetwork try { Interlocked.Increment(ref m_clientCount); - // Get the socket for the accepted client connection and put it into the - //ReadEventArg object user token - SocketAsyncEventArgs readEventArgs = m_Receivepool.Pop(); - //TODO readEventArgs.UserToken这里的 UserToken 有可能是空 - AsyncUserToken userToken; - if (readEventArgs.UserToken == null) - readEventArgs.UserToken = new AsyncUserToken(); - userToken = (AsyncUserToken)readEventArgs.UserToken; - userToken.Socket = e.AcceptSocket; - userToken.ConnectTime = DateTime.Now; - userToken.Remote = e.AcceptSocket.RemoteEndPoint; - userToken.IPAddress = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address; - - - userToken.RevIndex = MaxRevIndexNum; - userToken.SendIndex = MaxSendIndexNum; - - AddUserToken(userToken); - - OnClientNumberChange?.Invoke(1, userToken); - if (!e.AcceptSocket.ReceiveAsync(readEventArgs)) + //确保监听结束时,有连接才抛给数据接收 + if (e.AcceptSocket.RemoteEndPoint != null) { - ProcessReceive(readEventArgs); + // Get the socket for the accepted client connection and put it into the + //ReadEventArg object user token + SocketAsyncEventArgs readEventArgs = m_Receivepool.Pop(); + //TODO readEventArgs.UserToken这里的 UserToken 有可能是空 + AsyncUserToken userToken; + if (readEventArgs.UserToken == null) + readEventArgs.UserToken = new AsyncUserToken(); + + userToken = (AsyncUserToken)readEventArgs.UserToken; + userToken.Socket = e.AcceptSocket; + userToken.ConnectTime = DateTime.Now; + userToken.Remote = e.AcceptSocket.RemoteEndPoint; + userToken.IPAddress = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address; + + + userToken.RevIndex = MaxRevIndexNum; + userToken.SendIndex = MaxSendIndexNum; + + AddUserToken(userToken); + + OnClientNumberChange?.Invoke(1, userToken); + if (!e.AcceptSocket.ReceiveAsync(readEventArgs)) + { + ProcessReceive(readEventArgs); + } } } catch (Exception me)