public override System.IO.Stream Receive(TimeSpan timeout, Information options) { if (_disposed) throw new ObjectDisposedException(this.GetType().FullName); if (!_connect) throw new ConnectionException(); lock (_receiveLock) { Stream stream = null; RangeStream dataStream = null; try { stream = _connection.Receive(timeout, options); dataStream = new RangeStream(stream, 0, stream.Length - 4); byte[] verifyCrc = Crc32_Castagnoli.ComputeHash(dataStream); byte[] orignalCrc = new byte[4]; using (RangeStream crcStream = new RangeStream(stream, stream.Length - 4, 4, true)) { crcStream.Read(orignalCrc, 0, orignalCrc.Length); } if (!Unsafe.Equals(verifyCrc, orignalCrc)) throw new ArgumentException("Crc Error"); dataStream.Seek(0, SeekOrigin.Begin); return dataStream; } catch (ConnectionException e) { if (stream != null) stream.Dispose(); if (dataStream != null) dataStream.Dispose(); throw e; } catch (Exception e) { if (stream != null) stream.Dispose(); if (dataStream != null) dataStream.Dispose(); throw new ConnectionException(e.Message, e); } } }
public override System.IO.Stream Receive(TimeSpan timeout, Information options) { if (_disposed) throw new ObjectDisposedException(this.GetType().FullName); if (!_connect) throw new ConnectionException(); lock (_receiveLock) { Stream stream = null; try { stream = _connection.Receive(timeout, options); var version = (byte)stream.ReadByte(); Stream dataStream = null; try { dataStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position); if (version == (byte)0) { return dataStream; } else if (version == (byte)1) { BufferStream deflateBufferStream = null; try { deflateBufferStream = new BufferStream(_bufferManager); using (DeflateStream deflateStream = new DeflateStream(dataStream, CompressionMode.Decompress, true)) using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4)) { int length; while ((length = deflateStream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0) { deflateBufferStream.Write(safeBuffer.Value, 0, length); if (deflateBufferStream.Length > _maxReceiveCount) throw new ConnectionException(); } } } catch (Exception e) { if (deflateBufferStream != null) { deflateBufferStream.Dispose(); } throw e; } #if DEBUG Debug.WriteLine("Receive : {0}→{1} {2}", NetworkConverter.ToSizeString(stream.Length), NetworkConverter.ToSizeString(deflateBufferStream.Length), NetworkConverter.ToSizeString(stream.Length - deflateBufferStream.Length)); #endif deflateBufferStream.Seek(0, SeekOrigin.Begin); dataStream.Dispose(); return deflateBufferStream; } else { throw new ArgumentException("ArgumentException"); } } catch (ConnectionException e) { if (dataStream != null) dataStream.Dispose(); throw e; } catch (Exception e) { if (dataStream != null) dataStream.Dispose(); throw new ConnectionException(e.Message, e); } } catch (ConnectionException e) { if (stream != null) stream.Dispose(); throw e; } catch (Exception e) { if (stream != null) stream.Dispose(); throw new ConnectionException(e.Message, e); } } }
private static Stream RemoveHash(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); byte type = (byte)stream.ReadByte(); if (type == (byte)ConvertHashAlgorithm.Sha256) { Stream dataStream = null; try { byte[] hash = new byte[32]; using (RangeStream hashStream = new RangeStream(stream, stream.Length - 32, 32, true)) { hashStream.Read(hash, 0, hash.Length); } dataStream = new RangeStream(stream, 1, stream.Length - (1 + 32)); if (!Unsafe.Equals(hash, Sha256.ComputeHash(dataStream))) throw new FormatException(); dataStream.Seek(0, SeekOrigin.Begin); return dataStream; } catch (Exception) { if (dataStream != null) { dataStream.Dispose(); } throw; } } throw new NotSupportedException(); }