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);
        }
        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;
     }
 }
Пример #5
0
        /// <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);
        }
Пример #6
0
        /// <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()); //值
        }
        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());
 }
Пример #10
0
        /// <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);
            }
        }