示例#1
0
        protected override Stream DoGetFileStream(string path)
        {
            var file = FindFileInfo(path);

            if (file == null)
            {
                throw new InvalidOperationException("File not found in archive: " + path);
            }

            Stream stream = new FileStream(ArchivePath, FileMode.Open, FileAccess.Read);

            stream.Position = file.DataOffset;
            long length = file.Size;

            if (flags.HasFlag(ArchiveFlags.FileNameBeforeData))
            {
                // Consume (skip) filename before data
                var singleByteBuffer = new byte[1];
                stream.Read(singleByteBuffer, 0, 1);
                var stringLength = singleByteBuffer[0];
                stream.Seek(stringLength, SeekOrigin.Current);
                // Adjust length according to consumed data
                length -= stringLength + 1;
            }

            if (file.IsCompressed)
            {
                // Read original size
                var originalSizeBuffer = new byte[4];
                stream.Read(originalSizeBuffer, 0, 4);
                uint originalSize       = BitConverter.ToUInt32(originalSizeBuffer, 0);
                var  compressedByteData = new byte[length];
                stream.Read(compressedByteData, 0, (int)length);
                var uncompressedByteData = new byte[originalSize];
                LZ4.Decode(compressedByteData, 0, (int)length, uncompressedByteData, 0, (int)originalSize, true);
                return(new MemoryStream(uncompressedByteData));
                // Deflate stream
                //return new CustomDeflateStream(stream, originalSize);
            }
            else
            {
                return(new ArchiveSubstream(stream, length));
            }
        }
示例#2
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();
     }
 }