Exemplo n.º 1
0
        public ResponseDecoder(TsTtbResp response)
        {
            using (var s = new OtpInputStream(response.Response))
            {
                byte tag = s.Read1();
                if (tag != OtpExternal.VersionTag)
                {
                    string msg = string.Format(
                        "Expected OTP input stream to start with {0}, got {1}",
                        OtpExternal.VersionTag,
                        tag);
                    throw new InvalidOperationException(msg);
                }

                tag = s.Peek();
                switch (tag)
                {
                case OtpExternal.AtomTag:
                    string atom = s.ReadAtom();
                    decodedResponse = ParseAtomResult(atom);
                    break;

                case OtpExternal.SmallTupleTag:
                case OtpExternal.LargeTupleTag:
                    decodedResponse = ParseTupleResult(s);
                    break;

                default:
                    throw new InvalidOperationException("Expected an atom or tuple.");
                }
            }
        }
Exemplo n.º 2
0
        public ResponseDecoder(TsTtbResp response)
        {
            using (var s = new OtpInputStream(response.Response))
            {
                byte tag = s.Read1();
                if (tag != OtpExternal.VersionTag)
                {
                    string msg = string.Format(
                        "Expected OTP input stream to start with {0}, got {1}",
                        OtpExternal.VersionTag,
                        tag);
                    throw new InvalidOperationException(msg);
                }

                tag = s.Peek();
                switch (tag)
                {
                    case OtpExternal.AtomTag:
                        string atom = s.ReadAtom();
                        decodedResponse = ParseAtomResult(atom);
                        break;
                    case OtpExternal.SmallTupleTag:
                    case OtpExternal.LargeTupleTag:
                        decodedResponse = ParseTupleResult(s);
                        break;
                    default:
                        throw new InvalidOperationException("Expected an atom or tuple.");
                }
            }
        }
Exemplo n.º 3
0
 public void Can_Peek(byte[] buf, bool want)
 {
     using (var s = new OtpInputStream(buf))
     {
         byte b = s.Read1();
         Assert.AreEqual(OtpExternal.VersionTag, b);
         b = s.Peek();
         Assert.AreEqual(OtpExternal.AtomTag, b);
         string atom = s.ReadAtom();
         Assert.AreEqual(want.ToString().ToLowerInvariant(), atom);
     }
 }
Exemplo n.º 4
0
        private static Cell ParseCell(OtpInputStream s, int i, Column[] cols)
        {
            string msg;
            Column col = cols[i];
            byte   tag = s.Peek();

            switch (tag)
            {
            case OtpExternal.NilTag:
                tag = s.Read1();     // NB: actually consume the byte
                return(new Cell());

            case OtpExternal.BinTag:
                if (col.Type == ColumnType.Varchar || col.Type == ColumnType.Blob)
                {
                    return(new Cell(s.ReadBinaryAsString()));
                }
                else
                {
                    throw OnBadTag(tag, col, ColumnType.Varchar, ColumnType.Blob);
                }

            case OtpExternal.SmallIntTag:
            case OtpExternal.IntTag:
            case OtpExternal.SmallBigTag:
            case OtpExternal.LargeBigTag:
                long val = s.ReadLong();
                switch (col.Type)
                {
                case ColumnType.SInt64:
                    return(new Cell(val, isUnixTimestamp: false));

                case ColumnType.Timestamp:
                    return(new Cell(val, isUnixTimestamp: true));

                default:
                    throw OnBadTag(tag, col, ColumnType.SInt64, ColumnType.Timestamp);
                }

            case OtpExternal.FloatTag:
            case OtpExternal.NewFloatTag:
                if (col.Type != ColumnType.Double)
                {
                    throw OnBadTag(tag, col, ColumnType.Double);
                }

                return(new Cell(s.ReadDouble()));

            case OtpExternal.AtomTag:
                if (col.Type != ColumnType.Boolean)
                {
                    throw OnBadTag(tag, col, ColumnType.Boolean);
                }

                return(new Cell(s.ReadBoolean()));

            case OtpExternal.ListTag:
                int arity = s.ReadNil();
                if (arity == 0)
                {
                    // null cell
                    return(new Cell());
                }
                else
                {
                    msg = string.Format(
                        "Expected nil list, got one with arity {0}",
                        arity);
                    throw new InvalidOperationException(msg);
                }

            default:
                msg = string.Format(
                    "Unknown cell type encountered, tag {0}, '{1}:{2}'",
                    tag,
                    col.Name,
                    col.Type);
                throw new InvalidOperationException(msg);
            }
        }
Exemplo n.º 5
0
        private static Cell ParseCell(OtpInputStream s, int i, Column[] cols)
        {
            string msg;
            Column col = cols[i];
            byte tag = s.Peek();
            switch (tag)
            {
                case OtpExternal.NilTag:
                    tag = s.Read1(); // NB: actually consume the byte
                    return new Cell();

                case OtpExternal.BinTag:
                    if (col.Type == ColumnType.Varchar || col.Type == ColumnType.Blob)
                    {
                        return new Cell(s.ReadBinaryAsString());
                    }
                    else
                    {
                        throw OnBadTag(tag, col, ColumnType.Varchar, ColumnType.Blob);
                    }

                case OtpExternal.SmallIntTag:
                case OtpExternal.IntTag:
                case OtpExternal.SmallBigTag:
                case OtpExternal.LargeBigTag:
                    long val = s.ReadLong();
                    switch (col.Type)
                    {
                        case ColumnType.SInt64:
                            return new Cell(val, isUnixTimestamp: false);
                        case ColumnType.Timestamp:
                            return new Cell(val, isUnixTimestamp: true);
                        default:
                            throw OnBadTag(tag, col, ColumnType.SInt64, ColumnType.Timestamp);
                    }

                case OtpExternal.FloatTag:
                case OtpExternal.NewFloatTag:
                    if (col.Type != ColumnType.Double)
                    {
                        throw OnBadTag(tag, col, ColumnType.Double);
                    }

                    return new Cell(s.ReadDouble());

                case OtpExternal.AtomTag:
                    if (col.Type != ColumnType.Boolean)
                    {
                        throw OnBadTag(tag, col, ColumnType.Boolean);
                    }

                    return new Cell(s.ReadBoolean());

                case OtpExternal.ListTag:
                    int arity = s.ReadNil();
                    if (arity == 0)
                    {
                        // null cell
                        return new Cell();
                    }
                    else
                    {
                        msg = string.Format(
                            "Expected nil list, got one with arity {0}",
                            arity);
                        throw new InvalidOperationException(msg);
                    }

                default:
                    msg = string.Format(
                        "Unknown cell type encountered, tag {0}, '{1}:{2}'",
                        tag,
                        col.Name,
                        col.Type);
                    throw new InvalidOperationException(msg);
            }
        }