MessageFormatter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // System.Runtime.Remoting.MessageFormatter.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  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, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
  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.ArgCount == 0)
  67. methodFlags |= MethodFlags.NoArguments;
  68. else {
  69. if (AllTypesArePrimitive (call.Args))
  70. methodFlags |= MethodFlags.PrimitiveArguments;
  71. else {
  72. if (infoArraySize == 0)
  73. methodFlags |= MethodFlags.ArgumentsInSimpleArray;
  74. else {
  75. methodFlags |= MethodFlags.ArgumentsInMultiArray;
  76. infoArraySize++;
  77. }
  78. }
  79. }
  80. writer.Write ((byte) (methodFlags));
  81. // FIXME: what are the following 3 bytes for?
  82. writer.Write ((byte) 0);
  83. writer.Write ((byte) 0);
  84. writer.Write ((byte) 0);
  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.IncludesSignature) > 0) ainfo[n++] = call.MethodSignature;
  112. if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0) ainfo[n++] = call.LogicalCallContext;
  113. if (extraProperties != null) ainfo[n++] = extraProperties;
  114. info = ainfo;
  115. }
  116. else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0)
  117. info = call.Args;
  118. if (info != null)
  119. {
  120. ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat);
  121. objectWriter.WriteObjectGraph (writer, info, headers);
  122. }
  123. else
  124. writer.Write ((byte) BinaryElement.End);
  125. }
  126. public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
  127. {
  128. IMethodReturnMessage resp = (IMethodReturnMessage)obj;
  129. writer.Write ((byte) BinaryElement.MethodResponse);
  130. string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;
  131. int infoArrayLength = 0;
  132. object info = null;
  133. object[] extraProperties = null;
  134. // Type of return value
  135. ReturnTypeTag returnTypeTag;
  136. if (resp.Exception != null) {
  137. returnTypeTag = ReturnTypeTag.Exception | ReturnTypeTag.Null;
  138. info = new object[] {resp.Exception};
  139. internalProperties = MethodReturnDictionary.InternalExceptionKeys;
  140. }
  141. else if (resp.ReturnValue == null) {
  142. returnTypeTag = ReturnTypeTag.Null;
  143. }
  144. else if (IsMethodPrimitive(resp.ReturnValue.GetType())) {
  145. returnTypeTag = ReturnTypeTag.PrimitiveType;
  146. }
  147. else {
  148. returnTypeTag = ReturnTypeTag.ObjectType;
  149. infoArrayLength++;
  150. }
  151. // Message flags
  152. MethodFlags contextFlag;
  153. MethodFlags formatFlag;
  154. if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
  155. {
  156. contextFlag = MethodFlags.IncludesLogicalCallContext;
  157. infoArrayLength++;
  158. }
  159. else
  160. contextFlag = MethodFlags.ExcludeLogicalCallContext;
  161. if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
  162. {
  163. extraProperties = GetExtraProperties (resp.Properties, internalProperties);
  164. infoArrayLength++;
  165. }
  166. if (resp.OutArgCount == 0)
  167. formatFlag = MethodFlags.NoArguments;
  168. else
  169. {
  170. if (AllTypesArePrimitive (resp.Args))
  171. formatFlag = MethodFlags.PrimitiveArguments;
  172. else
  173. {
  174. if (infoArrayLength == 0)
  175. formatFlag = MethodFlags.ArgumentsInSimpleArray;
  176. else {
  177. formatFlag = MethodFlags.ArgumentsInMultiArray;
  178. infoArrayLength++;
  179. }
  180. }
  181. }
  182. writer.Write ((byte) (contextFlag | formatFlag));
  183. writer.Write ((byte) returnTypeTag);
  184. // FIXME: what are the following 2 bytes for?
  185. writer.Write ((byte) 0);
  186. writer.Write ((byte) 0);
  187. // Arguments
  188. if (returnTypeTag == ReturnTypeTag.PrimitiveType)
  189. {
  190. writer.Write (BinaryCommon.GetTypeCode (resp.ReturnValue.GetType()));
  191. ObjectWriter.WritePrimitiveValue (writer, resp.ReturnValue);
  192. }
  193. if (formatFlag == MethodFlags.PrimitiveArguments)
  194. {
  195. writer.Write ((uint)resp.ArgCount);
  196. for (int n=0; n<resp.ArgCount; n++)
  197. {
  198. object val = resp.GetArg(n);
  199. if (val != null) {
  200. writer.Write (BinaryCommon.GetTypeCode (val.GetType()));
  201. ObjectWriter.WritePrimitiveValue (writer, val);
  202. }
  203. else
  204. writer.Write ((byte)BinaryTypeCode.Null);
  205. }
  206. }
  207. if (infoArrayLength > 0)
  208. {
  209. object[] infoArray = new object[infoArrayLength];
  210. int n = 0;
  211. if (formatFlag == MethodFlags.ArgumentsInMultiArray)
  212. infoArray[n++] = resp.Args;
  213. if (returnTypeTag == ReturnTypeTag.ObjectType)
  214. infoArray[n++] = resp.ReturnValue;
  215. if (contextFlag == MethodFlags.IncludesLogicalCallContext)
  216. infoArray[n++] = resp.LogicalCallContext;
  217. if (extraProperties != null)
  218. infoArray[n++] = extraProperties;
  219. info = infoArray;
  220. }
  221. else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
  222. info = resp.Args;
  223. if (info != null)
  224. {
  225. ObjectWriter objectWriter = new ObjectWriter (surrogateSelector, context, assemblyFormat, typeFormat);
  226. objectWriter.WriteObjectGraph (writer, info, headers);
  227. }
  228. else
  229. writer.Write ((byte) BinaryElement.End);
  230. }
  231. public static object ReadMethodCall (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
  232. {
  233. BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
  234. if (elem != BinaryElement.MethodCall) throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
  235. MethodFlags flags = (MethodFlags) reader.ReadByte();
  236. // FIXME: find a meaning for those 3 bytes
  237. reader.ReadByte();
  238. reader.ReadByte();
  239. reader.ReadByte();
  240. if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
  241. string methodName = reader.ReadString();
  242. if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String) throw new SerializationException ("Invalid format");
  243. string className = reader.ReadString();
  244. //bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  245. object[] arguments = null;
  246. object methodSignature = null;
  247. object callContext = null;
  248. object[] extraProperties = null;
  249. Header[] headers = null;
  250. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  251. {
  252. uint count = reader.ReadUInt32();
  253. arguments = new object[count];
  254. for (int n=0; n<count; n++)
  255. {
  256. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  257. arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  258. }
  259. }
  260. if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
  261. {
  262. ObjectReader objectReader = new ObjectReader (formatter);
  263. object result;
  264. objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
  265. object[] msgInfo = (object[]) result;
  266. if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  267. arguments = msgInfo;
  268. }
  269. else
  270. {
  271. int n = 0;
  272. if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
  273. if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
  274. else arguments = new object[0];
  275. }
  276. if ((flags & MethodFlags.IncludesSignature) > 0)
  277. methodSignature = msgInfo[n++];
  278. if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
  279. callContext = msgInfo[n++];
  280. if (n < msgInfo.Length)
  281. extraProperties = (object[]) msgInfo[n];
  282. }
  283. }
  284. else {
  285. reader.ReadByte (); // Reads the stream ender
  286. }
  287. if (arguments == null) arguments = new object[0];
  288. string uri = null;
  289. if (headerHandler != null)
  290. uri = headerHandler(headers) as string;
  291. Header[] methodInfo = new Header[6];
  292. methodInfo[0] = new Header("__MethodName", methodName);
  293. methodInfo[1] = new Header("__MethodSignature", methodSignature);
  294. methodInfo[2] = new Header("__TypeName", className);
  295. methodInfo[3] = new Header("__Args", arguments);
  296. methodInfo[4] = new Header("__CallContext", callContext);
  297. methodInfo[5] = new Header("__Uri", uri);
  298. MethodCall call = new MethodCall (methodInfo);
  299. if (extraProperties != null) {
  300. foreach (DictionaryEntry entry in extraProperties)
  301. call.Properties [(string)entry.Key] = entry.Value;
  302. }
  303. return call;
  304. }
  305. public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
  306. {
  307. BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
  308. if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
  309. MethodFlags flags = (MethodFlags) reader.ReadByte ();
  310. ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
  311. bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  312. // FIXME: find a meaning for those 2 bytes
  313. reader.ReadByte();
  314. reader.ReadByte();
  315. object returnValue = null;
  316. object[] outArgs = null;
  317. LogicalCallContext callContext = null;
  318. Exception exception = null;
  319. object[] extraProperties = null;
  320. Header[] headers = null;
  321. if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
  322. {
  323. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  324. returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  325. }
  326. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  327. {
  328. uint count = reader.ReadUInt32();
  329. outArgs = new object[count];
  330. for (int n=0; n<count; n++) {
  331. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  332. outArgs[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  333. }
  334. }
  335. if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
  336. (typeTag & ReturnTypeTag.Exception) > 0 ||
  337. (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
  338. (flags & MethodFlags.ArgumentsInMultiArray) > 0)
  339. {
  340. // There objects that need to be deserialized using an ObjectReader
  341. ObjectReader objectReader = new ObjectReader (formatter);
  342. object result;
  343. objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
  344. object[] msgInfo = (object[]) result;
  345. if ((typeTag & ReturnTypeTag.Exception) > 0) {
  346. exception = (Exception) msgInfo[0];
  347. }
  348. else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
  349. int n = 0;
  350. if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo [n++];
  351. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
  352. if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
  353. }
  354. else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  355. outArgs = msgInfo;
  356. }
  357. else {
  358. int n = 0;
  359. outArgs = (object[]) msgInfo[n++];
  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. }
  365. else {
  366. reader.ReadByte (); // Reads the stream ender
  367. }
  368. if (headerHandler != null)
  369. headerHandler(headers);
  370. if (exception != null)
  371. return new ReturnMessage (exception, methodCallMessage);
  372. else
  373. {
  374. int argCount = (outArgs!=null) ? outArgs.Length : 0;
  375. ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
  376. if (extraProperties != null) {
  377. foreach (DictionaryEntry entry in extraProperties)
  378. result.Properties [(string)entry.Key] = entry.Value;
  379. }
  380. return result;
  381. }
  382. }
  383. private static bool AllTypesArePrimitive(object[] objects)
  384. {
  385. foreach (object ob in objects)
  386. {
  387. if (ob != null && !IsMethodPrimitive(ob.GetType()))
  388. return false;
  389. }
  390. return true;
  391. }
  392. // When serializing methods, string are considered primitive types
  393. public static bool IsMethodPrimitive (Type type)
  394. {
  395. return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
  396. }
  397. static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
  398. {
  399. object[] extraProperties = new object [properties.Count - internalKeys.Length];
  400. int n = 0;
  401. IDictionaryEnumerator e = properties.GetEnumerator();
  402. while (e.MoveNext())
  403. if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
  404. return extraProperties;
  405. }
  406. static bool IsInternalKey (string key, string[] internalKeys)
  407. {
  408. foreach (string ikey in internalKeys)
  409. if (key == ikey) return true;
  410. return false;
  411. }
  412. }
  413. }