In System:
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / MessageFormatter.cs
1 //
2 // System.Runtime.Remoting.MessageFormatter.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2003, Lluis Sanchez Gual
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.IO;
34 using System.Reflection;
35 using System.Collections;
36 using System.Runtime.Remoting;
37 using System.Runtime.Serialization;
38 using System.Runtime.Remoting.Messaging;
39
40 namespace System.Runtime.Serialization.Formatters.Binary
41 {
42         internal class MessageFormatter
43         {
44                 public static void WriteMethodCall (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
45                 {
46                         IMethodCallMessage call = (IMethodCallMessage)obj;
47                         writer.Write ((byte) BinaryElement.MethodCall);
48
49                         MethodFlags methodFlags;
50                         int infoArraySize = 0;
51                         object info = null;
52                         object[] extraProperties = null;
53
54                         if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo)
55                         {
56                                 methodFlags = MethodFlags.IncludesLogicalCallContext;
57                                 infoArraySize++;
58                         }
59                         else
60                                 methodFlags = MethodFlags.ExcludeLogicalCallContext;
61
62                         if (RemotingServices.IsMethodOverloaded (call))
63                         {
64                                 infoArraySize++;
65                                 methodFlags |= MethodFlags.IncludesSignature;
66                         }
67
68                         if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length)
69                         {
70                                 extraProperties = GetExtraProperties (call.Properties, MethodCallDictionary.InternalKeys);
71                                 infoArraySize++;
72                         }
73
74 #if NET_2_0
75                         if (call.MethodBase.IsGenericMethod) {
76                                 infoArraySize++;
77                                 methodFlags |= MethodFlags.GenericArguments;
78                         }
79 #endif
80                         if (call.ArgCount == 0)
81                                 methodFlags |= MethodFlags.NoArguments;
82                         else {
83                                 if (AllTypesArePrimitive (call.Args)) 
84                                         methodFlags |= MethodFlags.PrimitiveArguments;
85                                 else {
86                                         if (infoArraySize == 0)
87                                                 methodFlags |= MethodFlags.ArgumentsInSimpleArray;
88                                         else {
89                                                 methodFlags |= MethodFlags.ArgumentsInMultiArray;
90                                                 infoArraySize++;
91                                         }
92                                 }
93                         }
94
95                         writer.Write ((int) methodFlags);
96
97                         // Method name
98                         writer.Write ((byte) BinaryTypeCode.String);
99                         writer.Write (call.MethodName);
100
101                         // Class name
102                         writer.Write ((byte) BinaryTypeCode.String);
103                         writer.Write (call.TypeName);
104
105                         // Arguments
106
107                         if ((methodFlags & MethodFlags.PrimitiveArguments) > 0)
108                         {
109                                 writer.Write ((uint)call.Args.Length);
110                                 for (int n=0; n<call.ArgCount; n++)
111                                 {
112                                         object arg = call.GetArg(n);
113                                         if (arg != null) {
114                                                 writer.Write (BinaryCommon.GetTypeCode (arg.GetType()));
115                                                 ObjectWriter.WritePrimitiveValue (writer, arg);
116                                         }
117                                         else
118                                                 writer.Write ((byte)BinaryTypeCode.Null);
119                                 }
120                         }
121
122                         if ( infoArraySize > 0)
123                         {
124                                 object[] ainfo = new object[infoArraySize];
125                                 int n=0;
126                                 if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0) ainfo[n++] = call.Args;
127
128 #if NET_2_0
129                                 if ((methodFlags & MethodFlags.GenericArguments) > 0) ainfo[n++] = call.MethodBase.GetGenericArguments ();
130 #endif
131
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;
135                                 info = ainfo;
136                         }
137                         else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0)
138                                 info = call.Args;
139
140                         if (info != null)
141                         {
142                                 ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat);
143                                 objectWriter.WriteObjectGraph (writer, info, headers);
144                         }
145                         else
146                                 writer.Write ((byte) BinaryElement.End);
147                 }
148
149                 public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
150                 {
151                         IMethodReturnMessage resp = (IMethodReturnMessage)obj;
152                         writer.Write ((byte) BinaryElement.MethodResponse);
153
154                         string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;
155
156                         int infoArrayLength = 0;
157                         object info = null;
158                         object[] extraProperties = null;
159
160                         // Type of return value
161
162                         ReturnTypeTag returnTypeTag;
163                         MethodFlags contextFlag = MethodFlags.ExcludeLogicalCallContext;
164
165                         if (resp.Exception != null) {
166                                 returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null;
167                                 internalProperties = MethodReturnDictionary.InternalExceptionKeys;
168                                 infoArrayLength = 1;
169                         }
170                         else if (resp.ReturnValue == null) {
171                                 returnTypeTag = ReturnTypeTag.Null;
172                         }
173                         else if (IsMethodPrimitive(resp.ReturnValue.GetType())) {
174                                 returnTypeTag = ReturnTypeTag.PrimitiveType;
175                         }
176                         else {
177                                 returnTypeTag = ReturnTypeTag.ObjectType;
178                                 infoArrayLength++;
179                         }
180
181                         // Message flags
182
183                         MethodFlags formatFlag;
184
185                         if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo) 
186                         {
187                                 contextFlag = MethodFlags.IncludesLogicalCallContext;
188                                 infoArrayLength++;
189                         }
190
191                         if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
192                         {
193                                 extraProperties = GetExtraProperties (resp.Properties, internalProperties);
194                                 infoArrayLength++;
195                         }
196
197                         if (resp.OutArgCount == 0)
198                                 formatFlag = MethodFlags.NoArguments;
199                         else 
200                         {
201                                 if (AllTypesArePrimitive (resp.Args)) 
202                                         formatFlag = MethodFlags.PrimitiveArguments;
203                                 else 
204                                 {
205                                         if (infoArrayLength == 0)
206                                                 formatFlag = MethodFlags.ArgumentsInSimpleArray; 
207                                         else {
208                                                 formatFlag = MethodFlags.ArgumentsInMultiArray;
209                                                 infoArrayLength++;
210                                         }
211                                 }
212                         }
213
214                         writer.Write ((byte) (contextFlag | formatFlag));
215                         writer.Write ((byte) returnTypeTag);
216
217                         // FIXME: what are the following 2 bytes for?
218                         writer.Write ((byte) 0);
219                         writer.Write ((byte) 0);
220
221                         // Arguments
222
223                         if (returnTypeTag == ReturnTypeTag.PrimitiveType)
224                         {
225                                 writer.Write (BinaryCommon.GetTypeCode (resp.ReturnValue.GetType()));
226                                 ObjectWriter.WritePrimitiveValue (writer, resp.ReturnValue);
227                         }
228
229                         if (formatFlag == MethodFlags.PrimitiveArguments)
230                         {
231                                 writer.Write ((uint)resp.ArgCount);
232                                 for (int n=0; n<resp.ArgCount; n++)
233                                 {
234                                         object val = resp.GetArg(n);
235                                         if (val != null) {
236                                                 writer.Write (BinaryCommon.GetTypeCode (val.GetType()));
237                                                 ObjectWriter.WritePrimitiveValue (writer, val);
238                                         }
239                                         else
240                                                 writer.Write ((byte)BinaryTypeCode.Null);
241                                 }
242                         }
243
244                         if (infoArrayLength > 0)
245                         {
246                                 object[] infoArray = new object[infoArrayLength];
247                                 int n = 0;
248
249                                 if ((returnTypeTag & ReturnTypeTag.Exception) != 0)
250                                         infoArray[n++] = resp.Exception;
251                                 
252                                 if (formatFlag == MethodFlags.ArgumentsInMultiArray)
253                                         infoArray[n++] = resp.Args;
254
255                                 if (returnTypeTag == ReturnTypeTag.ObjectType)
256                                         infoArray[n++] = resp.ReturnValue;
257
258                                 if (contextFlag == MethodFlags.IncludesLogicalCallContext)
259                                         infoArray[n++] = resp.LogicalCallContext;
260
261                                 if (extraProperties != null)
262                                         infoArray[n++] = extraProperties;
263
264                                 info = infoArray;
265                         }
266                         else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
267                                 info = resp.Args;
268
269                         if (info != null)
270                         {
271                                 ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat);
272                                 objectWriter.WriteObjectGraph (writer, info, headers);
273                         }
274                         else
275                                 writer.Write ((byte) BinaryElement.End);
276                 }
277
278                 public static object ReadMethodCall (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
279                 {
280                         BinaryElement elem = (BinaryElement)reader.ReadByte();  // The element code
281                         if (elem != BinaryElement.MethodCall) throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " +  elem);
282
283                         MethodFlags flags = (MethodFlags) reader.ReadInt32();
284
285                         if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
286                         string methodName = reader.ReadString();
287
288                         if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
289                         string className = reader.ReadString();
290
291                         //bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
292
293                         object[] arguments = null;
294                         object methodSignature = null;
295                         object callContext = null;
296                         object[] extraProperties = null;
297                         Header[] headers = null;
298 #if NET_2_0
299                         Type[] genericArguments = null;
300 #endif
301
302                         if ((flags & MethodFlags.PrimitiveArguments) > 0)
303                         {
304                                 uint count = reader.ReadUInt32();
305                                 arguments = new object[count];
306                                 for (int n=0; n<count; n++)
307                                 {
308                                         Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
309                                         arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
310                                 }
311                         }
312
313                         if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
314                         {
315                                 ObjectReader objectReader = new ObjectReader (formatter);
316
317                                 object result;
318                                 objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
319                                 object[] msgInfo = (object[]) result;
320
321                                 if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
322                                         arguments = msgInfo;
323                                 }
324                                 else
325                                 {
326                                         int n = 0;
327                                         if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
328                                                 if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
329                                                 else arguments = new object[0];
330                                         }
331
332 #if NET_2_0
333                                         if ((flags & MethodFlags.GenericArguments) > 0)
334                                                 genericArguments = (Type[]) msgInfo[n++];
335 #endif
336
337                                         if ((flags & MethodFlags.IncludesSignature) > 0)
338                                                 methodSignature = msgInfo[n++];
339
340                                         if ((flags & MethodFlags.IncludesLogicalCallContext) > 0) 
341                                                 callContext = msgInfo[n++];
342
343                                         if (n < msgInfo.Length)
344                                                 extraProperties = (object[]) msgInfo[n];
345                                 }
346                         }
347                         else {
348                                 reader.ReadByte ();     // Reads the stream ender
349                         }
350
351                         if (arguments == null) arguments = new object[0];
352
353                         string uri = null;
354                         if (headerHandler != null)
355                                 uri = headerHandler(headers) as string;
356
357 #if NET_2_0
358                         Header[] methodInfo = new Header[7];
359 #else
360                         Header[] methodInfo = new Header[6];
361 #endif
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);
368 #if NET_2_0
369                         methodInfo[6] = new Header("__GenericArguments", genericArguments);
370 #endif
371
372                         MethodCall call = new MethodCall (methodInfo);
373
374                         if (extraProperties != null) {
375                                 foreach (DictionaryEntry entry in extraProperties)
376                                         call.Properties [(string)entry.Key] = entry.Value;
377                         }
378
379                         return call;
380                 }
381
382                 public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
383                 {
384                         BinaryElement elem = (BinaryElement)reader.ReadByte();  // The element code
385                         if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " +  elem);
386
387                         MethodFlags flags = (MethodFlags) reader.ReadByte ();
388                         ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
389                         bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
390
391                         // FIXME: find a meaning for those 2 bytes
392                         reader.ReadByte();
393                         reader.ReadByte();
394
395                         object returnValue = null;
396                         object[] outArgs = null;
397                         LogicalCallContext callContext = null;
398                         Exception exception = null;
399                         object[] extraProperties = null;
400                         Header[] headers = null;
401
402                         if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
403                         {
404                                 Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
405                                 returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
406                         }
407
408                         if ((flags & MethodFlags.PrimitiveArguments) > 0)
409                         {
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);
415                                 }
416                         }
417
418                         if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 || 
419                                 (typeTag & ReturnTypeTag.Exception) > 0 ||
420                                 (flags & MethodFlags.ArgumentsInSimpleArray) > 0 || 
421                                 (flags & MethodFlags.ArgumentsInMultiArray) > 0)
422                         {
423                                 // There objects that need to be deserialized using an ObjectReader
424
425                                 ObjectReader objectReader = new ObjectReader (formatter);
426                                 object result;
427                                 objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
428                                 object[] msgInfo = (object[]) result;
429
430                                 if ((typeTag & ReturnTypeTag.Exception) > 0) {
431                                         exception = (Exception) msgInfo[0];
432                                         if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[1];
433                                 }
434                                 else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
435                                         int n = 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];
439                                 }
440                                 else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
441                                         outArgs = msgInfo;
442                                 }
443                                 else {
444                                         int n = 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];
449                                 }
450                         }
451                         else {
452                                 reader.ReadByte ();     // Reads the stream ender
453                         }
454
455                         if (headerHandler != null) 
456                                 headerHandler(headers);
457
458                         if (exception != null)
459                                 return new ReturnMessage (exception, methodCallMessage);
460                         else
461                         {
462                                 int argCount = (outArgs!=null) ? outArgs.Length : 0;
463                                 ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
464
465                                 if (extraProperties != null) {
466                                         foreach (DictionaryEntry entry in extraProperties)
467                                                 result.Properties [(string)entry.Key] = entry.Value;
468                                 }
469
470                                 return result;
471                         }
472                 }
473
474                 private static bool AllTypesArePrimitive(object[] objects)
475                 {
476                         foreach (object ob in objects) 
477                         {
478                                 if (ob != null && !IsMethodPrimitive(ob.GetType())) 
479                                         return false;
480                         }
481                         return true;
482                 }
483
484                 // When serializing methods, string are considered primitive types
485                 public static bool IsMethodPrimitive (Type type)
486                 {
487                         return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
488                 }
489
490                 static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
491                 {
492                         object[] extraProperties = new object [properties.Count - internalKeys.Length];
493                         
494                         int n = 0;
495                         IDictionaryEnumerator e = properties.GetEnumerator();
496                         while (e.MoveNext())
497                                 if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
498
499                         return extraProperties;
500                 }
501
502                 static bool IsInternalKey (string key, string[] internalKeys)
503                 {
504                         foreach (string ikey in internalKeys)
505                                 if (key == ikey) return true;
506                         return false;
507                 }
508
509         }
510 }