SimpleWireFormat.cs 6.3 KB

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