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);
        }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 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;
         }
     }
 }
Ejemplo n.º 4
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);
            }
        }