PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/MessageFormatter.cs

https://bitbucket.org/danipen/mono
C# | 504 lines | 383 code | 81 blank | 40 comment | 140 complexity | 81c62ecdcae7472a888438a9fd621221 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Collections;
  34. using System.Runtime.Remoting;
  35. using System.Runtime.Serialization;
  36. using System.Runtime.Remoting.Messaging;
  37. namespace System.Runtime.Serialization.Formatters.Binary
  38. {
  39. internal class MessageFormatter
  40. {
  41. public static void WriteMethodCall (BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter)
  42. {
  43. IMethodCallMessage call = (IMethodCallMessage)obj;
  44. writer.Write ((byte) BinaryElement.MethodCall);
  45. MethodFlags methodFlags;
  46. int infoArraySize = 0;
  47. object info = null;
  48. object[] extraProperties = null;
  49. if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo)
  50. {
  51. methodFlags = MethodFlags.IncludesLogicalCallContext;
  52. infoArraySize++;
  53. }
  54. else
  55. methodFlags = MethodFlags.ExcludeLogicalCallContext;
  56. if (RemotingServices.IsMethodOverloaded (call))
  57. {
  58. infoArraySize++;
  59. methodFlags |= MethodFlags.IncludesSignature;
  60. }
  61. if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length)
  62. {
  63. extraProperties = GetExtraProperties (call.Properties, MethodCallDictionary.InternalKeys);
  64. infoArraySize++;
  65. }
  66. if (call.MethodBase.IsGenericMethod) {
  67. infoArraySize++;
  68. methodFlags |= MethodFlags.GenericArguments;
  69. }
  70. if (call.ArgCount == 0)
  71. methodFlags |= MethodFlags.NoArguments;
  72. else {
  73. if (AllTypesArePrimitive (call.Args))
  74. methodFlags |= MethodFlags.PrimitiveArguments;
  75. else {
  76. if (infoArraySize == 0)
  77. methodFlags |= MethodFlags.ArgumentsInSimpleArray;
  78. else {
  79. methodFlags |= MethodFlags.ArgumentsInMultiArray;
  80. infoArraySize++;
  81. }
  82. }
  83. }
  84. writer.Write ((int) methodFlags);
  85. // Method name
  86. writer.Write ((byte) BinaryTypeCode.String);
  87. writer.Write (call.MethodName);
  88. // Class name
  89. writer.Write ((byte) BinaryTypeCode.String);
  90. writer.Write (call.TypeName);
  91. // Arguments
  92. if ((methodFlags & MethodFlags.PrimitiveArguments) > 0)
  93. {
  94. writer.Write ((uint)call.Args.Length);
  95. for (int n=0; n<call.ArgCount; n++)
  96. {
  97. object arg = call.GetArg(n);
  98. if (arg != null) {
  99. writer.Write (BinaryCommon.GetTypeCode (arg.GetType()));
  100. ObjectWriter.WritePrimitiveValue (writer, arg);
  101. }
  102. else
  103. writer.Write ((byte)BinaryTypeCode.Null);
  104. }
  105. }
  106. if ( infoArraySize > 0)
  107. {
  108. object[] ainfo = new object[infoArraySize];
  109. int n=0;
  110. if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0) ainfo[n++] = call.Args;
  111. if ((methodFlags & MethodFlags.GenericArguments) > 0) ainfo[n++] = call.MethodBase.GetGenericArguments ();
  112. if ((methodFlags & MethodFlags.IncludesSignature) > 0) ainfo[n++] = call.MethodSignature;
  113. if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0) ainfo[n++] = call.LogicalCallContext;
  114. if (extraProperties != null) ainfo[n++] = extraProperties;
  115. info = ainfo;
  116. }
  117. else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0)
  118. info = call.Args;
  119. if (info != null)
  120. {
  121. ObjectWriter objectWriter = new ObjectWriter (formatter);
  122. objectWriter.WriteObjectGraph (writer, info, headers);
  123. }
  124. else
  125. writer.Write ((byte) BinaryElement.End);
  126. }
  127. public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter)
  128. {
  129. IMethodReturnMessage resp = (IMethodReturnMessage)obj;
  130. writer.Write ((byte) BinaryElement.MethodResponse);
  131. string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;
  132. int infoArrayLength = 0;
  133. object info = null;
  134. object[] extraProperties = null;
  135. // Type of return value
  136. ReturnTypeTag returnTypeTag;
  137. MethodFlags contextFlag = MethodFlags.ExcludeLogicalCallContext;
  138. if (resp.Exception != null) {
  139. returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null;
  140. internalProperties = MethodReturnDictionary.InternalExceptionKeys;
  141. infoArrayLength = 1;
  142. }
  143. else if (resp.ReturnValue == null) {
  144. returnTypeTag = ReturnTypeTag.Null;
  145. }
  146. else if (IsMethodPrimitive(resp.ReturnValue.GetType())) {
  147. returnTypeTag = ReturnTypeTag.PrimitiveType;
  148. }
  149. else {
  150. returnTypeTag = ReturnTypeTag.ObjectType;
  151. infoArrayLength++;
  152. }
  153. // Message flags
  154. MethodFlags formatFlag;
  155. if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo)
  156. {
  157. contextFlag = MethodFlags.IncludesLogicalCallContext;
  158. infoArrayLength++;
  159. }
  160. if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
  161. {
  162. extraProperties = GetExtraProperties (resp.Properties, internalProperties);
  163. infoArrayLength++;
  164. }
  165. if (resp.OutArgCount == 0)
  166. formatFlag = MethodFlags.NoArguments;
  167. else
  168. {
  169. if (AllTypesArePrimitive (resp.Args))
  170. formatFlag = MethodFlags.PrimitiveArguments;
  171. else
  172. {
  173. if (infoArrayLength == 0)
  174. formatFlag = MethodFlags.ArgumentsInSimpleArray;
  175. else {
  176. formatFlag = MethodFlags.ArgumentsInMultiArray;
  177. infoArrayLength++;
  178. }
  179. }
  180. }
  181. writer.Write ((byte) (contextFlag | formatFlag));
  182. writer.Write ((byte) returnTypeTag);
  183. // FIXME: what are the following 2 bytes for?
  184. writer.Write ((byte) 0);
  185. writer.Write ((byte) 0);
  186. // Arguments
  187. if (returnTypeTag == ReturnTypeTag.PrimitiveType)
  188. {
  189. writer.Write (BinaryCommon.GetTypeCode (resp.ReturnValue.GetType()));
  190. ObjectWriter.WritePrimitiveValue (writer, resp.ReturnValue);
  191. }
  192. if (formatFlag == MethodFlags.PrimitiveArguments)
  193. {
  194. writer.Write ((uint)resp.ArgCount);
  195. for (int n=0; n<resp.ArgCount; n++)
  196. {
  197. object val = resp.GetArg(n);
  198. if (val != null) {
  199. writer.Write (BinaryCommon.GetTypeCode (val.GetType()));
  200. ObjectWriter.WritePrimitiveValue (writer, val);
  201. }
  202. else
  203. writer.Write ((byte)BinaryTypeCode.Null);
  204. }
  205. }
  206. if (infoArrayLength > 0)
  207. {
  208. object[] infoArray = new object[infoArrayLength];
  209. int n = 0;
  210. if ((returnTypeTag & ReturnTypeTag.Exception) != 0)
  211. infoArray[n++] = resp.Exception;
  212. if (formatFlag == MethodFlags.ArgumentsInMultiArray)
  213. infoArray[n++] = resp.Args;
  214. if (returnTypeTag == ReturnTypeTag.ObjectType)
  215. infoArray[n++] = resp.ReturnValue;
  216. if (contextFlag == MethodFlags.IncludesLogicalCallContext)
  217. infoArray[n++] = resp.LogicalCallContext;
  218. if (extraProperties != null)
  219. infoArray[n++] = extraProperties;
  220. info = infoArray;
  221. }
  222. else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
  223. info = resp.Args;
  224. if (info != null)
  225. {
  226. ObjectWriter objectWriter = new ObjectWriter (formatter);
  227. objectWriter.WriteObjectGraph (writer, info, headers);
  228. }
  229. else
  230. writer.Write ((byte) BinaryElement.End);
  231. }
  232. public static object ReadMethodCall (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
  233. {
  234. BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
  235. return ReadMethodCall (elem, reader, hasHeaders, headerHandler, formatter);
  236. }
  237. public static object ReadMethodCall (BinaryElement elem, BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
  238. {
  239. if (elem != BinaryElement.MethodCall) throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
  240. MethodFlags flags = (MethodFlags) reader.ReadInt32();
  241. if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
  242. string methodName = reader.ReadString();
  243. if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
  244. string className = reader.ReadString();
  245. //bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  246. object[] arguments = null;
  247. object methodSignature = null;
  248. object callContext = null;
  249. object[] extraProperties = null;
  250. Header[] headers = null;
  251. Type[] genericArguments = null;
  252. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  253. {
  254. uint count = reader.ReadUInt32();
  255. arguments = new object[count];
  256. for (int n=0; n<count; n++)
  257. {
  258. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  259. arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  260. }
  261. }
  262. if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
  263. {
  264. ObjectReader objectReader = new ObjectReader (formatter);
  265. object result;
  266. objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
  267. object[] msgInfo = (object[]) result;
  268. if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  269. arguments = msgInfo;
  270. }
  271. else
  272. {
  273. int n = 0;
  274. if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
  275. if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
  276. else arguments = new object[0];
  277. }
  278. if ((flags & MethodFlags.GenericArguments) > 0)
  279. genericArguments = (Type[]) msgInfo[n++];
  280. if ((flags & MethodFlags.IncludesSignature) > 0)
  281. methodSignature = msgInfo[n++];
  282. if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
  283. callContext = msgInfo[n++];
  284. if (n < msgInfo.Length)
  285. extraProperties = (object[]) msgInfo[n];
  286. }
  287. }
  288. else {
  289. reader.ReadByte (); // Reads the stream ender
  290. }
  291. if (arguments == null) arguments = new object[0];
  292. string uri = null;
  293. if (headerHandler != null)
  294. uri = headerHandler(headers) as string;
  295. Header[] methodInfo = new Header[7];
  296. methodInfo[0] = new Header("__MethodName", methodName);
  297. methodInfo[1] = new Header("__MethodSignature", methodSignature);
  298. methodInfo[2] = new Header("__TypeName", className);
  299. methodInfo[3] = new Header("__Args", arguments);
  300. methodInfo[4] = new Header("__CallContext", callContext);
  301. methodInfo[5] = new Header("__Uri", uri);
  302. methodInfo[6] = new Header("__GenericArguments", genericArguments);
  303. MethodCall call = new MethodCall (methodInfo);
  304. if (extraProperties != null) {
  305. foreach (DictionaryEntry entry in extraProperties)
  306. call.Properties [(string)entry.Key] = entry.Value;
  307. }
  308. return call;
  309. }
  310. public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
  311. {
  312. BinaryElement elem = (BinaryElement) reader.ReadByte ();
  313. return ReadMethodResponse (elem, reader, hasHeaders, headerHandler, methodCallMessage, formatter);
  314. }
  315. public static object ReadMethodResponse (BinaryElement elem, BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
  316. {
  317. if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
  318. MethodFlags flags = (MethodFlags) reader.ReadByte ();
  319. ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
  320. bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  321. // FIXME: find a meaning for those 2 bytes
  322. reader.ReadByte();
  323. reader.ReadByte();
  324. object returnValue = null;
  325. object[] outArgs = null;
  326. LogicalCallContext callContext = null;
  327. Exception exception = null;
  328. object[] extraProperties = null;
  329. Header[] headers = null;
  330. if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
  331. {
  332. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  333. returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  334. }
  335. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  336. {
  337. uint count = reader.ReadUInt32();
  338. outArgs = new object[count];
  339. for (int n=0; n<count; n++) {
  340. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  341. outArgs[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  342. }
  343. }
  344. if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
  345. (typeTag & ReturnTypeTag.Exception) > 0 ||
  346. (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
  347. (flags & MethodFlags.ArgumentsInMultiArray) > 0)
  348. {
  349. // There objects that need to be deserialized using an ObjectReader
  350. ObjectReader objectReader = new ObjectReader (formatter);
  351. object result;
  352. objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
  353. object[] msgInfo = (object[]) result;
  354. if ((typeTag & ReturnTypeTag.Exception) > 0) {
  355. exception = (Exception) msgInfo[0];
  356. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[1];
  357. }
  358. else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
  359. int n = 0;
  360. if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo [n++];
  361. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
  362. if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
  363. }
  364. else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  365. outArgs = msgInfo;
  366. }
  367. else {
  368. int n = 0;
  369. outArgs = (object[]) msgInfo[n++];
  370. if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo[n++];
  371. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
  372. if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
  373. }
  374. }
  375. else {
  376. reader.ReadByte (); // Reads the stream ender
  377. }
  378. if (headerHandler != null)
  379. headerHandler(headers);
  380. if (exception != null)
  381. return new ReturnMessage (exception, methodCallMessage);
  382. else
  383. {
  384. int argCount = (outArgs!=null) ? outArgs.Length : 0;
  385. ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
  386. if (extraProperties != null) {
  387. foreach (DictionaryEntry entry in extraProperties)
  388. result.Properties [(string)entry.Key] = entry.Value;
  389. }
  390. return result;
  391. }
  392. }
  393. private static bool AllTypesArePrimitive(object[] objects)
  394. {
  395. foreach (object ob in objects)
  396. {
  397. if (ob != null && !IsMethodPrimitive(ob.GetType()))
  398. return false;
  399. }
  400. return true;
  401. }
  402. // When serializing methods, string are considered primitive types
  403. public static bool IsMethodPrimitive (Type type)
  404. {
  405. return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
  406. }
  407. static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
  408. {
  409. object[] extraProperties = new object [properties.Count - internalKeys.Length];
  410. int n = 0;
  411. IDictionaryEnumerator e = properties.GetEnumerator();
  412. while (e.MoveNext())
  413. if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
  414. return extraProperties;
  415. }
  416. static bool IsInternalKey (string key, string[] internalKeys)
  417. {
  418. foreach (string ikey in internalKeys)
  419. if (key == ikey) return true;
  420. return false;
  421. }
  422. }
  423. }