Пример #1
0
            public Builder ReissueReadOnReconnect(bool reissueReadOnReconnect)
            {
                SAssert.IsTrue(_settings.ReconnectOnIdle, () =>
                               new ArgumentException("ReissueReadOnReconnect requires ReconnectOnIdle"));

                _settings.ReissueCommandsOnReconnect = reissueReadOnReconnect;
                return(this);
            }
Пример #2
0
            public Builder ReissuePipelinedCallsOnReconnect(bool reissuePipeline)
            {
                SAssert.IsTrue(_settings.ReconnectOnIdle, () => new ArgumentException(
                                   "ReissuePipelinedCallsOnReconnect requires ReconnectOnIdle"));

                _settings.ReissuePipelinedCallsOnReconnect = reissuePipeline;
                return(this);
            }
Пример #3
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);
        }
Пример #4
0
            public Builder MaxReconnectRetries(int maxRetries)
            {
                SAssert.ArgumentPositive(() => maxRetries);
                SAssert.IsTrue(_settings.ReconnectOnIdle, () =>
                               new ArgumentException("MaxReconnectRetries requires ReconnectOnIdle"));

                _settings.MaxReconnectRetries = maxRetries;
                return(this);
            }
Пример #5
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));
        }
Пример #6
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."));
            }
        }
Пример #7
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);
        }
Пример #8
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());
        }
Пример #9
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);
        }
Пример #10
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();
        }