private void Deserialize(Stream content)
        {
            if (content == null || content.ReadByte() != SerializationRevision)
            {
                // Replace the un-readable format.
                _isModified = true;
                return;
            }

            int expectedEntries = DeserializeNumFrom3Bytes(content);

            _sessionIdBytes = ReadBytes(content, IdByteCount);

            for (int i = 0; i < expectedEntries; i++)
            {
                int keyLength  = DeserializeNumFrom2Bytes(content);
                var key        = new EncodedKey(ReadBytes(content, keyLength));
                int dataLength = DeserializeNumFrom4Bytes(content);
                _store[key] = ReadBytes(content, dataLength);
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _sessionId = new Guid(_sessionIdBytes).ToString();
                _logger.SessionLoaded(_sessionKey, _sessionId, expectedEntries);
            }
        }
        public void Set(string key, byte[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (IsAvailable)
            {
                var encodedKey = new EncodedKey(key);
                if (encodedKey.KeyBytes.Length > KeyLengthLimit)
                {
                    throw new ArgumentOutOfRangeException(nameof(key),
                                                          Resources.FormatException_KeyLengthIsExceeded(KeyLengthLimit));
                }

                if (!_tryEstablishSession())
                {
                    throw new InvalidOperationException(Resources.Exception_InvalidSessionEstablishment);
                }
                _isModified = true;
                byte[] copy = new byte[value.Length];
                Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
                _store[encodedKey] = copy;
            }
        }
Esempio n. 3
0
        public void Set(string key, byte[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (IsAvailable)
            {
                var encodedKey = new EncodedKey(key);
                if (encodedKey.KeyBytes.Length > KeyLengthLimit)
                {
                    throw new ArgumentOutOfRangeException(nameof(key),
                                                          $"The key cannot be longer than '{KeyLengthLimit}' when encoded with UTF-8.");
                }

                if (!_tryEstablishSession())
                {
                    throw new InvalidOperationException("The session cannot be established after the response has started.");
                }
                _isModified = true;
                byte[] copy = new byte[value.Length];
                Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
                _store[encodedKey] = copy;
            }
        }
Esempio n. 4
0
 public bool TryGetValue(EncodedKey key, [MaybeNullWhen(false)] out byte[] value)
 {
     value = null;
     return(false);
 }
Esempio n. 5
0
 public bool Remove(EncodedKey key) => false;
Esempio n. 6
0
 public void SetValue(EncodedKey key, byte[] value)
 {
 }
 public bool Remove(EncodedKey encodedKey)
 => _store.Remove(encodedKey);
 public void SetValue(EncodedKey key, byte[] value) => _store[key] = value;
 public bool TryGetValue(EncodedKey key, [MaybeNullWhen(false)] out byte[] value)
 => _store.TryGetValue(key, out value);