MessageFormatter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. //
  2. // System.Runtime.Remoting.MessageFormatter.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // (C) 2003, Lluis Sanchez Gual
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Collections;
  12. using System.Runtime.Remoting;
  13. using System.Runtime.Serialization;
  14. using System.Runtime.Remoting.Messaging;
  15. namespace System.Runtime.Serialization.Formatters.Binary
  16. {
  17. internal class MessageFormatter
  18. {
  19. public static void WriteMethodCall (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context)
  20. {
  21. IMethodCallMessage call = (IMethodCallMessage)obj;
  22. writer.Write ((byte) BinaryElement.MethodCall);
  23. MethodFlags methodFlags;
  24. int infoArraySize = 0;
  25. object info = null;
  26. object[] extraProperties = null;
  27. if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo)
  28. {
  29. methodFlags = MethodFlags.IncludesLogicalCallContext;
  30. infoArraySize++;
  31. }
  32. else
  33. methodFlags = MethodFlags.ExcludeLogicalCallContext;
  34. if (RemotingServices.IsMethodOverloaded (call))
  35. {
  36. infoArraySize++;
  37. methodFlags |= MethodFlags.IncludesSignature;
  38. }
  39. if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length)
  40. {
  41. extraProperties = GetExtraProperties (call.Properties, MethodCallDictionary.InternalKeys);
  42. infoArraySize++;
  43. }
  44. if (call.ArgCount == 0)
  45. methodFlags |= MethodFlags.NoArguments;
  46. else {
  47. if (AllTypesArePrimitive (call.Args))
  48. methodFlags |= MethodFlags.PrimitiveArguments;
  49. else {
  50. if (infoArraySize == 0)
  51. methodFlags |= MethodFlags.ArgumentsInSimpleArray;
  52. else {
  53. methodFlags |= MethodFlags.ArgumentsInMultiArray;
  54. infoArraySize++;
  55. }
  56. }
  57. }
  58. writer.Write ((byte) (methodFlags));
  59. // FIXME: what are the following 3 bytes for?
  60. writer.Write ((byte) 0);
  61. writer.Write ((byte) 0);
  62. writer.Write ((byte) 0);
  63. // Method name
  64. writer.Write ((byte) BinaryTypeCode.String);
  65. writer.Write (call.MethodName);
  66. // Class name
  67. writer.Write ((byte) BinaryTypeCode.String);
  68. writer.Write (call.TypeName);
  69. // Arguments
  70. if ((methodFlags & MethodFlags.PrimitiveArguments) > 0)
  71. {
  72. writer.Write ((uint)call.Args.Length);
  73. for (int n=0; n<call.InArgCount; n++)
  74. {
  75. object arg = call.GetArg(n);
  76. if (arg != null) {
  77. writer.Write (BinaryCommon.GetTypeCode (arg.GetType()));
  78. ObjectWriter.WritePrimitiveValue (writer, arg);
  79. }
  80. else
  81. writer.Write ((byte)BinaryTypeCode.Null);
  82. }
  83. }
  84. if ( infoArraySize > 0)
  85. {
  86. object[] ainfo = new object[infoArraySize];
  87. int n=0;
  88. if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0) ainfo[n++] = call.Args;
  89. if ((methodFlags & MethodFlags.IncludesSignature) > 0) ainfo[n++] = call.MethodSignature;
  90. if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0) ainfo[n++] = call.LogicalCallContext;
  91. if (extraProperties != null) ainfo[n++] = extraProperties;
  92. info = ainfo;
  93. }
  94. else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0)
  95. info = call.Args;
  96. if (info != null)
  97. {
  98. ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context);
  99. objectWriter.WriteObjectGraph (writer, info, headers);
  100. }
  101. else
  102. writer.Write ((byte) BinaryElement.End);
  103. }
  104. public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context)
  105. {
  106. IMethodReturnMessage resp = (IMethodReturnMessage)obj;
  107. writer.Write ((byte) BinaryElement.MethodResponse);
  108. string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;
  109. int infoArrayLength = 0;
  110. object info = null;
  111. object[] extraProperties = null;
  112. // Type of return value
  113. ReturnTypeTag returnTypeTag;
  114. if (resp.Exception != null) {
  115. returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null;
  116. info = new object[] {resp.Exception};
  117. internalProperties = MethodReturnDictionary.InternalExceptionKeys;
  118. }
  119. else if (resp.ReturnValue == null) {
  120. returnTypeTag = ReturnTypeTag.Null;
  121. }
  122. else if (IsMethodPrimitive(resp.ReturnValue.GetType())) {
  123. returnTypeTag = ReturnTypeTag.PrimitiveType;
  124. }
  125. else {
  126. returnTypeTag = ReturnTypeTag.ObjectType;
  127. infoArrayLength++;
  128. }
  129. // Message flags
  130. MethodFlags contextFlag;
  131. MethodFlags formatFlag;
  132. if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
  133. {
  134. contextFlag = MethodFlags.IncludesLogicalCallContext;
  135. infoArrayLength++;
  136. }
  137. else
  138. contextFlag = MethodFlags.ExcludeLogicalCallContext;
  139. if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
  140. {
  141. extraProperties = GetExtraProperties (resp.Properties, internalProperties);
  142. infoArrayLength++;
  143. }
  144. if (resp.OutArgCount == 0)
  145. formatFlag = MethodFlags.NoArguments;
  146. else
  147. {
  148. if (AllTypesArePrimitive (resp.OutArgs))
  149. formatFlag = MethodFlags.PrimitiveArguments;
  150. else
  151. {
  152. if (infoArrayLength == 0)
  153. formatFlag = MethodFlags.ArgumentsInSimpleArray;
  154. else {
  155. formatFlag = MethodFlags.ArgumentsInMultiArray;
  156. infoArrayLength++;
  157. }
  158. }
  159. }
  160. writer.Write ((byte) (contextFlag | formatFlag));
  161. writer.Write ((byte) returnTypeTag);
  162. // FIXME: what are the following 2 bytes for?
  163. writer.Write ((byte) 0);
  164. writer.Write ((byte) 0);
  165. // Arguments
  166. if (returnTypeTag == ReturnTypeTag.PrimitiveType)
  167. {
  168. writer.Write (BinaryCommon.GetTypeCode (resp.ReturnValue.GetType()));
  169. ObjectWriter.WritePrimitiveValue (writer, resp.ReturnValue);
  170. }
  171. if (formatFlag == MethodFlags.PrimitiveArguments)
  172. {
  173. writer.Write ((uint)resp.OutArgCount);
  174. for (int n=0; n<resp.OutArgCount; n++)
  175. {
  176. object val = resp.GetOutArg(n);
  177. if (val != null) {
  178. writer.Write (BinaryCommon.GetTypeCode (val.GetType()));
  179. ObjectWriter.WritePrimitiveValue (writer, val);
  180. }
  181. else
  182. writer.Write ((byte)BinaryTypeCode.Null);
  183. }
  184. }
  185. if (infoArrayLength > 0)
  186. {
  187. object[] infoArray = new object[infoArrayLength];
  188. int n = 0;
  189. if (formatFlag == MethodFlags.ArgumentsInMultiArray)
  190. infoArray[n++] = resp.OutArgs;
  191. if (returnTypeTag == ReturnTypeTag.ObjectType)
  192. infoArray[n++] = resp.ReturnValue;
  193. if (contextFlag == MethodFlags.IncludesLogicalCallContext)
  194. infoArray[n++] = resp.LogicalCallContext;
  195. if (extraProperties != null)
  196. infoArray[n++] = extraProperties;
  197. info = infoArray;
  198. }
  199. else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
  200. info = resp.OutArgs;
  201. if (info != null)
  202. {
  203. ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context);
  204. objectWriter.WriteObjectGraph (writer, info, headers);
  205. }
  206. else
  207. writer.Write ((byte) BinaryElement.End);
  208. }
  209. public static object ReadMethodCall (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, ISurrogateSelector surrogateSelector, StreamingContext context)
  210. {
  211. BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
  212. if (elem != BinaryElement.MethodCall) throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
  213. MethodFlags flags = (MethodFlags) reader.ReadByte();
  214. // FIXME: find a meaning for those 3 bytes
  215. reader.ReadByte();
  216. reader.ReadByte();
  217. reader.ReadByte();
  218. if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
  219. string methodName = reader.ReadString();
  220. if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
  221. string className = reader.ReadString();
  222. bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  223. object[] arguments = null;
  224. object methodSignature = null;
  225. object callContext = null;
  226. object[] extraProperties = null;
  227. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  228. {
  229. uint count = reader.ReadUInt32();
  230. arguments = new object[count];
  231. for (int n=0; n<count; n++)
  232. {
  233. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  234. arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  235. }
  236. }
  237. if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
  238. {
  239. ObjectReader objectReader = new ObjectReader(surrogateSelector, context);
  240. object[] msgInfo = (object[]) objectReader.ReadObjectGraph (reader, hasHeaders, headerHandler);
  241. if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  242. arguments = msgInfo;
  243. }
  244. else
  245. {
  246. int n = 0;
  247. if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
  248. if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
  249. else arguments = new object[0];
  250. }
  251. if ((flags & MethodFlags.IncludesSignature) > 0)
  252. methodSignature = msgInfo[n++];
  253. if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
  254. callContext = msgInfo[n++];
  255. if (n < msgInfo.Length)
  256. extraProperties = (object[]) msgInfo[n];
  257. }
  258. }
  259. else {
  260. reader.ReadByte (); // Reads the stream ender
  261. }
  262. if (arguments == null) arguments = new object[0];
  263. Header[] methodInfo = new Header[5];
  264. methodInfo[0] = new Header("__MethodName", methodName);
  265. methodInfo[1] = new Header("__MethodSignature", methodSignature);
  266. methodInfo[2] = new Header("__TypeName", className);
  267. methodInfo[3] = new Header("__Args", arguments);
  268. methodInfo[4] = new Header("__CallContext", callContext);
  269. MethodCall call = new MethodCall (methodInfo);
  270. if (extraProperties != null) {
  271. foreach (DictionaryEntry entry in extraProperties)
  272. call.Properties [(string)entry.Key] = entry.Value;
  273. }
  274. return call;
  275. }
  276. public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, ISurrogateSelector surrogateSelector, StreamingContext context)
  277. {
  278. BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
  279. if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
  280. MethodFlags flags = (MethodFlags) reader.ReadByte ();
  281. ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
  282. bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  283. // FIXME: find a meaning for those 2 bytes
  284. reader.ReadByte();
  285. reader.ReadByte();
  286. object returnValue = null;
  287. object[] outArgs = null;
  288. LogicalCallContext callContext = null;
  289. Exception exception = null;
  290. object[] extraProperties = null;
  291. if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
  292. {
  293. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  294. returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  295. }
  296. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  297. {
  298. uint count = reader.ReadUInt32();
  299. outArgs = new object[count];
  300. for (int n=0; n<count; n++) {
  301. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  302. outArgs[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  303. }
  304. }
  305. if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
  306. (typeTag & ReturnTypeTag.Exception) > 0 ||
  307. (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
  308. (flags & MethodFlags.ArgumentsInMultiArray) > 0)
  309. {
  310. // There objects that need to be deserialized using an ObjectReader
  311. ObjectReader objectReader = new ObjectReader(surrogateSelector, context);
  312. object[] msgInfo = (object[]) objectReader.ReadObjectGraph (reader, hasHeaders, headerHandler);
  313. if ((typeTag & ReturnTypeTag.Exception) > 0) {
  314. exception = (Exception) msgInfo[0];
  315. }
  316. else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
  317. int n = 0;
  318. if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo [n++];
  319. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
  320. if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
  321. }
  322. else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  323. outArgs = msgInfo;
  324. }
  325. else {
  326. int n = 0;
  327. outArgs = (object[]) msgInfo[n++];
  328. if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo[n++];
  329. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
  330. if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
  331. }
  332. }
  333. else {
  334. reader.ReadByte (); // Reads the stream ender
  335. }
  336. if (exception != null)
  337. return new ReturnMessage (exception, methodCallMessage);
  338. else
  339. {
  340. int argCount = (outArgs!=null) ? outArgs.Length : 0;
  341. ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
  342. if (extraProperties != null) {
  343. foreach (DictionaryEntry entry in extraProperties)
  344. result.Properties [(string)entry.Key] = entry.Value;
  345. }
  346. return result;
  347. }
  348. }
  349. private static bool AllTypesArePrimitive(object[] objects)
  350. {
  351. foreach (object ob in objects)
  352. {
  353. if (ob != null && !IsMethodPrimitive(ob.GetType()))
  354. return false;
  355. }
  356. return true;
  357. }
  358. // When serializing methods, string are considered primitive types
  359. public static bool IsMethodPrimitive (Type type)
  360. {
  361. return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
  362. }
  363. static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
  364. {
  365. object[] extraProperties = new object [properties.Count - internalKeys.Length];
  366. int n = 0;
  367. IDictionaryEnumerator e = properties.GetEnumerator();
  368. while (e.MoveNext())
  369. if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
  370. return extraProperties;
  371. }
  372. static bool IsInternalKey (string key, string[] internalKeys)
  373. {
  374. foreach (string ikey in internalKeys)
  375. if (key == ikey) return true;
  376. return false;
  377. }
  378. }
  379. }