Exemplo n.º 1
0
 public static long ReadInt64(Stream stream)
 {
     using (var r = new BinReader(stream))
     {
         return r.ReadInt64();
     }
 }
Exemplo n.º 2
0
 public static int ReadInt32(Stream stream)
 {
     using (var r = new BinReader(stream))
     {
         return r.ReadInt32();
     }
 }
Exemplo n.º 3
0
        public override void Decode(Stream inStream)
        {
            using (BinReader r = new BinReader(inStream))
            {
                clientMajorVersion = r.ReadInt32();
                clientMinorVersion = r.ReadInt32();
            }

            base.Decode(inStream);
        }
Exemplo n.º 4
0
        public static bool getBool(Stream inStream)
        {
            using (BinReader r = new BinReader(inStream))
            {
                int value = r.ReadInt32();

                if (value == 0)
                    return false;
                else if (value == 1)
                    return true;
                else
                    throw new ProtocolCodecException
                      ("Cannot interpret " + value + " as boolean");
            }
        }
Exemplo n.º 5
0
        private static void DecodeKeys(Stream inStream, ISet<Key> keys)
        {
            int len;
            using (BinReader r = new BinReader(inStream))
            {
                len = r.ReadInt32();
            }

            for (int keysetCount = len; keysetCount > 0; keysetCount--)
                keys.Add(new Key(XdrCoding.getBytes(inStream)));
        }
Exemplo n.º 6
0
        public static Keys Decode(Stream inStream)
        {
            int length;

            using (BinReader r = new BinReader(inStream))
            {
                length = r.ReadInt32();
            }

            if (length == 0)
                return EmptyKeys;

            try
            {
                Keys keys = new Keys();

                for (; length > 0; length--)
                {
                    using (BinReader r = new BinReader(inStream))
                    {
                        KeyScheme scheme = KeyScheme.SchemeFor(r.ReadInt32());
                        int keySetCount = r.ReadInt32();

                        if (scheme.IsDual())
                        {
                            if (keySetCount != 2)
                                throw new ProtocolCodecException("Dual key scheme with " + keySetCount + " key sets");

                            DualKeySet keyset = keys.NewKeysetFor((DualKeyScheme)scheme);

                            DecodeKeys(inStream, keyset.ProducerKeys);
                            DecodeKeys(inStream, keyset.ConsumerKeys);
                        }
                        else
                        {
                            if (keySetCount != 1)
                                throw new ProtocolCodecException
                                  ("Single key scheme with " + keySetCount + " key sets");

                            DecodeKeys(inStream, keys.NewKeysetFor((SingleKeyScheme)scheme));
                        }
                    }
                }

                return keys;
            }
            catch (ArgumentException ex)
            {
                // most likely an invalid KeyScheme ID
                throw new ProtocolCodecException("Could not decode keys.", ex);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Read an int >= 0 or generate an exception.
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        private static int getPositiveInt(Stream inStream)
        {
            using (BinReader r = new BinReader(inStream))
            {
                int value = r.ReadInt32();

                if (value >= 0)
                    return value;
                else
                    throw new ProtocolCodecException("Length cannot be negative: " + value);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Read a length-delimited 4-byte-aligned UTF-8 string.
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        public static String getString(Stream inStream)
        {
            try
            {
                int length = getPositiveInt(inStream);

                if (length == 0)
                {
                    return "";
                }
                else
                {
                    String s;
                    using (BinReader r = new BinReader(inStream))
                    {
                        byte[] b = r.ReadBytes(length);
                        s = FromUTF8(b, 0, length);
                    }

                    inStream.Seek(paddingFor(length), SeekOrigin.Current);
                    return s;
                }
            }
            catch (Exception ex)
            {
                throw new ProtocolCodecException("Invalid UTF-8 string", ex);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Read an object in type_id/value format.
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        public static Object getObject(Stream inStream)
        {
            using (BinReader r = new BinReader(inStream))
            {
                int type = r.ReadInt32();

                switch (type)
                {
                    case TYPE_INT32:
                        return r.ReadInt32();
                    case TYPE_INT64:
                        return r.ReadInt64();
                    case TYPE_REAL64:
                        return r.ReadDouble();
                    case TYPE_STRING:
                        return getString(inStream);
                    case TYPE_OPAQUE:
                        return getBytes(inStream);
                    default:
                        throw new ProtocolCodecException("Unknown type code: " + type);
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Read a length-demlimited array of longs.
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        public static long[] getLongArray(Stream inStream)
        {
            long[] longs = new long[getPositiveInt(inStream)];

            using (BinReader r = new BinReader(inStream))
            {
                for (int i = 0; i < longs.Length; i++)
                    longs[i] = r.ReadInt64();
            }

            return longs;
        }