public TcpRawMessage Compress(CompressionLevel compressionLevel) { CheckDisposed(); TcpRawMessage compressedMessage = new TcpRawMessage(this.memoryStreamPool, (int)this.stream.Length); compressedMessage.Flags = this.Flags; this.stream.Position = 0; byte[] buffer = null; try { buffer = ArrayPool <byte> .Shared.Rent(BUFFER_SIZE); using (GZipOutputStream gzip = new GZipOutputStream(compressedMessage.stream, BUFFER_SIZE)) { gzip.SetLevel(6); gzip.IsStreamOwner = false; int readBytes; while ((readBytes = this.stream.Read(buffer, 0, buffer.Length)) > 0) { gzip.Write(buffer, 0, readBytes); } } } finally { if (buffer != null) { ArrayPool <byte> .Shared.Return(buffer); } } compressedMessage.Position = 0; return(compressedMessage); }
public TcpRawMessage Decrypt(ICipher cipher) { CheckDisposed(); using (var encryptor = cipher.CreateDecryptor()) { using (CryptoStream cryptoStream = new CryptoStream(new NonDisposableStream(this.stream), encryptor, CryptoStreamMode.Read)) { TcpRawMessage decryptedMessage = new TcpRawMessage(this.memoryStreamPool, (int)this.stream.Length); decryptedMessage.Flags = this.Flags; this.stream.Position = 0; cryptoStream.CopyTo(decryptedMessage.BaseStream); decryptedMessage.Position = 0; return(decryptedMessage); } } }
public TcpRawMessage Encrypt(ICipher cipher) { CheckDisposed(); using (var encryptor = cipher.CreateEncryptor()) { TcpRawMessage encryptedMessage = new TcpRawMessage(this.memoryStreamPool, (int)this.stream.Length); encryptedMessage.Flags = this.Flags; this.stream.Position = 0; using (CryptoStream cryptoStream = new CryptoStream(new NonDisposableStream(encryptedMessage.BaseStream), encryptor, CryptoStreamMode.Write)) { this.stream.CopyTo(cryptoStream); cryptoStream.FlushFinalBlock(); encryptedMessage.Position = 0; return(encryptedMessage); } } }
public TcpRawMessage Decompress() { CheckDisposed(); if (!HasCompressionMark()) { throw new IOException("Message isn't compressed or compression mark not found"); } TcpRawMessage decompressedMessage = new TcpRawMessage(this.memoryStreamPool, (int)this.stream.Length); decompressedMessage.Flags = this.Flags; this.stream.Position = 0; byte[] buffer = null; try { buffer = ArrayPool <byte> .Shared.Rent(BUFFER_SIZE); using (GZipInputStream gzip = new GZipInputStream(this.stream)) { gzip.IsStreamOwner = false; int readBytes; while ((readBytes = gzip.Read(buffer, 0, buffer.Length)) > 0) { decompressedMessage.stream.Write(buffer, 0, readBytes); } } } finally { if (buffer != null) { ArrayPool <byte> .Shared.Return(buffer); } } decompressedMessage.Position = 0; return(decompressedMessage); }
public TcpRawMessageHeader(TcpRawMessage message) : this() { this.MessageSize = message.Length; this.options = new TcpRawMessageOptions(message.Flags); }