public Builder EncodingBufferSize(int bufferSize) { SAssert.ArgumentPositive(() => bufferSize); _settings.EncodingBufferSize = bufferSize; return(this); }
public Builder SerializationBufferSize(int bufferSize) { SAssert.ArgumentPositive(() => bufferSize); _settings.SerializationBufferSize = bufferSize; return(this); }
public Builder Port(int port) { SAssert.ArgumentBetween(() => port, 1, 65535); _settings.Port = port; return(this); }
public Builder KeyEncoding(Encoding encoding) { SAssert.ArgumentNotNull(() => encoding); _settings.KeyEncoding = encoding; return(this); }
// 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); }
public Builder Host(string host) { SAssert.ArgumentSatisfy(() => host, v => !string.IsNullOrEmpty(v), "Host cannot be null or empty."); _settings.Host = host; return(this); }
public void WriteSerializedBulk <T>(ISerializer <T> serializer, T obj, int count) { SAssert.ArgumentNotNull(() => serializer); SAssert.ArgumentNonNegative(() => count); serializer.Write(obj, _stream, count); writeCrLf(); }
public Builder ReissueReadOnReconnect(bool reissueReadOnReconnect) { SAssert.IsTrue(_settings.ReconnectOnIdle, () => new ArgumentException("ReissueReadOnReconnect requires ReconnectOnIdle")); _settings.ReissueCommandsOnReconnect = reissueReadOnReconnect; return(this); }
public Builder ReissuePipelinedCallsOnReconnect(bool reissuePipeline) { SAssert.IsTrue(_settings.ReconnectOnIdle, () => new ArgumentException( "ReissuePipelinedCallsOnReconnect requires ReconnectOnIdle")); _settings.ReissuePipelinedCallsOnReconnect = reissuePipeline; return(this); }
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); }
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); }
public Builder MaxReconnectRetries(int maxRetries) { SAssert.ArgumentPositive(() => maxRetries); SAssert.IsTrue(_settings.ReconnectOnIdle, () => new ArgumentException("MaxReconnectRetries requires ReconnectOnIdle")); _settings.MaxReconnectRetries = maxRetries; return(this); }
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(); }
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; }
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)); }
public byte[] ReadBulk(int length) { SAssert.ArgumentNonNegative(() => length); var buffer = new byte[length]; ReadBulk(buffer, 0, length); return(buffer); }
public T ReadSerializedBulk <T>(ISerializer <T> serializer, int bulkLength) { SAssert.ArgumentNotNull(() => serializer); SAssert.ArgumentNonNegative(() => bulkLength); var result = serializer.Read(_stream, bulkLength); readCrLf(); return(result); }
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(); }
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; }
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.")); } }
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(); }
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(); }
// 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); }
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()); }
// 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); }
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(); }
private void readType(ResponseType expectedType) { var type = _reader.ReadTypeChar(); SAssert.ResponseType(expectedType, type, _reader); }
public void WriteBulk(byte[] buffer) { SAssert.ArgumentNotNull(() => buffer); WriteBulk(buffer, 0, buffer.Length); }