private RedisRawResponse ExecuteInternal(IRedisConnection connection, bool throwException = true, bool sendNotReceive = false) { if (connection == null) { if (throwException) { throw new RedisFatalException(new ArgumentNullException("connection"), RedisErrorCode.MissingParameter); } return(null); } RedisRawResponse response = RedisVoidResponse.Void; if (sendNotReceive || !m_CommandType.HasFlag(RedisCommandType.SendAndReceive)) { connection.Send(this); } else { response = connection.SendReceive(this); if (ReferenceEquals(response, null) && throwException) { throw new RedisException("Corrupted redis response data", RedisErrorCode.CorruptResponse); } response.HandleError(); } return(response); }
protected static void SetReady(RedisRawResponse child) { child.SetReady(true); var parent = child.Parent as RedisRawResponse; if (parent != null) { var count = parent.ChildCount; if (count == 0 || count == parent.Length) { SetReady(parent); } } }
private RedisRawResponse ExecuteInternal(RedisSocketContext context, bool throwException = true, bool sendNotReceive = false) { if (context == null) { if (throwException) { throw new RedisFatalException(new ArgumentNullException("context"), RedisErrorCode.MissingParameter); } return(null); } var socket = context.Socket; if (socket == null) { if (throwException) { throw new RedisFatalException(new ArgumentNullException("context.Socket"), RedisErrorCode.MissingParameter); } return(null); } RedisRawResponse response = RedisVoidResponse.Void; if (sendNotReceive || !m_CommandType.HasFlag(RedisCommandType.SendAndReceive)) { WriteTo(socket); } else { WriteTo(socket); using (var reader = new RedisSingleResponseReader(context.Settings)) response = reader.Execute(socket); if (ReferenceEquals(response, null) && throwException) { throw new RedisException("Corrupted redis response data", RedisErrorCode.CorruptResponse); } response.HandleError(); } return(response); }
protected RedisRawResponse ProcessResponse(RedisSocket socket) { var b = ReadByte(socket); if (b < 0) { if (!Receiving) { return(null); } throw new RedisException("Unexpected byte for redis response type", RedisErrorCode.CorruptResponse); } var item = new RedisRawResponse(); item.SetTypeByte(b); if (item.Type == RedisRawObjectType.Undefined) { throw new RedisException("Undefined redis response type", RedisErrorCode.CorruptResponse); } var data = ReadLine(socket); if (data == null && !Receiving) { return(null); } switch (item.Type) { case RedisRawObjectType.Integer: case RedisRawObjectType.SimpleString: case RedisRawObjectType.Error: item.SetData(data); SetReady(item); break; case RedisRawObjectType.BulkString: { var lenStr = data.ToUTF8String(); if (lenStr.IsEmpty()) { throw new RedisException("Corrupted redis response, empty length for bulk string", RedisErrorCode.CorruptResponse); } int msgLength; if (!int.TryParse(lenStr, out msgLength)) { throw new RedisException("Corrupted redis response, not an integer value for bulk string", RedisErrorCode.CorruptResponse); } item.SetLength(Math.Max(-1, msgLength)); if (item.Length == -1) { item.SetData(null); } else { if (item.Length == 0) { item.SetData(new byte[0]); } else { data = ReadBytes(socket, item.Length); if (data == null && !Receiving) { return(null); } item.SetData(data); } if (!EatCRLF(socket)) { return(null); } } SetReady(item); } break; case RedisRawObjectType.Array: { var lenStr = data.ToUTF8String(); if (lenStr.IsEmpty()) { throw new RedisException("Corrupted redis response, empty length for array", RedisErrorCode.CorruptResponse); } int arrayLen; if (!int.TryParse(lenStr, out arrayLen)) { throw new RedisException("Corrupted redis response, not an integer value for array", RedisErrorCode.CorruptResponse); } arrayLen = Math.Max(-1, arrayLen); item.SetLength(arrayLen); if (arrayLen > 0) { for (var i = 0; i < arrayLen; i++) { var child = ProcessResponse(socket); if (child == null) { if (!Receiving) { return(null); } throw new RedisException("Unexpected response data, not valid data for array item", RedisErrorCode.CorruptResponse); } item.Add(child); } } } break; } return(item); }