예제 #1
0
        public void ReturnToPool(NetBlock netBlock)
        {
            if (netBlock.data != null)
            {
                switch (netBlock.data.Length)
                {
                case c_blockSize17k:
                    if (pool17k.Count < 16)
                    {
                        pool17k.Push(netBlock);
                    }
                    break;

                case c_blockSize64k:
                    if (pool64k.Count < 16)
                    {
                        pool64k.Push(netBlock);
                    }
                    break;

                default:
                    break;
                }
            }
        }
예제 #2
0
        public void SendNetBlock(NetBlock netBlock)
        {
            SendCompressed(netBlock.header, netBlock.data, 0, netBlock.length);
#if USE_NET_BLOCK_POOL
            m_netcontext.ReturnToPool(netBlock);
#endif
        }
예제 #3
0
        public NetBlock GetNetBlock(int header, int size)
        {
            NetBlock block;

#if USE_NET_BLOCK_POOL
            if (size == 0)
            {
                block = new NetBlock();
            }
            else if (size <= c_blockSize17k)
            {
                if (!pool17k.TryPop(out block))
                {
                    block      = new NetBlock();
                    block.data = new byte[c_blockSize17k];
                }
            }
            else if (size <= c_blockSize64k)
            {
                if (!pool64k.TryPop(out block))
                {
                    block      = new NetBlock();
                    block.data = new byte[c_blockSize64k];
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("the net block size out of range.");
            }
#else
            block      = new NetBlock();
            block.data = new byte[size];
#endif
            block.header = header;
            block.length = size;
            return(block);
        }
예제 #4
0
 public void ReceiveService()
 {
     try
     {
         while (true)
         {
             int length = m_socket.Receive(m_receiveBuffer, m_receiveDataLength, 65536, SocketFlags.None);
             if (length == 0)
             {
                 break;
             }
             m_receiveDataLength += length;
             int peekIndex = 0;
             while (true)
             {
                 if (m_receiveDataLength < c_metaLength)
                 {
                     break;
                 }
                 int blockSize = BitConverter.ToInt32(m_receiveBuffer, peekIndex + 4);
                 if (m_receiveDataLength < blockSize + c_metaLength)
                 {
                     break;
                 }
                 if (blockSize > 0)
                 {
                     int decodedSize = LZ4.Decode(m_receiveBuffer, peekIndex + c_metaLength, m_receiveProcessBuffer, 0, blockSize, 65536);
                     if (decodedSize > 0)
                     {
                         NetBlock block = m_netcontext.GetNetBlock(BitConverter.ToInt32(m_receiveBuffer, peekIndex), decodedSize);
                         Array.Copy(m_receiveProcessBuffer, block.data, decodedSize);
                         lock (m_netcontext.lock_receivedBlock)
                         {
                             m_netcontext.l_receivedBlock_w.Add(block);
                         }
                     }
                 }
                 else
                 {
                     NetBlock block = m_netcontext.GetNetBlock(BitConverter.ToInt32(m_receiveBuffer, peekIndex), 0);
                     lock (m_netcontext.lock_receivedBlock)
                     {
                         m_netcontext.l_receivedBlock_w.Add(block);
                     }
                 }
                 peekIndex           += blockSize + c_metaLength;
                 m_receiveDataLength -= blockSize + c_metaLength;
                 m_netcontext.receiveEvent.Set();
             }
             Array.ConstrainedCopy(m_receiveBuffer, peekIndex, m_receiveBuffer, 0, m_receiveDataLength);
         }
     }
     catch (SocketException)
     {
     }
     catch (Exception e)
     {
         m_netcontext.Log(e);
     }
     finally
     {
         m_netcontext.receiveEvent.Set();
     }
 }