internal void Push(IntPtr handle, IOCPData data)
 {
     lock (pool)
     {
         pool.Add(handle, data);
     }
 }
Exemple #2
0
    /// <summary>
    /// 监听Socket接受处理
    /// </summary>
    /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param>
    //private void ProcessAccept(SocketAsyncEventArgs e)
    //{
    //    if (e.SocketError == SocketError.Success)
    //    {
    //        Socket s = e.AcceptSocket;//和客户端关联的socket
    //        if (s.Connected)
    //        {
    //            try
    //            {
    //                Interlocked.Increment(ref _clientCount);//原子操作加1
    //                SocketAsyncEventArgs asyniar = _objectPool.Pull();
    //                asyniar.UserToken = s;
    //                asyniar.AcceptSocket = s;

    //                Log4Debug(String.Format("客户 {0} 连入, 共有 {1} 个连接。", s.RemoteEndPoint.ToString(), _clientCount));

    //                if (!s.ReceiveAsync(asyniar))//投递接收请求
    //                {
    //                    ProcessReceive(asyniar);
    //                }
    //            }
    //            catch (SocketException ex)
    //            {
    //                Log4Debug(String.Format("接收客户 {0} 数据出错, 异常信息: {1} 。", s.RemoteEndPoint, ex.ToString()));
    //                //TODO 异常处理
    //            }
    //            //投递下一个接受请求
    //            StartAccept(e);
    //        }
    //    }
    //}
    private void ProcessAccept(SocketAsyncEventArgs e)
    {
        if (e.SocketError == SocketError.Success)
        {
            Socket s = e.AcceptSocket;//和客户端关联的socket
            if (s.Connected)
            {
                try
                {
                    Interlocked.Increment(ref _clientCount);//原子操作加1
                    SocketAsyncEventArgs args = _objectPool.Pull();
                    //AsyncUserToken userToken = _objectPool.Pull();
                    ////AsyncUserToken userToken = new AsyncUserToken(1024);
                    //userToken.connectSocket = s;
                    //userToken.AsyncReceive.AcceptSocket = s;
                    //userToken.AsyncSend.AcceptSocket = s;
                    //userToken.AsyncReceive.UserToken = s;
                    //userToken.AsyncSend.UserToken = s;
                    args.AcceptSocket = s;
                    args.UserToken    = s;


                    IOCPData data = new IOCPData();
                    data.HeartbeatTime = DateTime.Now;
                    IOCPDataManager.instance.Push(s.Handle, data);


                    Log4Debug(String.Format("客户 {0} 连入, 共有 {1} 个连接,handle {2}", s.RemoteEndPoint.ToString(), _clientCount, s.Handle));

                    if (!s.ReceiveAsync(args))//投递接收请求
                    {
                        ProcessReceive(args);
                    }
                }
                catch (SocketException ex)
                {
                    Log4Debug(String.Format("接收客户 {0} 数据出错, 异常信息: {1} 。", s.RemoteEndPoint, ex.ToString()));
                    //TODO 异常处理
                }
                //投递下一个接受请求
                StartAccept(e);
            }
        }
    }
    internal void OutLine(IntPtr handle)
    {
        IOCPData data = GetData(handle);

        outLine.Add("", data);
    }