public void Serialize(Stream serializationStream, object graph, Header[] headers) { if (serializationStream == null) { throw new ArgumentNullException("serializationStream"); } BinaryWriter writer = new BinaryWriter(serializationStream); WriteBinaryHeader(writer, headers != null); if (graph is IMethodCallMessage) { MessageFormatter.WriteMethodCall(writer, graph, headers, surrogate_selector, context, assembly_format, type_format); } else if (graph is IMethodReturnMessage) { MessageFormatter.WriteMethodResponse(writer, graph, headers, surrogate_selector, context, assembly_format, type_format); } else { ObjectWriter serializer = new ObjectWriter(surrogate_selector, context, assembly_format, type_format); serializer.WriteObjectGraph(writer, graph, headers); } writer.Flush(); }
public static void WriteMethodResponse(BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter) { IMethodReturnMessage resp = (IMethodReturnMessage)obj; writer.Write((byte)BinaryElement.MethodResponse); string[] internalProperties = MethodReturnDictionary.InternalReturnKeys; int infoArrayLength = 0; object info = null; object[] extraProperties = null; // Type of return value ReturnTypeTag returnTypeTag; MethodFlags contextFlag = MethodFlags.ExcludeLogicalCallContext; if (resp.Exception != null) { returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null; internalProperties = MethodReturnDictionary.InternalExceptionKeys; infoArrayLength = 1; } else if (resp.ReturnValue == null) { returnTypeTag = ReturnTypeTag.Null; } else if (IsMethodPrimitive(resp.ReturnValue.GetType())) { returnTypeTag = ReturnTypeTag.PrimitiveType; } else { returnTypeTag = ReturnTypeTag.ObjectType; infoArrayLength++; } // Message flags MethodFlags formatFlag; if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo) { contextFlag = MethodFlags.IncludesLogicalCallContext; infoArrayLength++; } if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0)) { extraProperties = GetExtraProperties(resp.Properties, internalProperties); infoArrayLength++; } if (resp.OutArgCount == 0) { formatFlag = MethodFlags.NoArguments; } else { if (AllTypesArePrimitive(resp.Args)) { formatFlag = MethodFlags.PrimitiveArguments; } else { if (infoArrayLength == 0) { formatFlag = MethodFlags.ArgumentsInSimpleArray; } else { formatFlag = MethodFlags.ArgumentsInMultiArray; infoArrayLength++; } } } writer.Write((byte)(contextFlag | formatFlag)); writer.Write((byte)returnTypeTag); // FIXME: what are the following 2 bytes for? writer.Write((byte)0); writer.Write((byte)0); // Arguments if (returnTypeTag == ReturnTypeTag.PrimitiveType) { writer.Write(BinaryCommon.GetTypeCode(resp.ReturnValue.GetType())); ObjectWriter.WritePrimitiveValue(writer, resp.ReturnValue); } if (formatFlag == MethodFlags.PrimitiveArguments) { writer.Write((uint)resp.ArgCount); for (int n = 0; n < resp.ArgCount; n++) { object val = resp.GetArg(n); if (val != null) { writer.Write(BinaryCommon.GetTypeCode(val.GetType())); ObjectWriter.WritePrimitiveValue(writer, val); } else { writer.Write((byte)BinaryTypeCode.Null); } } } if (infoArrayLength > 0) { object[] infoArray = new object[infoArrayLength]; int n = 0; if ((returnTypeTag & ReturnTypeTag.Exception) != 0) { infoArray[n++] = resp.Exception; } if (formatFlag == MethodFlags.ArgumentsInMultiArray) { infoArray[n++] = resp.Args; } if (returnTypeTag == ReturnTypeTag.ObjectType) { infoArray[n++] = resp.ReturnValue; } if (contextFlag == MethodFlags.IncludesLogicalCallContext) { infoArray[n++] = resp.LogicalCallContext; } if (extraProperties != null) { infoArray[n++] = extraProperties; } info = infoArray; } else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0) { info = resp.Args; } if (info != null) { ObjectWriter objectWriter = new ObjectWriter(formatter); objectWriter.WriteObjectGraph(writer, info, headers); } else { writer.Write((byte)BinaryElement.End); } }
public static void WriteMethodCall(BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter) { IMethodCallMessage call = (IMethodCallMessage)obj; writer.Write((byte)BinaryElement.MethodCall); MethodFlags methodFlags; int infoArraySize = 0; object info = null; object[] extraProperties = null; if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo) { methodFlags = MethodFlags.IncludesLogicalCallContext; infoArraySize++; } else { methodFlags = MethodFlags.ExcludeLogicalCallContext; } if (RemotingServices.IsMethodOverloaded(call)) { infoArraySize++; methodFlags |= MethodFlags.IncludesSignature; } if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length) { extraProperties = GetExtraProperties(call.Properties, MethodCallDictionary.InternalKeys); infoArraySize++; } if (call.MethodBase.IsGenericMethod) { infoArraySize++; methodFlags |= MethodFlags.GenericArguments; } if (call.ArgCount == 0) { methodFlags |= MethodFlags.NoArguments; } else { if (AllTypesArePrimitive(call.Args)) { methodFlags |= MethodFlags.PrimitiveArguments; } else { if (infoArraySize == 0) { methodFlags |= MethodFlags.ArgumentsInSimpleArray; } else { methodFlags |= MethodFlags.ArgumentsInMultiArray; infoArraySize++; } } } writer.Write((int)methodFlags); // Method name writer.Write((byte)BinaryTypeCode.String); writer.Write(call.MethodName); // Class name writer.Write((byte)BinaryTypeCode.String); writer.Write(call.TypeName); // Arguments if ((methodFlags & MethodFlags.PrimitiveArguments) > 0) { writer.Write((uint)call.Args.Length); for (int n = 0; n < call.ArgCount; n++) { object arg = call.GetArg(n); if (arg != null) { writer.Write(BinaryCommon.GetTypeCode(arg.GetType())); ObjectWriter.WritePrimitiveValue(writer, arg); } else { writer.Write((byte)BinaryTypeCode.Null); } } } if (infoArraySize > 0) { object[] ainfo = new object[infoArraySize]; int n = 0; if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0) { ainfo[n++] = call.Args; } if ((methodFlags & MethodFlags.GenericArguments) > 0) { ainfo[n++] = call.MethodBase.GetGenericArguments(); } if ((methodFlags & MethodFlags.IncludesSignature) > 0) { ainfo[n++] = call.MethodSignature; } if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0) { ainfo[n++] = call.LogicalCallContext; } if (extraProperties != null) { ainfo[n++] = extraProperties; } info = ainfo; } else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0) { info = call.Args; } if (info != null) { ObjectWriter objectWriter = new ObjectWriter(formatter); objectWriter.WriteObjectGraph(writer, info, headers); } else { writer.Write((byte)BinaryElement.End); } }
public static void WriteMethodCall (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat) { IMethodCallMessage call = (IMethodCallMessage)obj; writer.Write ((byte) BinaryElement.MethodCall); MethodFlags methodFlags; int infoArraySize = 0; object info = null; object[] extraProperties = null; if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo) { methodFlags = MethodFlags.IncludesLogicalCallContext; infoArraySize++; } else methodFlags = MethodFlags.ExcludeLogicalCallContext; if (RemotingServices.IsMethodOverloaded (call)) { infoArraySize++; methodFlags |= MethodFlags.IncludesSignature; } if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length) { extraProperties = GetExtraProperties (call.Properties, MethodCallDictionary.InternalKeys); infoArraySize++; } #if NET_2_0 if (call.MethodBase.IsGenericMethod) { infoArraySize++; methodFlags |= MethodFlags.GenericArguments; } #endif if (call.ArgCount == 0) methodFlags |= MethodFlags.NoArguments; else { if (AllTypesArePrimitive (call.Args)) methodFlags |= MethodFlags.PrimitiveArguments; else { if (infoArraySize == 0) methodFlags |= MethodFlags.ArgumentsInSimpleArray; else { methodFlags |= MethodFlags.ArgumentsInMultiArray; infoArraySize++; } } } writer.Write ((int) methodFlags); // Method name writer.Write ((byte) BinaryTypeCode.String); writer.Write (call.MethodName); // Class name writer.Write ((byte) BinaryTypeCode.String); writer.Write (call.TypeName); // Arguments if ((methodFlags & MethodFlags.PrimitiveArguments) > 0) { writer.Write ((uint)call.Args.Length); for (int n=0; n<call.ArgCount; n++) { object arg = call.GetArg(n); if (arg != null) { writer.Write (BinaryCommon.GetTypeCode (arg.GetType())); ObjectWriter.WritePrimitiveValue (writer, arg); } else writer.Write ((byte)BinaryTypeCode.Null); } } if ( infoArraySize > 0) { object[] ainfo = new object[infoArraySize]; int n=0; if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0) ainfo[n++] = call.Args; #if NET_2_0 if ((methodFlags & MethodFlags.GenericArguments) > 0) ainfo[n++] = call.MethodBase.GetGenericArguments (); #endif if ((methodFlags & MethodFlags.IncludesSignature) > 0) ainfo[n++] = call.MethodSignature; if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0) ainfo[n++] = call.LogicalCallContext; if (extraProperties != null) ainfo[n++] = extraProperties; info = ainfo; } else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0) info = call.Args; if (info != null) { ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat); objectWriter.WriteObjectGraph (writer, info, headers); } else writer.Write ((byte) BinaryElement.End); }
public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat) { IMethodReturnMessage resp = (IMethodReturnMessage)obj; writer.Write ((byte) BinaryElement.MethodResponse); string[] internalProperties = MethodReturnDictionary.InternalReturnKeys; int infoArrayLength = 0; object info = null; object[] extraProperties = null; // Type of return value ReturnTypeTag returnTypeTag; MethodFlags contextFlag = MethodFlags.ExcludeLogicalCallContext; if (resp.Exception != null) { returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null; internalProperties = MethodReturnDictionary.InternalExceptionKeys; infoArrayLength = 1; } else if (resp.ReturnValue == null) { returnTypeTag = ReturnTypeTag.Null; } else if (IsMethodPrimitive(resp.ReturnValue.GetType())) { returnTypeTag = ReturnTypeTag.PrimitiveType; } else { returnTypeTag = ReturnTypeTag.ObjectType; infoArrayLength++; } // Message flags MethodFlags formatFlag; if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo) { contextFlag = MethodFlags.IncludesLogicalCallContext; infoArrayLength++; } if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0)) { extraProperties = GetExtraProperties (resp.Properties, internalProperties); infoArrayLength++; } if (resp.OutArgCount == 0) formatFlag = MethodFlags.NoArguments; else { if (AllTypesArePrimitive (resp.Args)) formatFlag = MethodFlags.PrimitiveArguments; else { if (infoArrayLength == 0) formatFlag = MethodFlags.ArgumentsInSimpleArray; else { formatFlag = MethodFlags.ArgumentsInMultiArray; infoArrayLength++; } } } writer.Write ((byte) (contextFlag | formatFlag)); writer.Write ((byte) returnTypeTag); // FIXME: what are the following 2 bytes for? writer.Write ((byte) 0); writer.Write ((byte) 0); // Arguments if (returnTypeTag == ReturnTypeTag.PrimitiveType) { writer.Write (BinaryCommon.GetTypeCode (resp.ReturnValue.GetType())); ObjectWriter.WritePrimitiveValue (writer, resp.ReturnValue); } if (formatFlag == MethodFlags.PrimitiveArguments) { writer.Write ((uint)resp.ArgCount); for (int n=0; n<resp.ArgCount; n++) { object val = resp.GetArg(n); if (val != null) { writer.Write (BinaryCommon.GetTypeCode (val.GetType())); ObjectWriter.WritePrimitiveValue (writer, val); } else writer.Write ((byte)BinaryTypeCode.Null); } } if (infoArrayLength > 0) { object[] infoArray = new object[infoArrayLength]; int n = 0; if ((returnTypeTag & ReturnTypeTag.Exception) != 0) infoArray[n++] = resp.Exception; if (formatFlag == MethodFlags.ArgumentsInMultiArray) infoArray[n++] = resp.Args; if (returnTypeTag == ReturnTypeTag.ObjectType) infoArray[n++] = resp.ReturnValue; if (contextFlag == MethodFlags.IncludesLogicalCallContext) infoArray[n++] = resp.LogicalCallContext; if (extraProperties != null) infoArray[n++] = extraProperties; info = infoArray; } else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0) info = resp.Args; if (info != null) { ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat); objectWriter.WriteObjectGraph (writer, info, headers); } else writer.Write ((byte) BinaryElement.End); }
public void Serialize(Stream serializationStream, object graph, Header[] headers) { if(serializationStream==null) { throw new ArgumentNullException ("serializationStream"); } BinaryWriter writer = new BinaryWriter (serializationStream); WriteBinaryHeader (writer, headers!=null); if (graph is IMethodCallMessage) { MessageFormatter.WriteMethodCall (writer, graph, headers, this); } else if (graph is IMethodReturnMessage) { MessageFormatter.WriteMethodResponse (writer, graph, headers, this); } else { ObjectWriter serializer = new ObjectWriter (this); serializer.WriteObjectGraph (writer, graph, headers); } writer.Flush(); }
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); } }