Exemplo n.º 1
0
            protected override void ProtectedImport(Stream stream, BufferManager bufferManager)
            {
                using (DeadlockMonitor.Lock(this.ThisLock))
                {
                    Encoding encoding     = new UTF8Encoding(false);
                    byte[]   lengthBuffer = new byte[4];

                    for (; ;)
                    {
                        if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length)
                        {
                            return;
                        }
                        int  length = NetworkConverter.ToInt32(lengthBuffer);
                        byte id     = (byte)stream.ReadByte();

                        using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true))
                        {
                            if (id == (byte)SerializeId.Name)
                            {
                                using (StreamReader reader = new StreamReader(rangeStream, encoding))
                                {
                                    this.Name = reader.ReadToEnd();
                                }
                            }
                            else if (id == (byte)SerializeId.State)
                            {
                                using (StreamReader reader = new StreamReader(rangeStream, encoding))
                                {
                                    this.State = (ChannelState)Enum.Parse(typeof(ChannelState), reader.ReadToEnd());
                                }
                            }
                            else if (id == (byte)SerializeId.Manager)
                            {
                                byte[] buffer = new byte[rangeStream.Length];
                                rangeStream.Read(buffer, 0, buffer.Length);

                                this.Managers.Add(buffer);
                            }
                            else if (id == (byte)SerializeId.Member)
                            {
                                byte[] buffer = new byte[rangeStream.Length];
                                rangeStream.Read(buffer, 0, buffer.Length);

                                this.Members.Add(buffer);
                            }
                        }
                    }
                }
            }
Exemplo n.º 2
0
        protected override void ProtectedImport(Stream stream, BufferManager bufferManager)
        {
            Encoding encoding = new UTF8Encoding(false);

            byte[] lengthBuffer = new byte[4];

            for (; ;)
            {
                if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length)
                {
                    return;
                }
                int  length = NetworkConverter.ToInt32(lengthBuffer);
                byte id     = (byte)stream.ReadByte();

                using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true))
                {
                    if (id == (byte)SerializeId.Id)
                    {
                        byte[] buffer = new byte[rangeStream.Length];
                        rangeStream.Read(buffer, 0, buffer.Length);

                        this.Id = buffer;
                    }
                    else if (id == (byte)SerializeId.Name)
                    {
                        using (StreamReader reader = new StreamReader(rangeStream, encoding))
                        {
                            this.Name = reader.ReadToEnd();
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private static string ToBase64String(Stream stream)
        {
            using (var targetStream = new RangeStream(stream, true))
                using (var safeBuffer = _bufferManager.CreateSafeBuffer((int)targetStream.Length))
                {
                    targetStream.Seek(0, SeekOrigin.Begin);
                    targetStream.Read(safeBuffer.Value, 0, (int)targetStream.Length);

                    return(NetworkConverter.ToBase64UrlString(safeBuffer.Value, 0, (int)targetStream.Length));
                }
        }
Exemplo n.º 4
0
        public void read()
        {
            MemoryStream memStream = new MemoryStream(testData, false);

            memStream.Seek(testDataOffset, SeekOrigin.Current);
            RangeStream rangeStream = new RangeStream(memStream, testDataLength);

            byte[] part1    = new byte[3];
            int    part1Len = rangeStream.Read(part1, 0, 3);

            Assert.AreEqual(3, part1Len);
            Assert.AreEqual(0x01, part1[0]);
            Assert.AreEqual(0x02, part1[1]);
            Assert.AreEqual(0x03, part1[2]);

            byte[] part2    = new byte[4];
            int    part2Len = rangeStream.Read(part2, 1, 3);

            Assert.AreEqual(1, part2Len);
            Assert.AreEqual(0x04, part2[1]);
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        public void Test_RangeStream()
        {
            using (MemoryStream memoryStream = new MemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }))
            {
                byte[] mbyte = new byte[4];
                memoryStream.Seek(7, SeekOrigin.Begin);
                memoryStream.Read(mbyte, 0, mbyte.Length);
                memoryStream.Seek(5, SeekOrigin.Begin);

                using (RangeStream rangeStream = new RangeStream(memoryStream, 7, 4))
                {
                    Assert.AreEqual(memoryStream.Position, 7, "RangeStream #1");
                    Assert.AreEqual(rangeStream.Position, 0, "RangeStream #2");

                    byte[] rbyte = new byte[4];
                    rangeStream.Read(rbyte, 0, rbyte.Length);

                    Assert.AreEqual(rangeStream.Length, 4, "RangeStream #3");
                    Assert.IsTrue(CollectionUtilities.Equals(mbyte, rbyte), "RangeStream #4");
                }
            }
        }
Exemplo n.º 7
0
        private static string ToBase64String(Stream stream)
        {
            using (var targetStream = new RangeStream(stream, true))
            {
                byte[] buffer = null;

                try
                {
                    buffer = _bufferManager.TakeBuffer((int)targetStream.Length);
                    targetStream.Seek(0, SeekOrigin.Begin);
                    targetStream.Read(buffer, 0, (int)targetStream.Length);

                    return(NetworkConverter.ToBase64UrlString(buffer, 0, (int)targetStream.Length));
                }
                finally
                {
                    if (buffer != null)
                    {
                        _bufferManager.ReturnBuffer(buffer);
                    }
                }
            }
        }
Exemplo n.º 8
0
        protected override void ProtectedImport(Stream stream, BufferManager bufferManager)
        {
            using (DeadlockMonitor.Lock(this.ThisLock))
            {
                Encoding encoding     = new UTF8Encoding(false);
                byte[]   lengthBuffer = new byte[4];

                for (; ;)
                {
                    if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length)
                    {
                        return;
                    }
                    int  length = NetworkConverter.ToInt32(lengthBuffer);
                    byte id     = (byte)stream.ReadByte();

                    using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true))
                    {
                        if (id == (byte)SerializeId.Command)
                        {
                            using (StreamReader reader = new StreamReader(rangeStream, encoding))
                            {
                                this.Command = reader.ReadToEnd();
                            }
                        }
                        else if (id == (byte)SerializeId.Content)
                        {
                            byte[] buff = new byte[(int)rangeStream.Length];
                            rangeStream.Read(buff, 0, buff.Length);

                            this.Content = new ArraySegment <byte>(buff, 0, (int)rangeStream.Length);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
        private static Stream ToStream <T>(int version, MessageBase <T> item)
            where T : MessageBase <T>
        {
            Stream stream = null;

            try
            {
                stream = new RangeStream(item.Export(_bufferManager));

                var dic = new Dictionary <byte, Stream>();

                try
                {
                    stream.Seek(0, SeekOrigin.Begin);

                    RecyclableMemoryStream deflateBufferStream = null;

                    try
                    {
                        deflateBufferStream = new RecyclableMemoryStream(_bufferManager);

                        using (var deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true))
                            using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                            {
                                int length;

                                while ((length = stream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                {
                                    deflateStream.Write(safeBuffer.Value, 0, length);
                                }
                            }

                        deflateBufferStream.Seek(0, SeekOrigin.Begin);

                        dic.Add((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream);
                    }
                    catch (Exception)
                    {
                        if (deflateBufferStream != null)
                        {
                            deflateBufferStream.Dispose();
                        }
                    }
                }
                catch (Exception)
                {
                }

                dic.Add((byte)ConvertCompressionAlgorithm.None, stream);

                var list = dic.ToList();

                list.Sort((x, y) =>
                {
                    int c = x.Value.Length.CompareTo(y.Value.Length);
                    if (c != 0)
                    {
                        return(c);
                    }

                    return(x.Key.CompareTo(y.Key));
                });

                for (int i = 1; i < list.Count; i++)
                {
                    list[i].Value.Dispose();
                }

                var headerStream = new RecyclableMemoryStream(_bufferManager);
                Varint.SetUInt64(headerStream, (uint)version);
                Varint.SetUInt64(headerStream, list[0].Key);

                var dataStream = new UniteStream(headerStream, list[0].Value);

                var crcStream = new MemoryStream(Crc32_Castagnoli.Compute(new WrapperStream(dataStream)));
                return(new UniteStream(dataStream, crcStream));
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }

                throw new ArgumentException(ex.Message, ex);
            }
        }
Exemplo n.º 10
0
        private static T FromStream <T>(int version, Stream stream)
            where T : MessageBase <T>
        {
            try
            {
                stream.Seek(0, SeekOrigin.Begin);

                // Check
                {
                    var verifyCrc  = Crc32_Castagnoli.Compute(new RangeStream(stream, 0, stream.Length - 4, true));
                    var orignalCrc = new byte[4];

                    using (var 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");
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);

                if (version != (int)Varint.GetUInt64(stream))
                {
                    throw new ArgumentException("version");
                }
                int type = (int)Varint.GetUInt64(stream);

                using (var dataStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position - 4, true))
                {
                    if (type == (int)ConvertCompressionAlgorithm.None)
                    {
                        return(MessageBase <T> .Import(dataStream, _bufferManager));
                    }
                    else if (type == (int)ConvertCompressionAlgorithm.Deflate)
                    {
                        using (var deflateBufferStream = new RecyclableMemoryStream(_bufferManager))
                        {
                            using (var 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 > 1024 * 1024 * 32)
                                        {
                                            throw new Exception("too large");
                                        }
                                    }
                                }

                            deflateBufferStream.Seek(0, SeekOrigin.Begin);

                            return(MessageBase <T> .Import(deflateBufferStream, _bufferManager));
                        }
                    }
                    else
                    {
                        throw new ArgumentException("ArgumentException");
                    }
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Exemplo n.º 11
0
        public override void Send(System.IO.Stream stream, TimeSpan timeout, Information options)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (!_connect)
            {
                throw new ConnectionException();
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (stream.Length == 0)
            {
                throw new ArgumentOutOfRangeException("stream");
            }

            bool isCompress = true;

            if (options != null)
            {
                if (options.Contains("IsCompress"))
                {
                    isCompress = (bool)options["IsCompress"];
                }
            }

            lock (_sendLock)
            {
                using (RangeStream targetStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position, true))
                {
                    try
                    {
                        List <KeyValuePair <byte, Stream> > list = new List <KeyValuePair <byte, Stream> >();

                        if (isCompress)
                        {
                            if (_otherCompressAlgorithm.HasFlag(CompressAlgorithm.Deflate))
                            {
                                BufferStream deflateBufferStream = null;

                                try
                                {
                                    deflateBufferStream = new BufferStream(_bufferManager);

                                    using (DeflateStream deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true))
                                    {
                                        byte[] compressBuffer = null;

                                        try
                                        {
                                            compressBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                            int i = -1;

                                            while ((i = targetStream.Read(compressBuffer, 0, compressBuffer.Length)) > 0)
                                            {
                                                deflateStream.Write(compressBuffer, 0, i);
                                            }
                                        }
                                        finally
                                        {
                                            if (compressBuffer != null)
                                            {
                                                _bufferManager.ReturnBuffer(compressBuffer);
                                            }
                                        }
                                    }

                                    deflateBufferStream.Seek(0, SeekOrigin.Begin);

                                    list.Add(new KeyValuePair <byte, Stream>((byte)1, deflateBufferStream));
                                }
                                catch (Exception e)
                                {
                                    if (deflateBufferStream != null)
                                    {
                                        deflateBufferStream.Dispose();
                                    }

                                    throw e;
                                }
                            }
                        }

                        list.Add(new KeyValuePair <byte, Stream>((byte)0, new WrapperStream(targetStream, true)));

                        list.Sort((x, y) =>
                        {
                            int c = x.Value.Length.CompareTo(y.Value.Length);
                            if (c != 0)
                            {
                                return(c);
                            }

                            return(x.Key.CompareTo(y.Key));
                        });

#if DEBUG
                        if (list[0].Value.Length != targetStream.Length)
                        {
                            Debug.WriteLine("Send : {0}→{1} {2}",
                                            NetworkConverter.ToSizeString(targetStream.Length),
                                            NetworkConverter.ToSizeString(list[0].Value.Length),
                                            NetworkConverter.ToSizeString(list[0].Value.Length - targetStream.Length));
                        }
#endif

                        for (int i = 1; i < list.Count; i++)
                        {
                            list[i].Value.Dispose();
                        }

                        BufferStream headerStream = new BufferStream(_bufferManager);
                        headerStream.WriteByte((byte)list[0].Key);

                        using (var dataStream = new UniteStream(headerStream, list[0].Value))
                        {
                            _connection.Send(dataStream, timeout, options);
                        }
                    }
                    catch (ConnectionException e)
                    {
                        throw e;
                    }
                    catch (Exception e)
                    {
                        throw new ConnectionException(e.Message, e);
                    }
                }
            }
        }
Exemplo n.º 12
0
        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);
                }
            }
        }
Exemplo n.º 13
0
        private static Stream ToStream <T>(ItemBase <T> item)
            where T : ItemBase <T>
        {
            Stream stream = null;

            try
            {
                stream = new RangeStream(item.Export(_bufferManager));

                List <KeyValuePair <byte, Stream> > list = new List <KeyValuePair <byte, Stream> >();

                try
                {
                    stream.Seek(0, SeekOrigin.Begin);

                    BufferStream deflateBufferStream = null;

                    try
                    {
                        deflateBufferStream = new BufferStream(_bufferManager);

                        using (DeflateStream deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true))
                        {
                            byte[] compressBuffer = null;

                            try
                            {
                                compressBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                int i = -1;

                                while ((i = stream.Read(compressBuffer, 0, compressBuffer.Length)) > 0)
                                {
                                    deflateStream.Write(compressBuffer, 0, i);
                                }
                            }
                            finally
                            {
                                if (compressBuffer != null)
                                {
                                    _bufferManager.ReturnBuffer(compressBuffer);
                                }
                            }
                        }

                        deflateBufferStream.Seek(0, SeekOrigin.Begin);

                        list.Add(new KeyValuePair <byte, Stream>((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream));
                    }
                    catch (Exception)
                    {
                        if (deflateBufferStream != null)
                        {
                            deflateBufferStream.Dispose();
                        }
                    }
                }
                catch (Exception)
                {
                }

                list.Add(new KeyValuePair <byte, Stream>((byte)ConvertCompressionAlgorithm.None, stream));

                list.Sort((x, y) =>
                {
                    int c = x.Value.Length.CompareTo(y.Value.Length);
                    if (c != 0)
                    {
                        return(c);
                    }

                    return(x.Key.CompareTo(y.Key));
                });

#if DEBUG
                if (list[0].Value.Length != stream.Length)
                {
                    Debug.WriteLine("AmoebaConverter ToStream : {0}→{1} {2}",
                                    NetworkConverter.ToSizeString(stream.Length),
                                    NetworkConverter.ToSizeString(list[0].Value.Length),
                                    NetworkConverter.ToSizeString(list[0].Value.Length - stream.Length));
                }
#endif

                for (int i = 1; i < list.Count; i++)
                {
                    list[i].Value.Dispose();
                }

                BufferStream headerStream = new BufferStream(_bufferManager);
                headerStream.WriteByte((byte)list[0].Key);

                var dataStream = new UniteStream(headerStream, list[0].Value);

                MemoryStream crcStream = new MemoryStream(Crc32_Castagnoli.ComputeHash(dataStream));
                return(new UniteStream(dataStream, crcStream));
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }

                throw new ArgumentException(ex.Message, ex);
            }
        }
Exemplo n.º 14
0
        private static T FromStream <T>(Stream stream)
            where T : ItemBase <T>
        {
            try
            {
                using (var targetStream = new RangeStream(stream, true))
                {
                    using (Stream verifyStream = new RangeStream(targetStream, 0, targetStream.Length - 4, true))
                    {
                        byte[] verifyCrc  = Crc32_Castagnoli.ComputeHash(verifyStream);
                        byte[] orignalCrc = new byte[4];

                        using (RangeStream crcStream = new RangeStream(targetStream, targetStream.Length - 4, 4, true))
                        {
                            crcStream.Read(orignalCrc, 0, orignalCrc.Length);
                        }

                        if (!Unsafe.Equals(verifyCrc, orignalCrc))
                        {
                            throw new ArgumentException("Crc Error");
                        }
                    }

                    targetStream.Seek(0, SeekOrigin.Begin);
                    byte type = (byte)targetStream.ReadByte();

                    using (Stream dataStream = new RangeStream(targetStream, targetStream.Position, targetStream.Length - targetStream.Position - 4, true))
                    {
                        if (type == (byte)ConvertCompressionAlgorithm.None)
                        {
                            return(ItemBase <T> .Import(dataStream, _bufferManager));
                        }
                        else if (type == (byte)ConvertCompressionAlgorithm.Deflate)
                        {
                            using (BufferStream deflateBufferStream = new BufferStream(_bufferManager))
                            {
                                byte[] decompressBuffer = null;

                                try
                                {
                                    decompressBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                    using (DeflateStream deflateStream = new DeflateStream(dataStream, CompressionMode.Decompress, true))
                                    {
                                        int i = -1;

                                        while ((i = deflateStream.Read(decompressBuffer, 0, decompressBuffer.Length)) > 0)
                                        {
                                            deflateBufferStream.Write(decompressBuffer, 0, i);
                                        }
                                    }
                                }
                                finally
                                {
                                    if (decompressBuffer != null)
                                    {
                                        _bufferManager.ReturnBuffer(decompressBuffer);
                                    }
                                }

#if DEBUG
                                Debug.WriteLine("AmoebaConverter FromStream : {0}→{1} {2}",
                                                NetworkConverter.ToSizeString(dataStream.Length),
                                                NetworkConverter.ToSizeString(deflateBufferStream.Length),
                                                NetworkConverter.ToSizeString(dataStream.Length - deflateBufferStream.Length));
#endif

                                deflateBufferStream.Seek(0, SeekOrigin.Begin);

                                return(ItemBase <T> .Import(deflateBufferStream, _bufferManager));
                            }
                        }
                        else
                        {
                            throw new ArgumentException("ArgumentException");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
        }
Exemplo n.º 15
0
        public override void Send(Stream stream, TimeSpan timeout, Information options)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (stream.Length == 0)
            {
                throw new ArgumentOutOfRangeException("stream");
            }
            if (!_connect)
            {
                throw new ConnectionException();
            }

            lock (_sendLock)
            {
                try
                {
                    using (RangeStream targetStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position, true))
                    {
                        if (_version.HasFlag(SecureConnectionVersion.Version3))
                        {
                            using (BufferStream bufferStream = new BufferStream(_bufferManager))
                            {
                                bufferStream.SetLength(8);
                                bufferStream.Seek(8, SeekOrigin.Begin);

                                if (_informationVersion3.CryptoAlgorithm.HasFlag(SecureVersion3.CryptoAlgorithm.Aes256))
                                {
                                    byte[] iv = new byte[16];
                                    _random.GetBytes(iv);
                                    bufferStream.Write(iv, 0, iv.Length);

                                    using (var aes = Aes.Create())
                                    {
                                        aes.KeySize = 256;
                                        aes.Mode    = CipherMode.CBC;
                                        aes.Padding = PaddingMode.PKCS7;

                                        using (CryptoStream cs = new CryptoStream(new WrapperStream(bufferStream, true), aes.CreateEncryptor(_informationVersion3.MyCryptoKey, iv), CryptoStreamMode.Write))
                                        {
                                            byte[] sendBuffer = null;

                                            try
                                            {
                                                sendBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                                int i = -1;

                                                while ((i = targetStream.Read(sendBuffer, 0, sendBuffer.Length)) > 0)
                                                {
                                                    cs.Write(sendBuffer, 0, i);
                                                }
                                            }
                                            finally
                                            {
                                                if (sendBuffer != null)
                                                {
                                                    _bufferManager.ReturnBuffer(sendBuffer);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    throw new ConnectionException();
                                }

                                _totalSendSize += (bufferStream.Length - 8);

                                bufferStream.Seek(0, SeekOrigin.Begin);

                                byte[] totalSendSizeBuff = NetworkConverter.GetBytes(_totalSendSize);
                                bufferStream.Write(totalSendSizeBuff, 0, totalSendSizeBuff.Length);

                                if (_informationVersion3.HashAlgorithm.HasFlag(SecureVersion3.HashAlgorithm.Sha256))
                                {
                                    bufferStream.Seek(0, SeekOrigin.Begin);
                                    byte[] hmacBuff = HmacSha256.ComputeHash(bufferStream, _informationVersion3.MyHmacKey);

                                    bufferStream.Seek(0, SeekOrigin.End);
                                    bufferStream.Write(hmacBuff, 0, hmacBuff.Length);
                                }
                                else
                                {
                                    throw new ConnectionException();
                                }

                                bufferStream.Seek(0, SeekOrigin.Begin);

                                _connection.Send(bufferStream, timeout, options);
                            }
                        }
                        else
                        {
                            throw new ConnectionException();
                        }
                    }
                }
                catch (ConnectionException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw new ConnectionException(e.Message, e);
                }
            }
        }
Exemplo n.º 16
0
        private static Stream Compress(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var targetStream = new RangeStream(stream, true);

            List <KeyValuePair <byte, Stream> > list = new List <KeyValuePair <byte, Stream> >();

            try
            {
                targetStream.Seek(0, SeekOrigin.Begin);

                BufferStream deflateBufferStream = null;

                try
                {
                    deflateBufferStream = new BufferStream(_bufferManager);

                    using (DeflateStream deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true))
                    {
                        byte[] compressBuffer = null;

                        try
                        {
                            compressBuffer = _bufferManager.TakeBuffer(1024 * 4);

                            int i = -1;

                            while ((i = targetStream.Read(compressBuffer, 0, compressBuffer.Length)) > 0)
                            {
                                deflateStream.Write(compressBuffer, 0, i);
                            }
                        }
                        finally
                        {
                            if (compressBuffer != null)
                            {
                                _bufferManager.ReturnBuffer(compressBuffer);
                            }
                        }
                    }

                    deflateBufferStream.Seek(0, SeekOrigin.Begin);

                    list.Add(new KeyValuePair <byte, Stream>((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream));
                }
                catch (Exception)
                {
                    if (deflateBufferStream != null)
                    {
                        deflateBufferStream.Dispose();
                    }
                }
            }
            catch (Exception)
            {
            }

            list.Add(new KeyValuePair <byte, Stream>((byte)ConvertCompressionAlgorithm.None, targetStream));

            list.Sort((x, y) =>
            {
                int c = x.Value.Length.CompareTo(y.Value.Length);
                if (c != 0)
                {
                    return(c);
                }

                return(x.Key.CompareTo(y.Key));
            });

#if DEBUG
            if (list[0].Value.Length != targetStream.Length)
            {
                Debug.WriteLine("ContentConverter Compress {3} : {0}→{1} {2}",
                                NetworkConverter.ToSizeString(targetStream.Length),
                                NetworkConverter.ToSizeString(list[0].Value.Length),
                                NetworkConverter.ToSizeString(list[0].Value.Length - targetStream.Length),
                                (ConvertCompressionAlgorithm)list[0].Key);
            }
#endif

            for (int i = 1; i < list.Count; i++)
            {
                list[i].Value.Dispose();
            }

            BufferStream metadataStream = new BufferStream(_bufferManager);
            metadataStream.WriteByte((byte)list[0].Key);

            return(new UniteStream(metadataStream, list[0].Value));
        }