Example #1
0
            public Builder EncodingBufferSize(int bufferSize)
            {
                SAssert.ArgumentPositive(() => bufferSize);

                _settings.EncodingBufferSize = bufferSize;
                return(this);
            }
Example #2
0
            public Builder SerializationBufferSize(int bufferSize)
            {
                SAssert.ArgumentPositive(() => bufferSize);

                _settings.SerializationBufferSize = bufferSize;
                return(this);
            }
Example #3
0
            public Builder Port(int port)
            {
                SAssert.ArgumentBetween(() => port, 1, 65535);

                _settings.Port = port;
                return(this);
            }
Example #4
0
            public Builder KeyEncoding(Encoding encoding)
            {
                SAssert.ArgumentNotNull(() => encoding);

                _settings.KeyEncoding = encoding;
                return(this);
            }
Example #5
0
            // Authentication settings for thread-wise pool, unused elsewhere.
            // TODO: Probably better to move this to the pool itself.
            public Builder Auth(string password)
            {
                SAssert.ArgumentSatisfy(() => password, v => !string.IsNullOrEmpty(v),
                                        "Password cannot be null or empty.");

                _settings.Password = password;
                return(this);
            }
Example #6
0
            public Builder Host(string host)
            {
                SAssert.ArgumentSatisfy(() => host, v => !string.IsNullOrEmpty(v),
                                        "Host cannot be null or empty.");

                _settings.Host = host;
                return(this);
            }
Example #7
0
        public void WriteSerializedBulk <T>(ISerializer <T> serializer, T obj, int count)
        {
            SAssert.ArgumentNotNull(() => serializer);
            SAssert.ArgumentNonNegative(() => count);

            serializer.Write(obj, _stream, count);
            writeCrLf();
        }
Example #8
0
            public Builder ReissueReadOnReconnect(bool reissueReadOnReconnect)
            {
                SAssert.IsTrue(_settings.ReconnectOnIdle, () =>
                               new ArgumentException("ReissueReadOnReconnect requires ReconnectOnIdle"));

                _settings.ReissueCommandsOnReconnect = reissueReadOnReconnect;
                return(this);
            }
Example #9
0
            public Builder ReissuePipelinedCallsOnReconnect(bool reissuePipeline)
            {
                SAssert.IsTrue(_settings.ReconnectOnIdle, () => new ArgumentException(
                                   "ReissuePipelinedCallsOnReconnect requires ReconnectOnIdle"));

                _settings.ReissuePipelinedCallsOnReconnect = reissuePipeline;
                return(this);
            }
Example #10
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);
        }
Example #11
0
        public ResponseType ReadTypeChar()
        {
            var b = _stream.ReadByte();

            SAssert.IsTrue(Enum.IsDefined(typeof(ResponseType), b),
                           () => new ResponseException("Invalid response data, expected a response type character"));

            return((ResponseType)b);
        }
Example #12
0
            public Builder MaxReconnectRetries(int maxRetries)
            {
                SAssert.ArgumentPositive(() => maxRetries);
                SAssert.IsTrue(_settings.ReconnectOnIdle, () =>
                               new ArgumentException("MaxReconnectRetries requires ReconnectOnIdle"));

                _settings.MaxReconnectRetries = maxRetries;
                return(this);
            }
Example #13
0
        public void WriteTypeChar(ResponseType type)
        {
            SAssert.ArgumentSatisfy(() => type,
                                    v => Enum.IsDefined(typeof(ResponseType), v), "Invalid type char.");

            // assuming writebuffersize >= 1 so fitInBuffer(1) == true
            // the else case is for testing purpose
            _stream.WriteByte((byte)type);
            flushIfAuto();
        }
Example #14
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;
        }
Example #15
0
        private void readCrLf()
        {
            var cr = (byte)_stream.ReadByte();
            var lf = (byte)_stream.ReadByte();

            SAssert.IsTrue(cr == '\r',
                           () => new ResponseException("Expecting a CR, got instead: " + (char)cr));
            SAssert.IsTrue(lf == '\n',
                           () => new ResponseException("Expecting a LF, got instead: " + (char)lf));
        }
Example #16
0
        public byte[] ReadBulk(int length)
        {
            SAssert.ArgumentNonNegative(() => length);

            var buffer = new byte[length];

            ReadBulk(buffer, 0, length);

            return(buffer);
        }
Example #17
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);
        }
Example #18
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();
        }
Example #19
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;
        }
Example #20
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."));
            }
        }
Example #21
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();
        }
Example #22
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();
        }
Example #23
0
        // TODO: Respect culture? Revert to using int.Parse?
        public int ReadNumberLine()
        {
            bool negate = false;
            int  num    = 0;
            int  b;

            // read the first byte which can be a minus '-' sign
            b = _stream.ReadByte();
            if (b == '-')
            {
                negate = true;
            }
            else
            {
                num = b - '0';
            }

            // read the rest of the numbers
            while ((b = _stream.ReadByte()) != -1)
            {
                if (b == '\r')
                {
                    break;
                }

                // shifts the number by one digit, adding the current
                // digit to the end e.g. "99" + "7" = 99 * 10 + 7 = "997"
                num = (num * 10) + b - '0';

                SAssert.IsTrue(b >= '0' && b <= '9',
                               () => new ResponseException("Expecting a digit, found instead: " + (char)b));
            }

            // consume leftover LF
            b = _stream.ReadByte();

            SAssert.IsTrue(b == '\n',
                           () => new ResponseException("Expecting LF, found instead: " + (char)b));

            return(negate ? -num : num);
        }
Example #24
0
        public string ReadStatusLine()
        {
            var sb = new StringBuilder();
            int b;

            while ((b = _stream.ReadByte()) != -1)
            {
                if (b == '\r')
                {
                    break;
                }

                sb.Append((char)b);
            }

            // consume leftover LF
            b = _stream.ReadByte();

            SAssert.IsTrue(b == '\n',
                           () => new ResponseException("Expecting LF, found instead: " + (char)b));

            return(sb.ToString());
        }
Example #25
0
        // same implementation as ReadNumberLine() but expecting 64-bit values
        public long ReadNumberLine64()
        {
            bool negate = false;
            long num    = 0;
            int  b;

            b = _stream.ReadByte();
            if (b == '-')
            {
                negate = true;
            }
            else
            {
                num = b - '0';
            }

            while ((b = _stream.ReadByte()) != -1)
            {
                if (b == '\r')
                {
                    break;
                }

                num = (num * 10) + b - '0';

                SAssert.IsTrue(b >= '0' && b <= '9',
                               () => new ResponseException("Expected a digit, found instead: " + (char)b));
            }

            b = _stream.ReadByte();

            SAssert.IsTrue(b == '\n',
                           () => new ResponseException("Expecting LF, found instead: " + (char)b));

            return(negate ? -num : num);
        }
Example #26
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();
        }
Example #27
0
        private void readType(ResponseType expectedType)
        {
            var type = _reader.ReadTypeChar();

            SAssert.ResponseType(expectedType, type, _reader);
        }
Example #28
0
        public void WriteBulk(byte[] buffer)
        {
            SAssert.ArgumentNotNull(() => buffer);

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