예제 #1
0
 /// <summary>
 /// 初始化连接池
 /// </summary>
 public void Init()
 {
     for (int i = 0; i < m_numConnections; i++) //按照连接数建立读写对象
     {
         AsyncSocketUserToken userToken = new AsyncSocketUserToken(m_receiveBufferSize);
         userToken.ReceiveEventArgs.Completed += IO_Completed;
         userToken.SendEventArgs.Completed    += IO_Completed;
         m_asyncSocketUserTokenPool.Push(userToken);
     }
 }
예제 #2
0
        public void Init()
        {
            AsyncSocketUserToken userToken = null;

            for (int i = 0; i < m_numConnections; i++)
            {
                userToken = new AsyncSocketUserToken(m_receiveBufferSize);
                userToken.ReceiveEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                userToken.SendEventArgs.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                m_AsyncSocketUserTokenPool.Push(userToken);
            }
        }
        /// <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);
        }
        /// <summary>
        /// 初始化函数
        /// </summary>
        public void Init()
        {
            _totalBytesRead = 0;

            _clientCount = 0;

            _bufferManager = new BufferManager(_bufferSize * _maxClient * (opsToPreAlloc), _bufferSize);

            _userTokenPool = new AsyncSocketUserTokenPool(_maxClient);

            _clientList = new AsyncSocketUserTokenList();

            _maxAcceptClientSem = new Semaphore(_maxClient, _maxClient);

            _stopWaitSem = new Semaphore(1, 1);

            _logOutEvtArgsPool = new Stack <LogOutEventArgs>(_maxClient);

            // preallocate pool of AsyncSocketUserToken and LogOutEventArgs
            AsyncSocketUserToken userToken;
            LogOutEventArgs      logArgs;

            for (int i = 0; i < _maxClient; i++)
            {
                //Pre-allocate a set of reusable AsyncSocketUserToken
                userToken = new AsyncSocketUserToken();
                userToken.SendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted);
                userToken.RecvEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted);

                // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object
                _bufferManager.SetBuffer(userToken.RecvEventArgs);
                _bufferManager.SetBuffer(userToken.SendEventArgs);

                _userTokenPool.Push(userToken);

                // Pre-allocate a set of reusable LogOutEventArgs
                logArgs = new LogOutEventArgs("", 0, 0);
                _logOutEvtArgsPool.Push(logArgs);
            }
        }