/// <summary>
        /// 关闭socket连接
        /// </summary>
        /// <param name="s"></param>
        /// <param name="e"></param>
        public void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncSocketUserToken userToken = e.UserToken as AsyncSocketUserToken;

            if (userToken.ConnectSocket == null)
            {
                return;
            }
            Interlocked.Decrement(ref _clientCount);
            Log4Debug(String.Format("客户端 {0} 断开连接, 当前 {1} 个连接。", userToken.RemoteEndPoint.ToString(), _clientCount));

            try
            {
                userToken.ConnectSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception)
            {
                // Throw if client has closed, so it is not necessary to catch.
            }
            finally
            {
                userToken.ConnectSocket.Close();
                userToken.ConnectSocket = null;
            }

            //Interlocked.Decrement(ref _clientCount);
            _maxAcceptClientSem.Release();
            _userTokenPool.Push(userToken);
            _clientList.Remove(userToken);
        }
Пример #2
0
        public void CloseClientSocket(AsyncSocketUserToken userToken)
        {
            if (userToken.ClientSocket == null)
            {
                return;
            }
            string socketInfo = string.Format("Local Address: {0} Remote Address: {1}", userToken.ClientSocket.LocalEndPoint,
                                              userToken.ClientSocket.RemoteEndPoint);

            Program.Logger.InfoFormat("Client connection disconnected. {0}", socketInfo);
            try
            {
                userToken.ClientSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception E)
            {
                Program.Logger.ErrorFormat("CloseClientSocket Disconnect client {0} error, message: {1}", socketInfo, E.Message);
            }
            userToken.ClientSocket.Close();
            userToken.ClientSocket = null; //释放引用,并清理缓存,包括释放协议对象等资源

            m_maxNumberAcceptedClients.Release();
            m_asyncSocketUserTokenPool.Push(userToken);
            m_asyncSocketUserTokenList.Remove(userToken);
        }
Пример #3
0
 public void GCUserToken(AsyncSocketUserToken userToken)
 {
     for (int i = 0; i < m_asyncSocketUserTokenList.Count; i++)
     {
         if (m_asyncSocketUserTokenList[i] != null && m_asyncSocketUserTokenList[i].TokenID == userToken.TokenID)
         {
             m_asyncSocketUserTokenList.Remove(userToken);
             m_asyncSocketUserTokenPool.Push(userToken);
             break;
         }
     }
 }