Пример #1
0
        public bool Remove(byte[] id)
        {
            lock (_lockObject)
            {
                if (id == null)
                {
                    throw new ArgumentNullException(nameof(id));
                }

                int i = RouteTable <T> .Distance(this.BaseId, id) - 1;

                if (i == -1)
                {
                    return(false);
                }

                var targetList = _nodesList[i];

                if (targetList != null)
                {
                    return(targetList.RemoveAll((item) => Unsafe.Equals(item.Id, id)) != 0);
                }

                return(false);
            }
        }
Пример #2
0
        public override bool Equals(Cash other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.CashAlgorithm != other.CashAlgorithm ||
                ((this.Key == null) != (other.Key == null)))
            {
                return(false);
            }

            if (this.Key != null && other.Key != null)
            {
                if (!Unsafe.Equals(this.Key, other.Key))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        public override bool Equals(ExchangePublicKey other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.CreationTime != other.CreationTime ||
                this.ExchangeAlgorithm != other.ExchangeAlgorithm ||
                ((this.PublicKey == null) != (other.PublicKey == null)))
            {
                return(false);
            }

            if (this.PublicKey != null && other.PublicKey != null)
            {
                if (!Unsafe.Equals(this.PublicKey, other.PublicKey))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #4
0
        public override bool Equals(Key other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if ((this.Hash == null) != (other.Hash == null)

                || this.HashAlgorithm != other.HashAlgorithm)
            {
                return(false);
            }

            if (this.Hash != null && other.Hash != null)
            {
                if (!Unsafe.Equals(this.Hash, other.Hash))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #5
0
        public override bool Equals(Certificate other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (!object.ReferenceEquals(this.Nickname, other.Nickname) ||
                this.DigitalSignatureAlgorithm != other.DigitalSignatureAlgorithm ||
                !object.ReferenceEquals(this.PublicKey, other.PublicKey) ||
                ((this.Signature == null) != (other.Signature == null)))
            {
                return(false);
            }

            if (this.Signature != null && other.Signature != null)
            {
                if (!Unsafe.Equals(this.Signature, other.Signature))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #6
0
            private static Stream RemoveHash(Stream stream)
            {
                if (stream == null)
                {
                    throw new ArgumentNullException(nameof(stream));
                }

                int type = (int)Varint.GetUInt64(stream);

                if (type == (int)ConvertHashAlgorithm.Sha256)
                {
                    var value = new byte[32];
                    stream.Read(value, 0, value.Length);

                    var dataStream = new RangeStream(stream, true);
                    if (!Unsafe.Equals(value, Sha256.Compute(new WrapperStream(dataStream))))
                    {
                        throw new ArgumentException("Hash");
                    }

                    dataStream.Seek(0, SeekOrigin.Begin);
                    return(dataStream);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
Пример #7
0
        public override bool Equals(Tag other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.Name != other.Name ||
                (this.Id == null) != (other.Id == null))
            {
                return(false);
            }

            if (this.Id != null && other.Id != null)
            {
                if (!Unsafe.Equals(this.Id, other.Id))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #8
0
        public Node <T> Get(byte[] id)
        {
            lock (_lockObject)
            {
                if (id == null)
                {
                    throw new ArgumentNullException(nameof(id));
                }

                int i = RouteTable <T> .Distance(this.BaseId, id) - 1;

                if (i == -1)
                {
                    return(default(Node <T>));
                }

                var targetList = _nodesList[i];

                if (targetList != null)
                {
                    return(targetList.FirstOrDefault((item) => Unsafe.Equals(item.Id, id)));
                }

                return(default(Node <T>));
            }
        }
Пример #9
0
        public override bool Equals(ProtocolInformation other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.KeyExchangeAlgorithm != other.KeyExchangeAlgorithm ||
                this.KeyDerivationAlgorithm != other.KeyDerivationAlgorithm ||
                this.CryptoAlgorithm != other.CryptoAlgorithm ||
                this.HashAlgorithm != other.HashAlgorithm ||
                (this.SessionId == null) != (other.SessionId == null))
            {
                return(false);
            }

            if (this.SessionId != null && other.SessionId != null)
            {
                if (!Unsafe.Equals(this.SessionId, other.SessionId))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #10
0
        public override bool Equals(Hash other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.Algorithm != other.Algorithm ||
                (this.Value == null) != (other.Value == null))
            {
                return(false);
            }

            if (this.Value != null && other.Value != null)
            {
                if (!Unsafe.Equals(this.Value, other.Value))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #11
0
        public override bool Equals(Node other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if ((this.Id == null) != (other.Id == null) ||
                (this.Uris == null) != (other.Uris == null))
            {
                return(false);
            }

            if (this.Id != null && other.Id != null)
            {
                if (!Unsafe.Equals(this.Id, other.Id))
                {
                    return(false);
                }
            }

            if (this.Uris != null && other.Uris != null)
            {
                if (!CollectionUtilities.Equals(this.Uris, other.Uris))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #12
0
        public override bool Equals(Index other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (!CollectionUtilities.Equals(this.Groups, other.Groups)

                || this.CompressionAlgorithm != other.CompressionAlgorithm

                || this.CryptoAlgorithm != other.CryptoAlgorithm ||
                (this.CryptoKey == null) != (other.CryptoKey == null))
            {
                return(false);
            }

            if (this.CryptoKey != null && other.CryptoKey != null)
            {
                if (!Unsafe.Equals(this.CryptoKey, other.CryptoKey))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #13
0
        public void Xor()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024 * 4; i++)
            {
                byte[] x;
                byte[] y;

                if (random.Next(0, 2) == 0)
                {
                    var length = random.Next(0, 1024 * 256);
                    x = new byte[length];
                    y = new byte[length];
                }
                else
                {
                    x = new byte[random.Next(0, 1024 * 256)];
                    y = new byte[random.Next(0, 1024 * 256)];
                }

                random.NextBytes(x);
                random.NextBytes(y);

                byte[] result1 = new byte[Math.Min(x.Length, y.Length)];
                byte[] result2 = new byte[Math.Min(x.Length, y.Length)];

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        Unsafe.Xor(x, y, result1);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        Benchmark.Xor(x, y, result2);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Xor: " + sw1.Elapsed.ToString());
            sb.AppendLine("Unsafe Xor: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
Пример #14
0
        // Addより優先的に
        public void Live(T item)
        {
            if (_baseNode == null)
            {
                throw new ArgumentNullException("BaseNode");
            }
            if (_baseNode.Id == null)
            {
                throw new ArgumentNullException("BaseNode.Id");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (item.Id == null)
            {
                throw new ArgumentNullException("item.Id");
            }

            lock (this.ThisLock)
            {
                int i = Kademlia <T> .Distance(this.BaseNode.Id, item.Id) - 1;

                if (i == -1)
                {
                    return;
                }

                var targetList = _nodesList[i];

                // 生存率の高いNodeはFirstに、そうでないNodeはLastに
                if (targetList != null)
                {
                    var orignal = targetList.FirstOrDefault(n => Unsafe.Equals(n.Id, item.Id));

                    if (orignal != null)
                    {
                        targetList.Remove(orignal);
                        targetList.AddFirst(item);
                    }
                    else
                    {
                        if (targetList.Count == _column)
                        {
                            targetList.RemoveLast();
                        }

                        targetList.AddFirst(item);
                    }
                }
                else
                {
                    targetList = new LinkedList <T>();
                    targetList.AddFirst(item);
                    _nodesList[i] = targetList;
                }
            }
        }
Пример #15
0
        public bool Equals(byte[] x, byte[] y)
        {
            if ((x == null) != (y == null))
            {
                return(false);
            }
            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }

            return(Unsafe.Equals(x, y));
        }
Пример #16
0
        public void FromHexString()
        {
            Random random = new Random();

            string value;

            {
                byte[] buffer = new byte[32];
                random.NextBytes(buffer);

                value = NetworkConverter.ToHexString(buffer);
            }

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024 * 1024 * 2; i++)
            {
                byte[] result1 = null;
                byte[] result2 = null;

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = NetworkConverter.FromHexString(value);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = NetworkConverter.FromHexString_2(value);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("FromHexString: " + sw1.Elapsed.ToString());
            sb.AppendLine("FromHexString_2: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
Пример #17
0
        public void Copy()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            var length = 1024 * 256;

            byte[] x1 = new byte[length];
            byte[] y1 = new byte[length];
            byte[] x2 = new byte[length];
            byte[] y2 = new byte[length];

            random.NextBytes(x1);
            random.NextBytes(x2);

            for (int i = 0; i < 1024 * 256; i++)
            {
                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        Unsafe.Copy(x1, 0, y1, 0, length);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        Array.Copy(x2, 0, y2, 0, length);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(x1, y1));
                Assert.IsTrue(Unsafe.Equals(x2, y2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Copy: " + sw1.Elapsed.ToString());
            sb.AppendLine("Array Copy: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
Пример #18
0
        public void Crc32_Castagnoli()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            var length = 1024;

            byte[] value = new byte[length];

            for (int i = 0; i < 1024 * 1024; i++)
            {
                byte[] result1 = null;
                byte[] result2 = null;

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = Library.Security.Crc32_Castagnoli.ComputeHash(value, 0, length);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = T_Crc32_Castagnoli.ComputeHash(value, 0, length);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Crc32_Castagnoli: " + sw1.Elapsed.ToString());
            sb.AppendLine("Managed Crc32_Castagnoli: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
Пример #19
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();
        }
Пример #20
0
        public bool Add(byte[] id, T value)
        {
            lock (_lockObject)
            {
                if (id == null)
                {
                    throw new ArgumentNullException(nameof(id));
                }

                int i = RouteTable <T> .Distance(this.BaseId, id) - 1;

                if (i == -1)
                {
                    return(false);
                }

                var targetList = _nodesList[i];

                // 生存率の高いNodeはFirstに、そうでないNodeはLastに
                if (targetList != null)
                {
                    if (!targetList.Any(n => Unsafe.Equals(n.Id, id)))
                    {
                        if (targetList.Count < _col)
                        {
                            targetList.AddFirst(new Node <T>(id, value));

                            return(true);
                        }
                    }
                }
                else
                {
                    targetList = new LinkedList <Node <T> >();
                    targetList.AddFirst(new Node <T>(id, value));
                    _nodesList[i] = targetList;

                    return(true);
                }

                return(false);
            }
        }
Пример #21
0
        public void Test_HmacSha256()
        {
            // http://tools.ietf.org/html/rfc4868#section-2.7.1
            {
                var key   = NetworkConverter.FromHexString("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
                var value = NetworkConverter.FromHexString("4869205468657265");

                using (MemoryStream stream = new MemoryStream(value))
                {
                    var s = NetworkConverter.ToHexString(HmacSha256.ComputeHash(stream, key));
                }

                using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
                {
                    var s = NetworkConverter.ToHexString(hmacSha256.ComputeHash(value));
                }
            }

            var list = new List <int>();

            list.Add(1);
            list.Add(64);
            list.Add(128);

            byte[] buffer = new byte[1024 * 32];
            _random.NextBytes(buffer);

            for (int i = 0; i < list.Count; i++)
            {
                byte[] key = new byte[list[i]];
                _random.NextBytes(key);

                using (MemoryStream stream1 = new MemoryStream(buffer))
                    using (MemoryStream stream2 = new MemoryStream(buffer))
                    {
                        using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
                        {
                            Assert.IsTrue(Unsafe.Equals(hmacSha256.ComputeHash(stream1), HmacSha256.ComputeHash(stream2, key)));
                        }
                    }
            }
        }
Пример #22
0
        public override bool Equals(Seed other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.Name != other.Name ||
                this.Length != other.Length ||
                this.CreationTime != other.CreationTime ||
                this.Comment != other.Comment ||
                this.Rank != other.Rank ||
                this.Key != other.Key

                || !CollectionUtilities.Equals(this.Keywords, other.Keywords)

                || this.CompressionAlgorithm != other.CompressionAlgorithm

                || this.CryptoAlgorithm != other.CryptoAlgorithm ||
                (this.CryptoKey == null) != (other.CryptoKey == null)

                || this.Certificate != other.Certificate)
            {
                return(false);
            }

            if (this.CryptoKey != null && other.CryptoKey != null)
            {
                if (!Unsafe.Equals(this.CryptoKey, other.CryptoKey))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #23
0
        public void ReadWriteTest()
        {
            for (int i = 0; i < 256; i++)
            {
                int size = _random.Next(1, 1024 * 1024 * 4);

                using (var safeBuffer = _bufferManager.CreateSafeBuffer(size))
                {
                    _random.NextBytes(safeBuffer.Value);

                    var block = new ArraySegment <byte>(safeBuffer.Value, 0, size);
                    var hash  = new Hash(HashAlgorithm.Sha256, Sha256.Compute(block));

                    _cacheManager.Set(hash, block);
                    var result = _cacheManager.GetBlock(hash);

                    Assert.True(Unsafe.Equals(block.Array, block.Offset, result.Array, result.Offset, size));

                    _bufferManager.ReturnBuffer(result.Array);
                }
            }
        }
Пример #24
0
        public override bool Equals(ConnectionSignature other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.CreationTime != other.CreationTime ||
                (this.ExchangeKey == null) != (other.ExchangeKey == null) ||
                (this.ProtocolHash == null) != (other.ProtocolHash == null)

                || this.Certificate != other.Certificate)
            {
                return(false);
            }

            if (this.ExchangeKey != null && other.ExchangeKey != null)
            {
                if (!Unsafe.Equals(this.ExchangeKey, other.ExchangeKey))
                {
                    return(false);
                }
            }

            if (this.ProtocolHash != null && other.ProtocolHash != null)
            {
                if (!Unsafe.Equals(this.ProtocolHash, other.ProtocolHash))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #25
0
            public bool Equals(Node <T> other)
            {
                if ((object)other == null)
                {
                    return(false);
                }

                if ((this.Id == null) != (other.Id == null) ||
                    !this.Value.Equals(other.Value))
                {
                    return(false);
                }

                if (this.Id != null && other.Id != null)
                {
                    if (!Unsafe.Equals(this.Id, other.Id))
                    {
                        return(false);
                    }
                }

                return(true);
            }
Пример #26
0
        public override bool Equals(DigitalSignature other)
        {
            if ((object)other == null)
            {
                return(false);
            }
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.Nickname != other.Nickname ||
                this.DigitalSignatureAlgorithm != other.DigitalSignatureAlgorithm ||
                ((this.PublicKey == null) != (other.PublicKey == null)) ||
                ((this.PrivateKey == null) != (other.PrivateKey == null)))
            {
                return(false);
            }

            if (this.PublicKey != null && other.PublicKey != null)
            {
                if (!Unsafe.Equals(this.PublicKey, other.PublicKey))
                {
                    return(false);
                }
            }

            if (this.PrivateKey != null && other.PrivateKey != null)
            {
                if (!Unsafe.Equals(this.PrivateKey, other.PrivateKey))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #27
0
        public void Equals()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024; i++)
            {
                byte[] x;
                byte[] y;

                if (random.Next(0, 2) == 0)
                {
                    var length = random.Next(0, 1024 * 256);
                    x = new byte[length];
                    y = new byte[length];
                }
                else
                {
                    x = new byte[random.Next(0, 1024 * 256)];
                    y = new byte[random.Next(0, 1024 * 256)];
                }

                if (random.Next(0, 3) == 0)
                {
                    random.NextBytes(x);
                    random.NextBytes(y);
                }

                for (int j = 0; j < 32; j++)
                {
                    bool result1 = false;
                    bool result2 = false;

                    random.Shuffle(flags);
                    foreach (var index in flags)
                    {
                        if (index == 0)
                        {
                            sw1.Start();
                            result1 = Unsafe.Equals(x, y);
                            sw1.Stop();
                        }
                        else if (index == 1)
                        {
                            sw2.Start();
                            result2 = Benchmark.Equals(x, y);
                            sw2.Stop();
                        }
                    }

                    Assert.IsTrue(result1 == result2);
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Equals: " + sw1.Elapsed.ToString());
            sb.AppendLine("Unsafe Equals: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
Пример #28
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();
                }
            }
        }
Пример #29
0
            public void Set(Hash hash, ArraySegment <byte> value)
            {
                if (value.Count > 1024 * 1024 * 32)
                {
                    throw new BadBlockException();
                }

                if (hash.Algorithm == HashAlgorithm.Sha256)
                {
                    if (!Unsafe.Equals(Sha256.Compute(value), hash.Value))
                    {
                        throw new BadBlockException();
                    }
                }
                else
                {
                    throw new FormatException();
                }

                lock (_lockObject)
                {
                    if (this.Contains(hash))
                    {
                        return;
                    }

                    var sectorList = new List <long>();

                    try
                    {
                        sectorList.AddRange(this.GetFreeSectors((value.Count + (SectorSize - 1)) / SectorSize));

                        for (int i = 0, remain = value.Count; i < sectorList.Count && 0 < remain; i++, remain -= SectorSize)
                        {
                            long posision = sectorList[i] * SectorSize;

                            if ((_fileStream.Length < posision + SectorSize))
                            {
                                int  unit = 1024 * 1024 * 256; // 256MB
                                long size = Roundup((posision + SectorSize), unit);

                                _fileStream.SetLength(Math.Min(size, this.Size));
                            }

                            if (_fileStream.Position != posision)
                            {
                                _fileStream.Seek(posision, SeekOrigin.Begin);
                            }

                            int length = Math.Min(remain, SectorSize);

                            Unsafe.Copy(value.Array, value.Offset + (SectorSize * i), _sectorBuffer, 0, length);
                            Unsafe.Zero(_sectorBuffer, length, _sectorBuffer.Length - length);

                            _fileStream.Write(_sectorBuffer, 0, _sectorBuffer.Length);
                        }

                        _fileStream.Flush();
                    }
                    catch (SpaceNotFoundException e)
                    {
                        Log.Error(e);

                        throw e;
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);

                        throw e;
                    }

                    _clusterIndex[hash] = new ClusterInfo(sectorList.ToArray(), value.Count, DateTime.UtcNow);

                    // Event
                    _addedBlockEventQueue.Enqueue(hash);
                }
            }
Пример #30
0
            public ArraySegment <byte>?Get(Hash hash)
            {
                ArraySegment <byte> result;

                lock (_lockObject)
                {
                    ClusterInfo clusterInfo = null;

                    if (_clusterIndex.TryGetValue(hash, out clusterInfo))
                    {
                        clusterInfo.UpdateTime = DateTime.UtcNow;
                    }

                    if (clusterInfo == null)
                    {
                        return(null);
                    }

                    var buffer = _bufferManager.TakeBuffer(clusterInfo.Length);

                    try
                    {
                        for (int i = 0, remain = clusterInfo.Length; i < clusterInfo.Indexes.Length; i++, remain -= SectorSize)
                        {
                            long posision = clusterInfo.Indexes[i] * SectorSize;
                            if (posision > _fileStream.Length)
                            {
                                throw new ArgumentOutOfRangeException();
                            }

                            if (_fileStream.Position != posision)
                            {
                                _fileStream.Seek(posision, SeekOrigin.Begin);
                            }

                            int length = Math.Min(remain, SectorSize);

                            _fileStream.Read(_sectorBuffer, 0, _sectorBuffer.Length);
                            Unsafe.Copy(_sectorBuffer, 0, buffer, SectorSize * i, length);
                        }

                        result = new ArraySegment <byte>(buffer, 0, clusterInfo.Length);
                    }
                    catch (Exception e)
                    {
                        _bufferManager.ReturnBuffer(buffer);

                        Log.Error(e);

                        return(null);
                    }
                }

                if (hash.Algorithm == HashAlgorithm.Sha256)
                {
                    if (Unsafe.Equals(Sha256.Compute(result), hash.Value))
                    {
                        return(result);
                    }
                    else
                    {
                        _bufferManager.ReturnBuffer(result.Array);

                        this.Remove(hash);

                        return(null);
                    }
                }
                else
                {
                    throw new FormatException();
                }
            }