Esempio n. 1
0
 public AsyncSendBufferManager(int bufferSize)
 {
     m_dynamicBufferManager    = new DynamicBufferManager(bufferSize);
     m_sendBufferList          = new List <SendBufferPacket>();
     m_sendBufferPacket.Offset = 0;
     m_sendBufferPacket.Count  = 0;
 }
Esempio n. 2
0
 public AsyncSocketUserToken(int asyncReceiveBufferSize)
 {
     m_connectSocket              = null;
     m_asyncSocketInvokeElement   = null;
     m_receiveEventArgs           = new SocketAsyncEventArgs();
     m_receiveEventArgs.UserToken = this;
     m_asyncReceiveBuffer         = new byte[asyncReceiveBufferSize];
     m_receiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length);
     m_sendEventArgs           = new SocketAsyncEventArgs();
     m_sendEventArgs.UserToken = this;
     m_receiveBuffer           = new DynamicBufferManager(ProtocolConst.InitBufferSize);
     m_sendBuffer = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);
     m_machineId  = "";
 }
Esempio n. 3
0
        public virtual bool ProcessReceive(byte[] buffer, int offset, int count) //接收异步事件返回的数据,用于对数据进行缓存和分包
        {
            m_activeDT = DateTime.UtcNow;
            DynamicBufferManager receiveBuffer = m_asyncSocketUserToken.ReceiveBuffer;

            receiveBuffer.WriteBuffer(buffer, offset, count);
            if (receiveBuffer.DataCount > 0)
            {
                //按照长度分包

                /*
                 * int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度
                 * if (NetByteOrder)
                 *  packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序
                 *
                 *
                 * if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) //最大Buffer异常保护
                 *  return false;
                 *
                 * if ((receiveBuffer.DataCount - sizeof(int)) >= packetLength) //收到的数据达到包长度
                 * {
                 *  bool result = ProcessPacket(receiveBuffer.Buffer, sizeof(int), packetLength);
                 *  if (result)
                 *      receiveBuffer.Clear(packetLength + sizeof(int)); //从缓存中清理
                 *  return result;
                 * }
                 * else
                 * {
                 *  return true;
                 * }
                 * */
                byte[]       handleBuffer = buffer.Take(count).ToArray();
                MachineLogic machineLogic = new MachineLogic();
                byte[]       result       = machineLogic.HandleHexByte(handleBuffer, m_asyncSocketUserToken, m_asyncSocketServer);
                if (result.Length > 0)
                {
                    //Program.Logger.InfoFormat("return message is {0}, machine  is {1}", ByteHelper.byteToHexStr(result), m_asyncSocketUserToken.MachineId);
                    DoSendBuffer(result, 0, result.Length);
                    receiveBuffer.Clear(count);
                }

                return(true);
            }
            else
            {
                return(true);
            }
        }