2 // System.Runtime.Remoting.MessageFormatter.cs
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
6 // (C) 2003, Lluis Sanchez Gual
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Reflection;
35 using System.Collections;
36 using System.Runtime.Remoting;
37 using System.Runtime.Serialization;
38 using System.Runtime.Remoting.Messaging;
40 namespace System.Runtime.Serialization.Formatters.Binary
42 internal class MessageFormatter
44 public static void WriteMethodCall (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
46 IMethodCallMessage call = (IMethodCallMessage)obj;
47 writer.Write ((byte) BinaryElement.MethodCall);
49 MethodFlags methodFlags;
50 int infoArraySize = 0;
52 object[] extraProperties = null;
54 if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo)
56 methodFlags = MethodFlags.IncludesLogicalCallContext;
60 methodFlags = MethodFlags.ExcludeLogicalCallContext;
62 if (RemotingServices.IsMethodOverloaded (call))
65 methodFlags |= MethodFlags.IncludesSignature;
68 if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length)
70 extraProperties = GetExtraProperties (call.Properties, MethodCallDictionary.InternalKeys);
75 if (call.MethodBase.IsGenericMethod) {
77 methodFlags |= MethodFlags.GenericArguments;
80 if (call.ArgCount == 0)
81 methodFlags |= MethodFlags.NoArguments;
83 if (AllTypesArePrimitive (call.Args))
84 methodFlags |= MethodFlags.PrimitiveArguments;
86 if (infoArraySize == 0)
87 methodFlags |= MethodFlags.ArgumentsInSimpleArray;
89 methodFlags |= MethodFlags.ArgumentsInMultiArray;
95 writer.Write ((int) methodFlags);
98 writer.Write ((byte) BinaryTypeCode.String);
99 writer.Write (call.MethodName);
102 writer.Write ((byte) BinaryTypeCode.String);
103 writer.Write (call.TypeName);
107 if ((methodFlags & MethodFlags.PrimitiveArguments) > 0)
109 writer.Write ((uint)call.Args.Length);
110 for (int n=0; n<call.ArgCount; n++)
112 object arg = call.GetArg(n);
114 writer.Write (BinaryCommon.GetTypeCode (arg.GetType()));
115 ObjectWriter.WritePrimitiveValue (writer, arg);
118 writer.Write ((byte)BinaryTypeCode.Null);
122 if ( infoArraySize > 0)
124 object[] ainfo = new object[infoArraySize];
126 if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0) ainfo[n++] = call.Args;
129 if ((methodFlags & MethodFlags.GenericArguments) > 0) ainfo[n++] = call.MethodBase.GetGenericArguments ();
132 if ((methodFlags & MethodFlags.IncludesSignature) > 0) ainfo[n++] = call.MethodSignature;
133 if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0) ainfo[n++] = call.LogicalCallContext;
134 if (extraProperties != null) ainfo[n++] = extraProperties;
137 else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0)
142 ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat);
143 objectWriter.WriteObjectGraph (writer, info, headers);
146 writer.Write ((byte) BinaryElement.End);
149 public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
151 IMethodReturnMessage resp = (IMethodReturnMessage)obj;
152 writer.Write ((byte) BinaryElement.MethodResponse);
154 string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;
156 int infoArrayLength = 0;
158 object[] extraProperties = null;
160 // Type of return value
162 ReturnTypeTag returnTypeTag;
163 MethodFlags contextFlag = MethodFlags.ExcludeLogicalCallContext;
165 if (resp.Exception != null) {
166 returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null;
167 internalProperties = MethodReturnDictionary.InternalExceptionKeys;
170 else if (resp.ReturnValue == null) {
171 returnTypeTag = ReturnTypeTag.Null;
173 else if (IsMethodPrimitive(resp.ReturnValue.GetType())) {
174 returnTypeTag = ReturnTypeTag.PrimitiveType;
177 returnTypeTag = ReturnTypeTag.ObjectType;
183 MethodFlags formatFlag;
185 if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo)
187 contextFlag = MethodFlags.IncludesLogicalCallContext;
191 if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
193 extraProperties = GetExtraProperties (resp.Properties, internalProperties);
197 if (resp.OutArgCount == 0)
198 formatFlag = MethodFlags.NoArguments;
201 if (AllTypesArePrimitive (resp.Args))
202 formatFlag = MethodFlags.PrimitiveArguments;
205 if (infoArrayLength == 0)
206 formatFlag = MethodFlags.ArgumentsInSimpleArray;
208 formatFlag = MethodFlags.ArgumentsInMultiArray;
214 writer.Write ((byte) (contextFlag | formatFlag));
215 writer.Write ((byte) returnTypeTag);
217 // FIXME: what are the following 2 bytes for?
218 writer.Write ((byte) 0);
219 writer.Write ((byte) 0);
223 if (returnTypeTag == ReturnTypeTag.PrimitiveType)
225 writer.Write (BinaryCommon.GetTypeCode (resp.ReturnValue.GetType()));
226 ObjectWriter.WritePrimitiveValue (writer, resp.ReturnValue);
229 if (formatFlag == MethodFlags.PrimitiveArguments)
231 writer.Write ((uint)resp.ArgCount);
232 for (int n=0; n<resp.ArgCount; n++)
234 object val = resp.GetArg(n);
236 writer.Write (BinaryCommon.GetTypeCode (val.GetType()));
237 ObjectWriter.WritePrimitiveValue (writer, val);
240 writer.Write ((byte)BinaryTypeCode.Null);
244 if (infoArrayLength > 0)
246 object[] infoArray = new object[infoArrayLength];
249 if ((returnTypeTag & ReturnTypeTag.Exception) != 0)
250 infoArray[n++] = resp.Exception;
252 if (formatFlag == MethodFlags.ArgumentsInMultiArray)
253 infoArray[n++] = resp.Args;
255 if (returnTypeTag == ReturnTypeTag.ObjectType)
256 infoArray[n++] = resp.ReturnValue;
258 if (contextFlag == MethodFlags.IncludesLogicalCallContext)
259 infoArray[n++] = resp.LogicalCallContext;
261 if (extraProperties != null)
262 infoArray[n++] = extraProperties;
266 else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
271 ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat);
272 objectWriter.WriteObjectGraph (writer, info, headers);
275 writer.Write ((byte) BinaryElement.End);
278 public static object ReadMethodCall (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
280 BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
281 if (elem != BinaryElement.MethodCall) throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
283 MethodFlags flags = (MethodFlags) reader.ReadInt32();
285 if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
286 string methodName = reader.ReadString();
288 if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
289 string className = reader.ReadString();
291 //bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
293 object[] arguments = null;
294 object methodSignature = null;
295 object callContext = null;
296 object[] extraProperties = null;
297 Header[] headers = null;
299 Type[] genericArguments = null;
302 if ((flags & MethodFlags.PrimitiveArguments) > 0)
304 uint count = reader.ReadUInt32();
305 arguments = new object[count];
306 for (int n=0; n<count; n++)
308 Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
309 arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
313 if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
315 ObjectReader objectReader = new ObjectReader (formatter);
318 objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
319 object[] msgInfo = (object[]) result;
321 if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
327 if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
328 if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
329 else arguments = new object[0];
333 if ((flags & MethodFlags.GenericArguments) > 0)
334 genericArguments = (Type[]) msgInfo[n++];
337 if ((flags & MethodFlags.IncludesSignature) > 0)
338 methodSignature = msgInfo[n++];
340 if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
341 callContext = msgInfo[n++];
343 if (n < msgInfo.Length)
344 extraProperties = (object[]) msgInfo[n];
348 reader.ReadByte (); // Reads the stream ender
351 if (arguments == null) arguments = new object[0];
354 if (headerHandler != null)
355 uri = headerHandler(headers) as string;
358 Header[] methodInfo = new Header[7];
360 Header[] methodInfo = new Header[6];
362 methodInfo[0] = new Header("__MethodName", methodName);
363 methodInfo[1] = new Header("__MethodSignature", methodSignature);
364 methodInfo[2] = new Header("__TypeName", className);
365 methodInfo[3] = new Header("__Args", arguments);
366 methodInfo[4] = new Header("__CallContext", callContext);
367 methodInfo[5] = new Header("__Uri", uri);
369 methodInfo[6] = new Header("__GenericArguments", genericArguments);
372 MethodCall call = new MethodCall (methodInfo);
374 if (extraProperties != null) {
375 foreach (DictionaryEntry entry in extraProperties)
376 call.Properties [(string)entry.Key] = entry.Value;
382 public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
384 BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
385 if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
387 MethodFlags flags = (MethodFlags) reader.ReadByte ();
388 ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
389 bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
391 // FIXME: find a meaning for those 2 bytes
395 object returnValue = null;
396 object[] outArgs = null;
397 LogicalCallContext callContext = null;
398 Exception exception = null;
399 object[] extraProperties = null;
400 Header[] headers = null;
402 if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
404 Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
405 returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
408 if ((flags & MethodFlags.PrimitiveArguments) > 0)
410 uint count = reader.ReadUInt32();
411 outArgs = new object[count];
412 for (int n=0; n<count; n++) {
413 Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
414 outArgs[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
418 if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
419 (typeTag & ReturnTypeTag.Exception) > 0 ||
420 (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
421 (flags & MethodFlags.ArgumentsInMultiArray) > 0)
423 // There objects that need to be deserialized using an ObjectReader
425 ObjectReader objectReader = new ObjectReader (formatter);
427 objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
428 object[] msgInfo = (object[]) result;
430 if ((typeTag & ReturnTypeTag.Exception) > 0) {
431 exception = (Exception) msgInfo[0];
432 if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[1];
434 else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
436 if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo [n++];
437 if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
438 if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
440 else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
445 outArgs = (object[]) msgInfo[n++];
446 if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo[n++];
447 if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
448 if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
452 reader.ReadByte (); // Reads the stream ender
455 if (headerHandler != null)
456 headerHandler(headers);
458 if (exception != null)
459 return new ReturnMessage (exception, methodCallMessage);
462 int argCount = (outArgs!=null) ? outArgs.Length : 0;
463 ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
465 if (extraProperties != null) {
466 foreach (DictionaryEntry entry in extraProperties)
467 result.Properties [(string)entry.Key] = entry.Value;
474 private static bool AllTypesArePrimitive(object[] objects)
476 foreach (object ob in objects)
478 if (ob != null && !IsMethodPrimitive(ob.GetType()))
484 // When serializing methods, string are considered primitive types
485 public static bool IsMethodPrimitive (Type type)
487 return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
490 static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
492 object[] extraProperties = new object [properties.Count - internalKeys.Length];
495 IDictionaryEnumerator e = properties.GetEnumerator();
497 if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
499 return extraProperties;
502 static bool IsInternalKey (string key, string[] internalKeys)
504 foreach (string ikey in internalKeys)
505 if (key == ikey) return true;