Пример #1
0
            public Builder KeyEncoding(Encoding encoding)
            {
                SAssert.ArgumentNotNull(() => encoding);

                _settings.KeyEncoding = encoding;
                return(this);
            }
Пример #2
0
        public void WriteSerializedBulk <T>(ISerializer <T> serializer, T obj, int count)
        {
            SAssert.ArgumentNotNull(() => serializer);
            SAssert.ArgumentNonNegative(() => count);

            serializer.Write(obj, _stream, count);
            writeCrLf();
        }
Пример #3
0
        public RedisReader(Stream stream, RedisSettings settings)
        {
            SAssert.ArgumentNotNull(() => stream);
            SAssert.ArgumentSatisfy(() => stream, s => s.CanRead, "Stream must be readable.");
            SAssert.ArgumentNotNull(() => settings);

            _settings = settings;
            _stream   = new BufferedStream(stream, _settings.ReadBufferSize);
        }
Пример #4
0
        public LimitingStream(Stream inner, int bytesLimit)
        {
            SAssert.ArgumentNotNull(() => inner);
            SAssert.ArgumentNonNegative(() => bytesLimit);
            SAssert.ArgumentSatisfy(() => inner, s => s.CanRead,
                                    "Stream must be readable.");

            _inner     = inner;
            _bytesLeft = bytesLimit;
        }
Пример #5
0
        public T ReadSerializedBulk <T>(ISerializer <T> serializer, int bulkLength)
        {
            SAssert.ArgumentNotNull(() => serializer);
            SAssert.ArgumentNonNegative(() => bulkLength);

            var result = serializer.Read(_stream, bulkLength);

            readCrLf();

            return(result);
        }
Пример #6
0
        public void ReadBulkTo(Stream target, int bulkLength, int bufferSize)
        {
            SAssert.ArgumentNotNull(() => target);
            SAssert.ArgumentNonNegative(() => bulkLength);
            SAssert.ArgumentPositive(() => bufferSize);

            using (var limiter = new LimitingStream(_stream, bulkLength))
                limiter.CopyTo(target);

            readCrLf();
        }
Пример #7
0
        public RedisWriter(Stream stream, RedisSettings settings)
        {
            SAssert.ArgumentNotNull(() => stream);
            SAssert.ArgumentSatisfy(() => stream, s => s.CanWrite, "Stream must be writable.");
            SAssert.ArgumentNotNull(() => settings);

            _settings  = settings;
            _stream    = new BufferedStream(stream, _settings.WriteBufferSize);
            _strBuffer = new byte[_settings.EncodingBufferSize];

            AutoFlush = false;
        }
Пример #8
0
        public void WriteBulkFrom(Stream source, int count)
        {
            SAssert.ArgumentNotNull(() => source);
            SAssert.ArgumentNonNegative(() => count);

            using (var limiter = new LimitingStream(source, count)) {
                limiter.CopyTo(_stream);
                writeCrLf();

                SAssert.IsTrue(limiter.BytesLeft == 0,
                               () => new InvalidOperationException("Stream does not contains enough data."));
            }
        }
Пример #9
0
        public void WriteBulk(byte[] buffer, int offset, int count)
        {
            SAssert.ArgumentNotNull(() => buffer);

            if (!(offset == 0 && count == 0))
            {
                SAssert.ArgumentBetween(() => offset, 0, buffer.Length);
                SAssert.ArgumentBetween(() => count, 0, buffer.Length + 1);
            }

            SAssert.ArgumentSatisfy(() => offset, o => o + count <= buffer.Length,
                                    "Offset plus count is larger than the buffer.");

            _stream.Write(buffer, offset, count);
            writeCrLf();
        }
Пример #10
0
        public void WriteLine(string str)
        {
            SAssert.ArgumentNotNull(() => str);

            var bytesNeeded = Encoding.Default.GetByteCount(str);

            if (bytesNeeded < _strBuffer.Length)
            {
                Encoding.Default.GetBytes(str, 0, str.Length, _strBuffer, 0);
                _stream.Write(_strBuffer, 0, bytesNeeded);
            }
            else
            {
                var buffer = Encoding.Default.GetBytes(str);
                _stream.Write(buffer, 0, buffer.Length);
            }

            writeCrLf();
        }
Пример #11
0
        public void ReadBulk(byte[] buffer, int offset, int bulkLength)
        {
            SAssert.ArgumentNotNull(() => buffer);
            SAssert.ArgumentNonNegative(() => bulkLength);

            // special case for empty reads
            if (offset == 0 && bulkLength == 0)
            {
                return;
            }

            SAssert.ArgumentBetween(() => offset, 0, buffer.Length);
            SAssert.ArgumentSatisfy(() => offset, o => o + bulkLength <= buffer.Length,
                                    "Offset plus bulkLength is larger than the supplied buffer.");

            // read data from the stream, expect as much data as there's bulkLength
            var bytesRead = _stream.Read(buffer, 0, bulkLength);

            SAssert.IsTrue(bytesRead == bulkLength,
                           () => new ResponseException("Expected " + bulkLength.ToString() +
                                                       " bytes of bulk data, but only " + bytesRead.ToString() + " bytes are read."));

            readCrLf();
        }
Пример #12
0
        public void WriteBulk(byte[] buffer)
        {
            SAssert.ArgumentNotNull(() => buffer);

            WriteBulk(buffer, 0, buffer.Length);
        }