SimpleWireFormat.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // System.Runtime.Remoting.Channels.Simple.SimpleWireFormat.cs
  2. //
  3. // Author:
  4. // DietmarMaurer ([email protected])
  5. //
  6. // (C) 2002 Ximian, Inc. http://www.ximian.com
  7. using System.Runtime.Serialization.Formatters;
  8. using System.Runtime.Serialization;
  9. using System.Reflection;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Runtime.Remoting.Messaging;
  13. namespace System.Runtime.Remoting.Channels.Simple {
  14. public sealed class SimpleWireFormat
  15. {
  16. enum TypeId : byte {
  17. Boolean,
  18. Byte,
  19. Char,
  20. Decimal,
  21. Double,
  22. Int16,
  23. Int32,
  24. Int64,
  25. SByte,
  26. String,
  27. Single,
  28. UInt16,
  29. UInt32,
  30. UInt64,
  31. NULL
  32. }
  33. public SimpleWireFormat ()
  34. {
  35. }
  36. void SerializeObject (BinaryWriter writer, object obj)
  37. {
  38. if (obj == null) {
  39. writer.Write ((byte)TypeId.NULL);
  40. return;
  41. }
  42. Type type = obj.GetType ();
  43. if (type == typeof (String))
  44. {
  45. writer.Write ((byte)TypeId.String);
  46. writer.Write ((String)obj);
  47. return;
  48. }
  49. if (type == typeof (int)) {
  50. writer.Write ((byte)TypeId.Int32);
  51. writer.Write ((int)obj);
  52. return;
  53. }
  54. if (type == typeof (long)) {
  55. writer.Write ((byte)TypeId.Int64);
  56. writer.Write ((long)obj);
  57. return;
  58. }
  59. if (type == typeof (uint)) {
  60. writer.Write ((byte)TypeId.UInt32);
  61. writer.Write ((uint)obj);
  62. return;
  63. }
  64. if (type == typeof (ulong)) {
  65. writer.Write ((byte)TypeId.UInt64);
  66. writer.Write ((ulong)obj);
  67. return;
  68. }
  69. if (type == typeof (bool)) {
  70. writer.Write ((byte)TypeId.Boolean);
  71. writer.Write ((bool)obj);
  72. return;
  73. }
  74. if (type == typeof (byte)) {
  75. writer.Write ((byte)TypeId.Byte);
  76. writer.Write ((byte)obj);
  77. return;
  78. }
  79. if (type == typeof (sbyte)) {
  80. writer.Write ((byte)TypeId.SByte);
  81. writer.Write ((sbyte)obj);
  82. return;
  83. }
  84. if (type == typeof (char)) {
  85. writer.Write ((byte)TypeId.Char);
  86. writer.Write ((char)obj);
  87. return;
  88. }
  89. if (type == typeof (double)) {
  90. writer.Write ((byte)TypeId.Double);
  91. writer.Write ((double)obj);
  92. return;
  93. }
  94. if (type == typeof (Single)) {
  95. writer.Write ((byte)TypeId.Single);
  96. writer.Write ((Single)obj);
  97. return;
  98. }
  99. if (type == typeof (Int16)) {
  100. writer.Write ((byte)TypeId.Int16);
  101. writer.Write ((Int16)obj);
  102. return;
  103. }
  104. if (type == typeof (UInt16)) {
  105. writer.Write ((byte)TypeId.UInt16);
  106. writer.Write ((UInt16)obj);
  107. return;
  108. }
  109. if (type == typeof (Decimal)) {
  110. writer.Write ((byte)TypeId.Decimal);
  111. writer.Write ((Decimal)obj);
  112. return;
  113. }
  114. throw new NotSupportedException ();
  115. }
  116. object DeserializeObject (BinaryReader reader)
  117. {
  118. TypeId tid = (TypeId)reader.ReadByte ();
  119. if (tid == TypeId.NULL)
  120. return null;
  121. if (tid == TypeId.String) {
  122. return reader.ReadString ();
  123. }
  124. if (tid == TypeId.Int32) {
  125. return reader.ReadInt32 ();
  126. }
  127. if (tid == TypeId.Int64) {
  128. return reader.ReadInt64 ();
  129. }
  130. if (tid == TypeId.UInt32) {
  131. return reader.ReadUInt32 ();
  132. }
  133. if (tid == TypeId.UInt64) {
  134. return reader.ReadUInt64 ();
  135. }
  136. if (tid == TypeId.Boolean) {
  137. return reader.ReadBoolean ();
  138. }
  139. if (tid == TypeId.Byte) {
  140. return reader.ReadByte ();
  141. }
  142. if (tid == TypeId.SByte) {
  143. return reader.ReadSByte ();
  144. }
  145. if (tid == TypeId.Char) {
  146. return reader.ReadChar ();
  147. }
  148. if (tid == TypeId.Double) {
  149. return reader.ReadDouble ();
  150. }
  151. if (tid == TypeId.Single) {
  152. return reader.ReadSingle ();
  153. }
  154. if (tid == TypeId.Byte) {
  155. return reader.ReadByte ();
  156. }
  157. if (tid == TypeId.Int16) {
  158. return reader.ReadInt16 ();
  159. }
  160. if (tid == TypeId.UInt16) {
  161. return reader.ReadUInt16 ();
  162. }
  163. if (tid == TypeId.Decimal) {
  164. return reader.ReadDecimal ();
  165. }
  166. throw new NotSupportedException ();
  167. }
  168. public IMethodCallMessage DeserializeRequest (Stream serializationStream, string uri)
  169. {
  170. if (serializationStream == null) {
  171. throw new ArgumentNullException ("serializationStream is null");
  172. }
  173. Type svr_type = RemotingServices.GetServerTypeForUri (uri);
  174. if (svr_type == null)
  175. throw new RemotingException ("no registered server for uri " + uri);
  176. BinaryReader reader = new BinaryReader (serializationStream);
  177. string method_name = reader.ReadString ();
  178. int arg_count = reader.ReadInt32 ();
  179. object [] args = new object [arg_count];
  180. for (int i = 0; i < arg_count; i++) {
  181. args [i] = DeserializeObject (reader);
  182. }
  183. MonoMethodMessage msg = new MonoMethodMessage (svr_type, method_name, args);
  184. msg.Uri = uri;
  185. return msg;
  186. }
  187. public IMethodReturnMessage DeserializeResponse (Stream serializationStream,
  188. IMethodCallMessage request)
  189. {
  190. BinaryReader reader = new BinaryReader (serializationStream);
  191. object return_value = DeserializeObject (reader);
  192. int arg_count = reader.ReadInt32 ();
  193. object [] out_args = new object [arg_count];
  194. for (int i = 0; i < arg_count; i++)
  195. out_args [i] = DeserializeObject (reader);
  196. return new ReturnMessage (return_value, out_args, arg_count, null, request);
  197. }
  198. public void SerializeRequest (Stream serializationStream, object graph)
  199. {
  200. if (serializationStream == null) {
  201. throw new ArgumentNullException ("serializationStream is null");
  202. }
  203. BinaryWriter writer = new BinaryWriter (serializationStream);
  204. IMethodCallMessage msg = graph as IMethodCallMessage;
  205. if (msg != null) {
  206. writer.Write (msg.MethodName);
  207. writer.Write ((int)msg.InArgCount);
  208. for (int i = 0; i < msg.InArgCount; i++)
  209. SerializeObject (writer, msg.GetInArg (i));
  210. return;
  211. }
  212. throw new NotSupportedException ();
  213. }
  214. public void SerializeResponse (Stream serializationStream, object graph)
  215. {
  216. if (serializationStream == null) {
  217. throw new ArgumentNullException ("serializationStream is null");
  218. }
  219. BinaryWriter writer = new BinaryWriter (serializationStream);
  220. IMethodReturnMessage res = graph as IMethodReturnMessage;
  221. if (res != null) {
  222. // this channel does not support serialization of exception,
  223. // so we simply let the transport decide what to do
  224. if (res.Exception != null)
  225. return;
  226. SerializeObject (writer, res.ReturnValue);
  227. writer.Write (res.OutArgCount);
  228. for (int i = 0; i < res.OutArgCount; i++)
  229. SerializeObject (writer, res.GetOutArg (i));
  230. return;
  231. }
  232. throw new NotSupportedException ();
  233. }
  234. }
  235. }