| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- //
- // System.Runtime.Remoting.MessageFormatter.cs
- //
- // Author: Lluis Sanchez Gual ([email protected])
- //
- // (C) 2003, Lluis Sanchez Gual
- //
- using System;
- using System.IO;
- using System.Reflection;
- using System.Collections;
- using System.Runtime.Remoting;
- using System.Runtime.Serialization;
- using System.Runtime.Remoting.Messaging;
- namespace System.Runtime.Serialization.Formatters.Binary
- {
- internal class MessageFormatter
- {
- public static void WriteMethodCall (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context)
- {
- 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.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 ((byte) (methodFlags));
- // FIXME: what are the following 3 bytes for?
- writer.Write ((byte) 0);
- writer.Write ((byte) 0);
- writer.Write ((byte) 0);
- // 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.InArgCount; 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.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);
- 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)
- {
- 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;
- if (resp.Exception != null) {
- returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null;
- info = new object[] {resp.Exception};
- internalProperties = MethodReturnDictionary.InternalExceptionKeys;
- }
- 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 contextFlag;
- MethodFlags formatFlag;
- if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
- {
- contextFlag = MethodFlags.IncludesLogicalCallContext;
- infoArrayLength++;
- }
- else
- contextFlag = MethodFlags.ExcludeLogicalCallContext;
- 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.OutArgs))
- 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.OutArgCount);
- for (int n=0; n<resp.OutArgCount; n++)
- {
- object val = resp.GetOutArg(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 (formatFlag == MethodFlags.ArgumentsInMultiArray)
- infoArray[n++] = resp.OutArgs;
- 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.OutArgs;
- if (info != null)
- {
- ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context);
- objectWriter.WriteObjectGraph (writer, info, headers);
- }
- else
- writer.Write ((byte) BinaryElement.End);
- }
- public static object ReadMethodCall (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, ISurrogateSelector surrogateSelector, StreamingContext context)
- {
- BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
- if (elem != BinaryElement.MethodCall) throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
- MethodFlags flags = (MethodFlags) reader.ReadByte();
- // FIXME: find a meaning for those 3 bytes
- reader.ReadByte();
- reader.ReadByte();
- reader.ReadByte();
- if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
- string methodName = reader.ReadString();
- if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
- string className = reader.ReadString();
- bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
- object[] arguments = null;
- object methodSignature = null;
- object callContext = null;
- object[] extraProperties = null;
- if ((flags & MethodFlags.PrimitiveArguments) > 0)
- {
- uint count = reader.ReadUInt32();
- arguments = new object[count];
- for (int n=0; n<count; n++)
- {
- Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
- arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
- }
- }
- if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
- {
- ObjectReader objectReader = new ObjectReader(surrogateSelector, context);
- object[] msgInfo = (object[]) objectReader.ReadObjectGraph (reader, hasHeaders, headerHandler);
- if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
- arguments = msgInfo;
- }
- else
- {
- int n = 0;
- if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
- if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
- else arguments = new object[0];
- }
- if ((flags & MethodFlags.IncludesSignature) > 0)
- methodSignature = msgInfo[n++];
- if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
- callContext = msgInfo[n++];
- if (n < msgInfo.Length)
- extraProperties = (object[]) msgInfo[n];
- }
- }
- else {
- reader.ReadByte (); // Reads the stream ender
- }
- if (arguments == null) arguments = new object[0];
- Header[] methodInfo = new Header[5];
- methodInfo[0] = new Header("__MethodName", methodName);
- methodInfo[1] = new Header("__MethodSignature", methodSignature);
- methodInfo[2] = new Header("__TypeName", className);
- methodInfo[3] = new Header("__Args", arguments);
- methodInfo[4] = new Header("__CallContext", callContext);
- MethodCall call = new MethodCall (methodInfo);
- if (extraProperties != null) {
- foreach (DictionaryEntry entry in extraProperties)
- call.Properties [(string)entry.Key] = entry.Value;
- }
- return call;
- }
- public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, ISurrogateSelector surrogateSelector, StreamingContext context)
- {
- BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
- if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
- MethodFlags flags = (MethodFlags) reader.ReadByte ();
- ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
- bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
- // FIXME: find a meaning for those 2 bytes
- reader.ReadByte();
- reader.ReadByte();
- object returnValue = null;
- object[] outArgs = null;
- LogicalCallContext callContext = null;
- Exception exception = null;
- object[] extraProperties = null;
- if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
- {
- Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
- returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
- }
- if ((flags & MethodFlags.PrimitiveArguments) > 0)
- {
- uint count = reader.ReadUInt32();
- outArgs = new object[count];
- for (int n=0; n<count; n++) {
- Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
- outArgs[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
- }
- }
- if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
- (typeTag & ReturnTypeTag.Exception) > 0 ||
- (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
- (flags & MethodFlags.ArgumentsInMultiArray) > 0)
- {
- // There objects that need to be deserialized using an ObjectReader
- ObjectReader objectReader = new ObjectReader(surrogateSelector, context);
- object[] msgInfo = (object[]) objectReader.ReadObjectGraph (reader, hasHeaders, headerHandler);
- if ((typeTag & ReturnTypeTag.Exception) > 0) {
- exception = (Exception) msgInfo[0];
- }
- else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
- int n = 0;
- if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo [n++];
- if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
- if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
- }
- else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
- outArgs = msgInfo;
- }
- else {
- int n = 0;
- outArgs = (object[]) msgInfo[n++];
- if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo[n++];
- if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
- if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
- }
- }
- else {
- reader.ReadByte (); // Reads the stream ender
- }
- if (exception != null)
- return new ReturnMessage (exception, methodCallMessage);
- else
- {
- int argCount = (outArgs!=null) ? outArgs.Length : 0;
- ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
- if (extraProperties != null) {
- foreach (DictionaryEntry entry in extraProperties)
- result.Properties [(string)entry.Key] = entry.Value;
- }
- return result;
- }
- }
- private static bool AllTypesArePrimitive(object[] objects)
- {
- foreach (object ob in objects)
- {
- if (ob != null && !IsMethodPrimitive(ob.GetType()))
- return false;
- }
- return true;
- }
- // When serializing methods, string are considered primitive types
- public static bool IsMethodPrimitive (Type type)
- {
- return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
- }
- static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
- {
- object[] extraProperties = new object [properties.Count - internalKeys.Length];
-
- int n = 0;
- IDictionaryEnumerator e = properties.GetEnumerator();
- while (e.MoveNext())
- if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
- return extraProperties;
- }
- static bool IsInternalKey (string key, string[] internalKeys)
- {
- foreach (string ikey in internalKeys)
- if (key == ikey) return true;
- return false;
- }
- }
- }
|