Exemplo n.º 1
0
 public void ReceiveService()
 {
     try
     {
         while (true)
         {
             int length = 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 = LZ4Codec.Decode(new Span <byte>(m_receiveBuffer, peekIndex + c_metaLength, blockSize), new Span <byte>(m_receiveProcessBuffer, 0, 65536));
                     if (decodedSize > 0)
                     {
                         NetPack block = GetNetBlock(BitConverter.ToInt32(m_receiveBuffer, peekIndex), decodedSize);
                         Array.Copy(m_receiveProcessBuffer, block.data, decodedSize);
                         receivedBlocks.Add(block);
                     }
                 }
                 else
                 {
                     receivedBlocks.Add(GetNetBlock(BitConverter.ToInt32(m_receiveBuffer, peekIndex), 0));
                 }
                 peekIndex           += blockSize + c_metaLength;
                 m_receiveDataLength -= blockSize + c_metaLength;
                 receiveEvent.Set();
             }
             Array.ConstrainedCopy(m_receiveBuffer, peekIndex, m_receiveBuffer, 0, m_receiveDataLength);
         }
     }
     catch (SocketException)
     {
     }
     catch (Exception e)
     {
         Log(e);
     }
     finally
     {
         receiveEvent.Set();
     }
 }
Exemplo n.º 2
0
        public NetPack GetNetBlock(int header, int size)
        {
            NetPack block;

            block        = new NetPack();
            block.data   = new byte[size];
            block.header = header;
            block.length = size;
            return(block);
        }
Exemplo n.º 3
0
 public void Send(NetPack netPack)
 {
     SendCompressed(netPack.header, netPack.data, 0, netPack.length);
 }