private byte[] ParseSingleLine(string r) { if (log.IsDebugEnabled) { Log("R: {0}", r); } if (r.Length == 0) { throw CreateResponseError("Zero length response"); } char c = r[0]; if (c == '-') { throw CreateResponseError(r.StartsWith("-ERR") ? r.Substring(5) : r.Substring(1)); } if (c == '$') { if (r == "$-1") { return(null); } int count; if (int.TryParse(r.Substring(1), out count)) { var retbuf = new byte[count]; var offset = 0; while (count > 0) { var readCount = Bstream.Read(retbuf, offset, count); if (readCount <= 0) { throw CreateResponseError("Unexpected end of Stream"); } offset += readCount; count -= readCount; } if (Bstream.ReadByte() != '\r' || Bstream.ReadByte() != '\n') { throw CreateResponseError("Invalid termination"); } return(retbuf); } throw CreateResponseError("Invalid length"); } if (c == ':' || c == '+') { //match the return value return(r.Substring(1).ToUtf8Bytes()); } throw CreateResponseError("Unexpected reply: " + r); }
protected byte[] Bread(int length) { ThrowIfEnded(); byte[] data; if (Buffered < length) { BwaitIndex = BreadIndex + length; Bwait.WaitOne(); if (Ended) { return(null); } Bwait.Reset(); BwaitIndex = -1; } lock (BreadLock) { data = new byte[length]; lock (BwriteLock) { Bstream.Position = BreadIndex; Bstream.Read(data, 0, length); BreadIndex += length; } } Btrim(); return(data); }
private int SafeReadByte(string name) { if (log.IsDebugEnabled && RedisConfig.EnableVerboseLogging) { logDebug(name + "()"); } return(Bstream.ReadByte()); }
private int SafeReadByte() { try { return(Bstream.ReadByte()); } catch (Exception) { DeactivatedAt = DateTime.UtcNow; throw; } }
private int SafeReadByte() { try { return(Bstream.ReadByte()); } catch (Exception) { HadExceptions = true; throw; } }
/// <summary> /// Send command outside of managed Write Buffer /// </summary> /// <param name="cmdWithBinaryArgs"></param> protected void SendUnmanagedExpectSuccess(params byte[][] cmdWithBinaryArgs) { var bytes = GetCmdBytes('*', cmdWithBinaryArgs.Length); foreach (var safeBinaryValue in cmdWithBinaryArgs) { bytes = bytes.Combine(GetCmdBytes('$', safeBinaryValue.Length), safeBinaryValue, endData); } Bstream.Write(bytes, 0, bytes.Length); ExpectSuccess(); }
protected void Bwrite(byte[] data) { ThrowIfEnded(); lock (BwriteLock) { Bstream.Position = BwriteIndex; Bstream.Write(data, 0, data.Length); BwriteIndex += data.Length; if (BreadIndex != -1 && BwriteIndex >= BreadIndex) { Bwait.Set(); } } }
/// <summary> /// 读数字 /// </summary> /// <returns></returns> private int ReadNumber() { int state = Bstream.ReadByte(); if (state == -1) { Status = 1; return(-1); } if (state == '*') { return(int.Parse(ReadLine())); } Status = 2; return(-1); }
/// <summary> /// 读文本 /// </summary> /// <returns>文本</returns> private string ReadString() { int state = Bstream.ReadByte(); if (state == -1) { Status = 1; return(null); } if (state != '$') { Status = 2; return(null); } ReadLine(); //长度 return(ReadLine()); //值 }
/// <summary> /// Send command outside of managed Write Buffer /// </summary> /// <param name="cmdWithBinaryArgs"></param> protected void SendUnmanagedExpectSuccess(params byte[][] cmdWithBinaryArgs) { var bytes = GetCmdBytes('*', cmdWithBinaryArgs.Length); foreach (var safeBinaryValue in cmdWithBinaryArgs) { bytes = bytes.Combine(GetCmdBytes('$', safeBinaryValue.Length), safeBinaryValue, endData); } if (log.IsDebugEnabled && RedisConfig.EnableVerboseLogging) { logDebug("stream.Write: " + Encoding.UTF8.GetString(bytes, 0, Math.Min(bytes.Length, 50)).Replace("\r\n", " ").SafeSubstring(0, 50)); } Bstream.Write(bytes, 0, bytes.Length); ExpectSuccess(); }
virtual public void End() { ThrowIfEnded(); lock (BreadLock) { lock (BwriteLock) { Ended = true; Bstream.Dispose(); Bblock.Set(); Bwait.Set(); Bblock.Dispose(); Bwait.Dispose(); PipedTo = null; Paused = false; } } }
protected void Btrim() { ThrowIfEnded(); lock (BreadLock) { lock (BwriteLock) { int buffered = Buffered; Bstream.Position = BreadIndex; byte[] remain = new byte[buffered]; Bstream.Read(remain, 0, buffered); Bstream.Position = 0; Bstream.Write(remain, 0, buffered); BreadIndex = 0; BwriteIndex = buffered; } } }
protected string ReadLine() { var sb = new StringBuilder(); int c; while ((c = Bstream.ReadByte()) != -1) { if (c == '\r') { continue; } if (c == '\n') { break; } sb.Append((char)c); } return(sb.ToString()); }
protected string ReadLine() { var sb = StringBuilderCache.Allocate(); int c; while ((c = Bstream.ReadByte()) != -1) { if (c == '\r') { continue; } if (c == '\n') { break; } sb.Append((char)c); } return(StringBuilderCache.ReturnAndFree(sb)); }
private int SafeReadByte() { return(Bstream.ReadByte()); }
/// <summary> /// 读一行返回值 /// </summary> /// <param name="result">记录返回值的对象</param> /// <returns>false表示发生错误</returns> private bool ReadSingleLine(LineResult result) { result.type = Bstream.ReadByte(); if (result.type == -1) { Status = 1; return(false); } switch ((char)result.type) { case ':': result.type = 0; result.sValue = ReadLine(); return(true); case '-': result.type = -2; Status = 100; LastError = ReadLine(); return(false); case '*': result.type = 1; result.sValue = ReadLine(); return(true); case '$': result.type = 2; { int num; result.sValue = ReadLine(); if (result.sValue[0] == '-') { return(true); } if (!int.TryParse(result.sValue, out num)) { Status = -2; LastError = ("Invalid length"); return(false); } result.bValue = new byte[num]; int offset = 0; while (num > 0) { int num3 = Bstream.Read(result.bValue, offset, num); if (num3 <= 0) { Status = -2; LastError = ("Unexpected end of Stream"); return(false); } offset += num3; num -= num3; } if ((Bstream.ReadByte() != 13) || (Bstream.ReadByte() != 10)) { Status = -3; LastError = ("Invalid termination"); return(false); } return(true); } default: Status = -1; result.type = -3; LastError = "Unexpected reply: " + ReadLine(); return(false); } }