예제 #1
0
        /// <summary>
        /// 关闭套件信息
        /// </summary>
        /// <param name="e"></param>
        protected void CloseClientSocket(SocketAsyncEventArgs e)
        {
            var client = e.UserToken as Socket;

            if (client == null)
            {
                return;
            }

            // close the socket associated with the client
            try
            {
                //保证资源清理
                SocketAsyncEventArgs readSocketEventArgs;
                ReadSocketEventArgsTable.TryRemove(client.RemoteEndPoint.ToString(), out readSocketEventArgs);
                e.UserToken = null;
                client.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception ex)
            {
                Logger.Error(ex, "CloseClientSocket");
            }
            client.Close();
            // decrement the counter keeping track of the total number of clients connected to the server
            Interlocked.Decrement(ref _mNumConnectedSockets);
            _mMaxNumberAcceptedClients.Release();
            Logger.Log(LogLevel.Trace, string.Format("关闭连接. 目前仍有{0}客户连接", _mNumConnectedSockets));
            // Free the SocketAsyncEventArg so they can be reused by another client
            _mReadPool.Push(e);
        }
예제 #2
0
        /// <summary>
        /// 根据键值关闭套件信息
        /// </summary>
        /// <param name="key"></param>
        public void CloseClientSocket(string key)
        {
            SocketAsyncEventArgs readSocketEventArgs;
            var flag = ReadSocketEventArgsTable.TryRemove(key, out readSocketEventArgs);

            if (flag && readSocketEventArgs != null)
            {
                CloseClientSocket(readSocketEventArgs);
            }
        }