示例#1
0
 //当一个EVentArgs异步线程未结束时,不能重复调用EventArgs;
 private void _socketArg_Completed(object sender, SocketAsyncEventArgs e)
 {
     lock (locker)
     {
         if (e.LastOperation == SocketAsyncOperation.Receive)
         {
             //数据接收成功且不为0,则将收到数据写入缓存池,并触发接收完成事件
             if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
             {
                 _bufferPool.EnCache(e.Buffer, e.BytesTransferred);
                 //收到报文记录
                 byte[] logByte = new byte[e.BytesTransferred];
                 Array.Copy(e.Buffer, logByte, logByte.Length);
                 _log.ByteSteamLog(ActionType.RECEIVE, logByte);
                 readDataHanlder();
             }
             else
             {
                 DisConnect();
                 _log.ErrorLog("socket anyc read error:{0}", e.SocketError.ToString());
                 return;
             }
         }
         _subSocketArgFlag = false;
         invokSocketArg();
     }
 }
示例#2
0
        private void SocketArg_Completed(object sender, SocketAsyncEventArgs e)
        {
            lock (locker)
            {
                switch (e.LastOperation)
                {
                case SocketAsyncOperation.Receive:
                    //数据接收成功且不为0,则将收到数据写入缓存池,并触发接收完成事件
                    if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
                    {
                        bufferPool.EnCache(socketArg.Buffer, socketArg.BytesTransferred);

                        ///收到报文记录
                        byte[] logByte = new byte[socketArg.BytesTransferred];
                        Array.Copy(socketArg.Buffer, logByte, logByte.Length);
                        log.ByteSteamLog(ActionType.RECEIVE, logByte);

                        ReadComplete?.Invoke(this);
                    }
                    else
                    {
                        Clear();
                        if (_m_ConnectStatePool != null)
                        {
                            _m_ConnectStatePool.Return(this);
                        }
                    }
                    break;

                case SocketAsyncOperation.Send:
                    //数据发送成功且不为0,触发发送完成事件
                    if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
                    {
                        SendComplete?.Invoke(this);
                    }
                    else
                    {
                        Clear();
                        if (_m_ConnectStatePool != null)
                        {
                            _m_ConnectStatePool.Return(this);
                        }
                    }
                    break;

                default:
                    Clear();
                    if (_m_ConnectStatePool != null)
                    {
                        _m_ConnectStatePool.Return(this);
                    }
                    break;
                }
            }
        }