2
0

MessageFormatter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.ArgCount; 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, SerializationBinder binder)
  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. Header[] headers = null;
  228. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  229. {
  230. uint count = reader.ReadUInt32();
  231. arguments = new object[count];
  232. for (int n=0; n<count; n++)
  233. {
  234. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  235. arguments[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  236. }
  237. }
  238. if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
  239. {
  240. ObjectReader objectReader = new ObjectReader(surrogateSelector, context, binder);
  241. object result;
  242. objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
  243. object[] msgInfo = (object[]) result;
  244. if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  245. arguments = msgInfo;
  246. }
  247. else
  248. {
  249. int n = 0;
  250. if ((flags & MethodFlags.ArgumentsInMultiArray) > 0) {
  251. if (msgInfo.Length > 1) arguments = (object[]) msgInfo[n++];
  252. else arguments = new object[0];
  253. }
  254. if ((flags & MethodFlags.IncludesSignature) > 0)
  255. methodSignature = msgInfo[n++];
  256. if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
  257. callContext = msgInfo[n++];
  258. if (n < msgInfo.Length)
  259. extraProperties = (object[]) msgInfo[n];
  260. }
  261. }
  262. else {
  263. reader.ReadByte (); // Reads the stream ender
  264. }
  265. if (arguments == null) arguments = new object[0];
  266. string uri = null;
  267. if (headerHandler != null)
  268. uri = headerHandler(headers) as string;
  269. Header[] methodInfo = new Header[6];
  270. methodInfo[0] = new Header("__MethodName", methodName);
  271. methodInfo[1] = new Header("__MethodSignature", methodSignature);
  272. methodInfo[2] = new Header("__TypeName", className);
  273. methodInfo[3] = new Header("__Args", arguments);
  274. methodInfo[4] = new Header("__CallContext", callContext);
  275. methodInfo[5] = new Header("__Uri", uri);
  276. MethodCall call = new MethodCall (methodInfo);
  277. if (extraProperties != null) {
  278. foreach (DictionaryEntry entry in extraProperties)
  279. call.Properties [(string)entry.Key] = entry.Value;
  280. }
  281. return call;
  282. }
  283. public static object ReadMethodResponse (BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, ISurrogateSelector surrogateSelector, StreamingContext context, SerializationBinder binder)
  284. {
  285. BinaryElement elem = (BinaryElement)reader.ReadByte(); // The element code
  286. if (elem != BinaryElement.MethodResponse) throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
  287. MethodFlags flags = (MethodFlags) reader.ReadByte ();
  288. ReturnTypeTag typeTag = (ReturnTypeTag) reader.ReadByte ();
  289. bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;
  290. // FIXME: find a meaning for those 2 bytes
  291. reader.ReadByte();
  292. reader.ReadByte();
  293. object returnValue = null;
  294. object[] outArgs = null;
  295. LogicalCallContext callContext = null;
  296. Exception exception = null;
  297. object[] extraProperties = null;
  298. Header[] headers = null;
  299. if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
  300. {
  301. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  302. returnValue = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  303. }
  304. if ((flags & MethodFlags.PrimitiveArguments) > 0)
  305. {
  306. uint count = reader.ReadUInt32();
  307. outArgs = new object[count];
  308. for (int n=0; n<count; n++) {
  309. Type type = BinaryCommon.GetTypeFromCode (reader.ReadByte());
  310. outArgs[n] = ObjectReader.ReadPrimitiveTypeValue (reader, type);
  311. }
  312. }
  313. if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
  314. (typeTag & ReturnTypeTag.Exception) > 0 ||
  315. (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
  316. (flags & MethodFlags.ArgumentsInMultiArray) > 0)
  317. {
  318. // There objects that need to be deserialized using an ObjectReader
  319. ObjectReader objectReader = new ObjectReader(surrogateSelector, context, binder);
  320. object result;
  321. objectReader.ReadObjectGraph (reader, hasHeaders, out result, out headers);
  322. object[] msgInfo = (object[]) result;
  323. if ((typeTag & ReturnTypeTag.Exception) > 0) {
  324. exception = (Exception) msgInfo[0];
  325. }
  326. else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0) {
  327. int n = 0;
  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. else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0) {
  333. outArgs = msgInfo;
  334. }
  335. else {
  336. int n = 0;
  337. outArgs = (object[]) msgInfo[n++];
  338. if ((typeTag & ReturnTypeTag.ObjectType) > 0) returnValue = msgInfo[n++];
  339. if (hasContextInfo) callContext = (LogicalCallContext)msgInfo[n++];
  340. if (n < msgInfo.Length) extraProperties = (object[]) msgInfo[n];
  341. }
  342. }
  343. else {
  344. reader.ReadByte (); // Reads the stream ender
  345. }
  346. if (headerHandler != null)
  347. headerHandler(headers);
  348. if (exception != null)
  349. return new ReturnMessage (exception, methodCallMessage);
  350. else
  351. {
  352. int argCount = (outArgs!=null) ? outArgs.Length : 0;
  353. ReturnMessage result = new ReturnMessage (returnValue, outArgs, argCount, callContext, methodCallMessage);
  354. if (extraProperties != null) {
  355. foreach (DictionaryEntry entry in extraProperties)
  356. result.Properties [(string)entry.Key] = entry.Value;
  357. }
  358. return result;
  359. }
  360. }
  361. private static bool AllTypesArePrimitive(object[] objects)
  362. {
  363. foreach (object ob in objects)
  364. {
  365. if (ob != null && !IsMethodPrimitive(ob.GetType()))
  366. return false;
  367. }
  368. return true;
  369. }
  370. // When serializing methods, string are considered primitive types
  371. public static bool IsMethodPrimitive (Type type)
  372. {
  373. return type.IsPrimitive || type == typeof(string) || type == typeof (DateTime) || type == typeof (Decimal);
  374. }
  375. static object[] GetExtraProperties (IDictionary properties, string[] internalKeys)
  376. {
  377. object[] extraProperties = new object [properties.Count - internalKeys.Length];
  378. int n = 0;
  379. IDictionaryEnumerator e = properties.GetEnumerator();
  380. while (e.MoveNext())
  381. if (!IsInternalKey ((string) e.Entry.Key, internalKeys)) extraProperties [n++] = e.Entry;
  382. return extraProperties;
  383. }
  384. static bool IsInternalKey (string key, string[] internalKeys)
  385. {
  386. foreach (string ikey in internalKeys)
  387. if (key == ikey) return true;
  388. return false;
  389. }
  390. }
  391. }