//this requires a seekable stream for now
        public void Write(Type type, Array val)
        {
            Type elemType = type.GetElementType();

            //TODO: more fast paths for primitive arrays
            if (elemType == typeof(byte))
            {
                Write((uint)val.Length);
                stream.Write((byte[])val, 0, val.Length);
                return;
            }

            Write((uint)0);
            long lengthPos = stream.Position - 4;

            //advance to the alignment of the element
            WritePad(Protocol.GetAlignment(Signature.TypeToDType(elemType)));

            long startPos = stream.Position;

            foreach (object elem in val)
            {
                Write(elemType, elem);
            }

            long endPos = stream.Position;

            stream.Position = lengthPos;
            Write((uint)(endPos - startPos));

            stream.Position = endPos;
        }
Example #2
0
        public object ReadValue(Type type)
        {
            if (type == typeof(void))
            {
                return(null);
            }

            if (type.IsArray)
            {
                return(ReadArray(type.GetElementType()));
            }
            else if (type == typeof(ObjectPath))
            {
                return(ReadObjectPath());
            }
            else if (type == typeof(Signature))
            {
                return(ReadSignature());
            }
            else if (type == typeof(object))
            {
                return(ReadVariant());
            }
            else if (type == typeof(string))
            {
                return(ReadString());
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                Type[] genArgs = type.GetGenericArguments();
                //Type dictType = typeof (Dictionary<,>).MakeGenericType (genArgs);
                //workaround for Mono bug #81035 (memory leak)
                Type dictType = Mapper.GetGenericType(typeof(Dictionary <,>), genArgs);
                System.Collections.IDictionary idict = (System.Collections.IDictionary)Activator.CreateInstance(dictType, new object[0]);
                GetValueToDict(genArgs[0], genArgs[1], idict);
                return(idict);
            }
            else if (Mapper.IsPublic(type))
            {
                return(GetObject(type));
            }
            else if (!type.IsPrimitive && !type.IsEnum)
            {
                return(ReadStruct(type));
            }
            else
            {
                object val;
                DType  dtype = Signature.TypeToDType(type);
                val = ReadValue(dtype);

                if (type.IsEnum)
                {
                    val = Enum.ToObject(type, val);
                }

                return(val);
            }
        }
        public void Write(Type type, object val)
        {
            if (type == typeof(void))
            {
                return;
            }

            if (type.IsArray)
            {
                Write(type, (Array)val);
            }
            else if (type == typeof(ObjectPath))
            {
                Write((ObjectPath)val);
            }
            else if (type == typeof(Signature))
            {
                Write((Signature)val);
            }
            else if (type == typeof(object))
            {
                Write(val);
            }
            else if (type == typeof(string))
            {
                Write((string)val);
            }
            else if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary <,>) || type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                Type[] genArgs = type.GetGenericArguments();
                System.Collections.IDictionary idict = (System.Collections.IDictionary)val;
                WriteFromDict(genArgs[0], genArgs[1], idict);
            }
            else if (Mapper.IsPublic(type))
            {
                WriteObject(type, val);
            }
            else if (!type.IsPrimitive && !type.IsEnum)
            {
                WriteStruct(type, val);

                /*
                 * } else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) {
                 * //is it possible to support nullable types?
                 * Type[] genArgs = type.GetGenericArguments ();
                 * WriteVariant (genArgs[0], val);
                 */
            }
            else
            {
                Write(Signature.TypeToDType(type), val);
            }
        }
Example #4
0
        public void Write(Type type, object val)
        {
            if (type == typeof(void))
            {
                return;
            }

            if (type.IsArray)
            {
                MethodInfo miDict = typeof(MessageWriter).GetMethod("WriteArray");
                MethodInfo mi     = miDict.MakeGenericMethod(type.GetElementType());
                mi.Invoke(this, new object[] { val });
            }
            else if (type == typeof(ObjectPath))
            {
                Write((ObjectPath)val);
            }
            else if (type == typeof(Signature))
            {
                Write((Signature)val);
            }
            else if (type == typeof(object))
            {
                Write(val);
            }
            else if (type == typeof(string))
            {
                Write((string)val);
            }
            else if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary <,>) || type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                Type[]     genArgs = type.GetGenericArguments();
                MethodInfo miDict  = typeof(MessageWriter).GetMethod("WriteFromDict");
                MethodInfo mi      = miDict.MakeGenericMethod(genArgs);
                mi.Invoke(this, new object[] { val });
            }
            else if (Mapper.IsPublic(type))
            {
                WriteObject(type, val);
            }
            else if (!type.IsPrimitive && !type.IsEnum)
            {
                WriteValueType(val, type);
            }
            else
            {
                Write(Signature.TypeToDType(type), val);
            }
        }
        public void Write(Type type, object val)
        {
            if (type == typeof(void))
            {
                return;
            }

            if (type.IsArray)
            {
                WriteArray(val, type.GetElementType());
            }
            else if (type == typeof(ObjectPath))
            {
                Write((ObjectPath)val);
            }
            else if (type == typeof(Signature))
            {
                Write((Signature)val);
            }
            else if (type == typeof(object))
            {
                Write(val);
            }
            else if (type == typeof(string))
            {
                Write((string)val);
            }
            else if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary <,>) || type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                Type[] genArgs = type.GetGenericArguments();
                System.Collections.IDictionary idict = (System.Collections.IDictionary)val;
                WriteFromDict(genArgs[0], genArgs[1], idict);
            }
            else if (Mapper.IsPublic(type))
            {
                WriteObject(type, val);
            }
            else if (!type.IsPrimitive && !type.IsEnum)
            {
                WriteValueType(val, type);
            }
            else
            {
                Write(Signature.TypeToDType(type), val);
            }
        }
        //this requires a seekable stream for now
        public void WriteArray(object obj, Type elemType)
        {
            Array val = (Array)obj;

            //TODO: more fast paths for primitive arrays
            if (elemType == typeof(byte))
            {
                if (val.Length > Protocol.MaxArrayLength)
                {
                    throw new Exception("Array length " + val.Length + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
                }

                Write((uint)val.Length);
                stream.Write((byte[])val, 0, val.Length);
                return;
            }

            long origPos = stream.Position;

            Write((uint)0);

            //advance to the alignment of the element
            WritePad(Protocol.GetAlignment(Signature.TypeToDType(elemType)));

            long startPos = stream.Position;

            foreach (object elem in val)
            {
                Write(elemType, elem);
            }

            long endPos = stream.Position;
            uint ln     = (uint)(endPos - startPos);

            stream.Position = origPos;

            if (ln > Protocol.MaxArrayLength)
            {
                throw new Exception("Array length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
            }

            Write(ln);
            stream.Position = endPos;
        }
        //this could be made generic to avoid boxing
        public void GetValue(Type type, out Array val)
        {
            Type elemType = type.GetElementType();

            uint ln;

            GetValue(out ln);

            //TODO: more fast paths for primitive arrays
            if (elemType == typeof(byte))
            {
                byte[] valb = new byte[ln];
                Array.Copy(data, pos, valb, 0, (int)ln);
                val  = valb;
                pos += (int)ln;
                return;
            }

            //advance to the alignment of the element
            ReadPad(Protocol.GetAlignment(Signature.TypeToDType(elemType)));

            int endPos = pos + (int)ln;

            //List<T> vals = new List<T> ();
            System.Collections.ArrayList vals = new System.Collections.ArrayList();

            //while (stream.Position != endPos)
            while (pos < endPos)
            {
                object elem;
                //GetValue (Signature.TypeToDType (elemType), out elem);
                GetValue(elemType, out elem);
                vals.Add(elem);
            }

            if (pos != endPos)
            {
                throw new Exception("Read pos " + pos + " != ep " + endPos);
            }

            val = vals.ToArray(elemType);
            //val = Array.CreateInstance (elemType.UnderlyingSystemType, vals.Count);
        }
Example #8
0
        //this could be made generic to avoid boxing
        public Array ReadArray(Type elemType)
        {
            uint ln = ReadUInt32();

            if (ln > Protocol.MaxArrayLength)
            {
                throw new Exception("Array length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
            }

            //TODO: more fast paths for primitive arrays
            if (elemType == typeof(byte))
            {
                byte[] valb = new byte[ln];
                Array.Copy(data, pos, valb, 0, (int)ln);
                pos += (int)ln;
                return(valb);
            }

            //advance to the alignment of the element
            ReadPad(Protocol.GetAlignment(Signature.TypeToDType(elemType)));

            int endPos = pos + (int)ln;

            //List<T> vals = new List<T> ();
            System.Collections.ArrayList vals = new System.Collections.ArrayList();

            //while (stream.Position != endPos)
            while (pos < endPos)
            {
                vals.Add(ReadValue(elemType));
            }

            if (pos != endPos)
            {
                throw new Exception("Read pos " + pos + " != ep " + endPos);
            }

            return(vals.ToArray(elemType));
        }
        public static Signature GetSig(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            //this is inelegant, but works for now
            if (type == typeof(Signature))
            {
                return(new Signature(DType.Signature));
            }

            if (type == typeof(ObjectPath))
            {
                return(new Signature(DType.ObjectPath));
            }

            if (type == typeof(void))
            {
                return(Signature.Empty);
            }

            if (type == typeof(string))
            {
                return(new Signature(DType.String));
            }

            if (type == typeof(object))
            {
                return(new Signature(DType.Variant));
            }

            if (type.IsArray)
            {
                return(GetSig(type.GetElementType()).MakeArraySignature());
            }

            if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary <,>) || type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                Type[] genArgs = type.GetGenericArguments();
                return(Signature.MakeDict(GetSig(genArgs[0]), GetSig(genArgs[1])));
            }

            if (Mapper.IsPublic(type))
            {
                return(new Signature(DType.ObjectPath));
            }

            if (!type.IsPrimitive && !type.IsEnum)
            {
                Signature sig = Signature.Empty;

                foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    sig += GetSig(fi.FieldType);
                }

                return(Signature.MakeStruct(sig));
            }

            DType dtype = Signature.TypeToDType(type);

            return(new Signature(dtype));
        }
        public void GetValue(Type type, out object val)
        {
            if (type == typeof(void))
            {
                val = null;
                return;
            }

            if (type.IsArray)
            {
                Array valArr;
                GetValue(type, out valArr);
                val = valArr;
            }
            else if (type == typeof(ObjectPath))
            {
                ObjectPath valOP;
                GetValue(out valOP);
                val = valOP;
            }
            else if (type == typeof(Signature))
            {
                Signature valSig;
                GetValue(out valSig);
                val = valSig;
            }
            else if (type == typeof(object))
            {
                GetValue(out val);
            }
            else if (type == typeof(string))
            {
                string valStr;
                GetValue(out valStr);
                val = valStr;
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                Type[] genArgs = type.GetGenericArguments();
                //Type dictType = typeof (Dictionary<,>).MakeGenericType (genArgs);
                //workaround for Mono bug #81035 (memory leak)
                Type dictType = Mapper.GetGenericType(typeof(Dictionary <,>), genArgs);
                val = Activator.CreateInstance(dictType, new object[0]);
                System.Collections.IDictionary idict = (System.Collections.IDictionary)val;
                GetValueToDict(genArgs[0], genArgs[1], idict);
            }
            else if (Mapper.IsPublic(type))
            {
                GetObject(type, out val);
            }
            else if (!type.IsPrimitive && !type.IsEnum)
            {
                GetValueStruct(type, out val);
            }
            else
            {
                DType dtype = Signature.TypeToDType(type);
                GetValue(dtype, out val);
            }

            if (type.IsEnum)
            {
                val = Enum.ToObject(type, val);
            }
        }