private static object[] GetExtraProperties(IDictionary properties, string[] internalKeys)
        {
            object[] array = new object[properties.Count - internalKeys.Length];
            int      num   = 0;
            IDictionaryEnumerator enumerator = properties.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (!MessageFormatter.IsInternalKey((string)enumerator.Entry.Key, internalKeys))
                {
                    array[num++] = enumerator.Entry;
                }
            }
            return(array);
        }
        private object NoCheckDeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException("serializationStream");
            }
            if (serializationStream.CanSeek && serializationStream.Length == 0L)
            {
                throw new SerializationException("serializationStream supports seeking, but its length is 0");
            }
            BinaryReader reader = new BinaryReader(serializationStream);
            bool         hasHeaders;

            this.ReadBinaryHeader(reader, out hasHeaders);
            return(MessageFormatter.ReadMethodResponse(reader, hasHeaders, handler, methodCallMessage, this));
        }
예제 #3
0
        // shared by Deserialize and UnsafeDeserialize which both involve different security checks
        private object NoCheckDeserialize(Stream serializationStream, HeaderHandler handler)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException("serializationStream");
            }
            if (serializationStream.CanSeek &&
                serializationStream.Length == 0)
            {
                throw new SerializationException("serializationStream supports seeking, but its length is 0");
            }

            BinaryReader reader = new BinaryReader(serializationStream);

            bool hasHeader;

            ReadBinaryHeader(reader, out hasHeader);

            // Messages are read using a special static method, which does not use ObjectReader
            // if it is not needed. This saves time and memory.

            BinaryElement elem = (BinaryElement)reader.Read();

            if (elem == BinaryElement.MethodCall)
            {
                return(MessageFormatter.ReadMethodCall(elem, reader, hasHeader, handler, this));
            }
            else if (elem == BinaryElement.MethodResponse)
            {
                return(MessageFormatter.ReadMethodResponse(elem, reader, hasHeader, handler, null, this));
            }
            else
            {
                ObjectReader serializer = new ObjectReader(this);

                object   result;
                Header[] headers;
                serializer.ReadObjectGraph(elem, reader, hasHeader, out result, out headers);
                if (handler != null)
                {
                    handler(headers);
                }
                return(result);
            }
        }
        /// <summary>Serializes the object, or graph of objects with the specified top (root), to the given stream attaching the provided headers.</summary>
        /// <param name="serializationStream">The stream to which the object is to be serialized. </param>
        /// <param name="graph">The object at the root of the graph to serialize. </param>
        /// <param name="headers">Remoting headers to include in the serialization. Can be null. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="serializationStream" /> is null. </exception>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">An error has occurred during serialization, such as if an object in the <paramref name="graph" /> parameter is not marked as serializable. </exception>
        /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="MemberAccess" />
        /// </PermissionSet>
        public void Serialize(Stream serializationStream, object graph, Header[] headers)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException("serializationStream");
            }
            BinaryWriter binaryWriter = new BinaryWriter(serializationStream);

            this.WriteBinaryHeader(binaryWriter, headers != null);
            if (graph is IMethodCallMessage)
            {
                MessageFormatter.WriteMethodCall(binaryWriter, graph, headers, this.surrogate_selector, this.context, this.assembly_format, this.type_format);
            }
            else if (graph is IMethodReturnMessage)
            {
                MessageFormatter.WriteMethodResponse(binaryWriter, graph, headers, this.surrogate_selector, this.context, this.assembly_format, this.type_format);
            }
            else
            {
                ObjectWriter objectWriter = new ObjectWriter(this.surrogate_selector, this.context, this.assembly_format, this.type_format);
                objectWriter.WriteObjectGraph(binaryWriter, graph, headers);
            }
            binaryWriter.Flush();
        }
        public static object ReadMethodResponse(BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
        {
            BinaryElement elem = (BinaryElement)reader.ReadByte();

            return(MessageFormatter.ReadMethodResponse(elem, reader, hasHeaders, headerHandler, methodCallMessage, formatter));
        }
        public static void WriteMethodResponse(BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
        {
            IMethodReturnMessage methodReturnMessage = (IMethodReturnMessage)obj;

            writer.Write(22);
            string[] array = MethodReturnDictionary.InternalReturnKeys;
            int      num   = 0;
            object   obj2  = null;

            object[]      array2      = null;
            MethodFlags   methodFlags = MethodFlags.ExcludeLogicalCallContext;
            ReturnTypeTag returnTypeTag;

            if (methodReturnMessage.Exception != null)
            {
                returnTypeTag = (ReturnTypeTag)34;
                array         = MethodReturnDictionary.InternalExceptionKeys;
                num           = 1;
            }
            else if (methodReturnMessage.ReturnValue == null)
            {
                returnTypeTag = ReturnTypeTag.Null;
            }
            else if (MessageFormatter.IsMethodPrimitive(methodReturnMessage.ReturnValue.GetType()))
            {
                returnTypeTag = ReturnTypeTag.PrimitiveType;
            }
            else
            {
                returnTypeTag = ReturnTypeTag.ObjectType;
                num++;
            }
            if (methodReturnMessage.LogicalCallContext != null && methodReturnMessage.LogicalCallContext.HasInfo)
            {
                methodFlags = MethodFlags.IncludesLogicalCallContext;
                num++;
            }
            if (methodReturnMessage.Properties.Count > array.Length && (byte)(returnTypeTag & ReturnTypeTag.Exception) == 0)
            {
                array2 = MessageFormatter.GetExtraProperties(methodReturnMessage.Properties, array);
                num++;
            }
            MethodFlags methodFlags2;

            if (methodReturnMessage.OutArgCount == 0)
            {
                methodFlags2 = MethodFlags.NoArguments;
            }
            else if (MessageFormatter.AllTypesArePrimitive(methodReturnMessage.Args))
            {
                methodFlags2 = MethodFlags.PrimitiveArguments;
            }
            else if (num == 0)
            {
                methodFlags2 = MethodFlags.ArgumentsInSimpleArray;
            }
            else
            {
                methodFlags2 = MethodFlags.ArgumentsInMultiArray;
                num++;
            }
            writer.Write((byte)(methodFlags | methodFlags2));
            writer.Write((byte)returnTypeTag);
            writer.Write(0);
            writer.Write(0);
            if (returnTypeTag == ReturnTypeTag.PrimitiveType)
            {
                writer.Write(BinaryCommon.GetTypeCode(methodReturnMessage.ReturnValue.GetType()));
                ObjectWriter.WritePrimitiveValue(writer, methodReturnMessage.ReturnValue);
            }
            if (methodFlags2 == MethodFlags.PrimitiveArguments)
            {
                writer.Write((uint)methodReturnMessage.ArgCount);
                for (int i = 0; i < methodReturnMessage.ArgCount; i++)
                {
                    object arg = methodReturnMessage.GetArg(i);
                    if (arg != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(arg.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, arg);
                    }
                    else
                    {
                        writer.Write(17);
                    }
                }
            }
            if (num > 0)
            {
                object[] array3 = new object[num];
                int      num2   = 0;
                if ((byte)(returnTypeTag & ReturnTypeTag.Exception) != 0)
                {
                    array3[num2++] = methodReturnMessage.Exception;
                }
                if (methodFlags2 == MethodFlags.ArgumentsInMultiArray)
                {
                    array3[num2++] = methodReturnMessage.Args;
                }
                if (returnTypeTag == ReturnTypeTag.ObjectType)
                {
                    array3[num2++] = methodReturnMessage.ReturnValue;
                }
                if (methodFlags == MethodFlags.IncludesLogicalCallContext)
                {
                    array3[num2++] = methodReturnMessage.LogicalCallContext;
                }
                if (array2 != null)
                {
                    array3[num2++] = array2;
                }
                obj2 = array3;
            }
            else if ((methodFlags2 & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
            {
                obj2 = methodReturnMessage.Args;
            }
            if (obj2 != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context, assemblyFormat, typeFormat);
                objectWriter.WriteObjectGraph(writer, obj2, headers);
            }
            else
            {
                writer.Write(11);
            }
        }
        public static void WriteMethodCall(BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
        {
            IMethodCallMessage methodCallMessage = (IMethodCallMessage)obj;

            writer.Write(21);
            int    num  = 0;
            object obj2 = null;

            object[]    array = null;
            MethodFlags methodFlags;

            if (methodCallMessage.LogicalCallContext != null && methodCallMessage.LogicalCallContext.HasInfo)
            {
                methodFlags = MethodFlags.IncludesLogicalCallContext;
                num++;
            }
            else
            {
                methodFlags = MethodFlags.ExcludeLogicalCallContext;
            }
            if (RemotingServices.IsMethodOverloaded(methodCallMessage))
            {
                num++;
                methodFlags |= MethodFlags.IncludesSignature;
            }
            if (methodCallMessage.Properties.Count > MethodCallDictionary.InternalKeys.Length)
            {
                array = MessageFormatter.GetExtraProperties(methodCallMessage.Properties, MethodCallDictionary.InternalKeys);
                num++;
            }
            if (methodCallMessage.MethodBase.IsGenericMethod)
            {
                num++;
                methodFlags |= MethodFlags.GenericArguments;
            }
            if (methodCallMessage.ArgCount == 0)
            {
                methodFlags |= MethodFlags.NoArguments;
            }
            else if (MessageFormatter.AllTypesArePrimitive(methodCallMessage.Args))
            {
                methodFlags |= MethodFlags.PrimitiveArguments;
            }
            else if (num == 0)
            {
                methodFlags |= MethodFlags.ArgumentsInSimpleArray;
            }
            else
            {
                methodFlags |= MethodFlags.ArgumentsInMultiArray;
                num++;
            }
            writer.Write((int)methodFlags);
            writer.Write(18);
            writer.Write(methodCallMessage.MethodName);
            writer.Write(18);
            writer.Write(methodCallMessage.TypeName);
            if ((methodFlags & MethodFlags.PrimitiveArguments) > (MethodFlags)0)
            {
                writer.Write((uint)methodCallMessage.Args.Length);
                for (int i = 0; i < methodCallMessage.ArgCount; i++)
                {
                    object arg = methodCallMessage.GetArg(i);
                    if (arg != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(arg.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, arg);
                    }
                    else
                    {
                        writer.Write(17);
                    }
                }
            }
            if (num > 0)
            {
                object[] array2 = new object[num];
                int      num2   = 0;
                if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.Args;
                }
                if ((methodFlags & MethodFlags.GenericArguments) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.MethodBase.GetGenericArguments();
                }
                if ((methodFlags & MethodFlags.IncludesSignature) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.MethodSignature;
                }
                if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.LogicalCallContext;
                }
                if (array != null)
                {
                    array2[num2++] = array;
                }
                obj2 = array2;
            }
            else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
            {
                obj2 = methodCallMessage.Args;
            }
            if (obj2 != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context, assemblyFormat, typeFormat);
                objectWriter.WriteObjectGraph(writer, obj2, headers);
            }
            else
            {
                writer.Write(11);
            }
        }