SimpleWireFormat.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Runtime.Serialization.Formatters;
  28. using System.Runtime.Serialization;
  29. using System.Reflection;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Runtime.Remoting.Messaging;
  33. namespace System.Runtime.Remoting.Channels.Simple {
  34. public sealed class SimpleWireFormat
  35. {
  36. enum TypeId : byte {
  37. Boolean,
  38. Byte,
  39. Char,
  40. Decimal,
  41. Double,
  42. Int16,
  43. Int32,
  44. Int64,
  45. SByte,
  46. String,
  47. Single,
  48. UInt16,
  49. UInt32,
  50. UInt64,
  51. NULL
  52. }
  53. public SimpleWireFormat ()
  54. {
  55. }
  56. void SerializeObject (BinaryWriter writer, object obj)
  57. {
  58. if (obj == null) {
  59. writer.Write ((byte)TypeId.NULL);
  60. return;
  61. }
  62. Type type = obj.GetType ();
  63. if (type == typeof (String))
  64. {
  65. writer.Write ((byte)TypeId.String);
  66. writer.Write ((String)obj);
  67. return;
  68. }
  69. if (type == typeof (int)) {
  70. writer.Write ((byte)TypeId.Int32);
  71. writer.Write ((int)obj);
  72. return;
  73. }
  74. if (type == typeof (long)) {
  75. writer.Write ((byte)TypeId.Int64);
  76. writer.Write ((long)obj);
  77. return;
  78. }
  79. if (type == typeof (uint)) {
  80. writer.Write ((byte)TypeId.UInt32);
  81. writer.Write ((uint)obj);
  82. return;
  83. }
  84. if (type == typeof (ulong)) {
  85. writer.Write ((byte)TypeId.UInt64);
  86. writer.Write ((ulong)obj);
  87. return;
  88. }
  89. if (type == typeof (bool)) {
  90. writer.Write ((byte)TypeId.Boolean);
  91. writer.Write ((bool)obj);
  92. return;
  93. }
  94. if (type == typeof (byte)) {
  95. writer.Write ((byte)TypeId.Byte);
  96. writer.Write ((byte)obj);
  97. return;
  98. }
  99. if (type == typeof (sbyte)) {
  100. writer.Write ((byte)TypeId.SByte);
  101. writer.Write ((sbyte)obj);
  102. return;
  103. }
  104. if (type == typeof (char)) {
  105. writer.Write ((byte)TypeId.Char);
  106. writer.Write ((char)obj);
  107. return;
  108. }
  109. if (type == typeof (double)) {
  110. writer.Write ((byte)TypeId.Double);
  111. writer.Write ((double)obj);
  112. return;
  113. }
  114. if (type == typeof (Single)) {
  115. writer.Write ((byte)TypeId.Single);
  116. writer.Write ((Single)obj);
  117. return;
  118. }
  119. if (type == typeof (Int16)) {
  120. writer.Write ((byte)TypeId.Int16);
  121. writer.Write ((Int16)obj);
  122. return;
  123. }
  124. if (type == typeof (UInt16)) {
  125. writer.Write ((byte)TypeId.UInt16);
  126. writer.Write ((UInt16)obj);
  127. return;
  128. }
  129. if (type == typeof (Decimal)) {
  130. writer.Write ((byte)TypeId.Decimal);
  131. writer.Write ((Decimal)obj);
  132. return;
  133. }
  134. throw new NotSupportedException ();
  135. }
  136. object DeserializeObject (BinaryReader reader)
  137. {
  138. TypeId tid = (TypeId)reader.ReadByte ();
  139. if (tid == TypeId.NULL)
  140. return null;
  141. if (tid == TypeId.String) {
  142. return reader.ReadString ();
  143. }
  144. if (tid == TypeId.Int32) {
  145. return reader.ReadInt32 ();
  146. }
  147. if (tid == TypeId.Int64) {
  148. return reader.ReadInt64 ();
  149. }
  150. if (tid == TypeId.UInt32) {
  151. return reader.ReadUInt32 ();
  152. }
  153. if (tid == TypeId.UInt64) {
  154. return reader.ReadUInt64 ();
  155. }
  156. if (tid == TypeId.Boolean) {
  157. return reader.ReadBoolean ();
  158. }
  159. if (tid == TypeId.Byte) {
  160. return reader.ReadByte ();
  161. }
  162. if (tid == TypeId.SByte) {
  163. return reader.ReadSByte ();
  164. }
  165. if (tid == TypeId.Char) {
  166. return reader.ReadChar ();
  167. }
  168. if (tid == TypeId.Double) {
  169. return reader.ReadDouble ();
  170. }
  171. if (tid == TypeId.Single) {
  172. return reader.ReadSingle ();
  173. }
  174. if (tid == TypeId.Byte) {
  175. return reader.ReadByte ();
  176. }
  177. if (tid == TypeId.Int16) {
  178. return reader.ReadInt16 ();
  179. }
  180. if (tid == TypeId.UInt16) {
  181. return reader.ReadUInt16 ();
  182. }
  183. if (tid == TypeId.Decimal) {
  184. return reader.ReadDecimal ();
  185. }
  186. throw new NotSupportedException ();
  187. }
  188. public IMethodCallMessage DeserializeRequest (Stream serializationStream, string uri)
  189. {
  190. if (serializationStream == null) {
  191. throw new ArgumentNullException ("serializationStream is null");
  192. }
  193. Type svr_type = RemotingServices.GetServerTypeForUri (uri);
  194. if (svr_type == null)
  195. throw new RemotingException ("no registered server for uri " + uri);
  196. BinaryReader reader = new BinaryReader (serializationStream);
  197. string method_name = reader.ReadString ();
  198. int arg_count = reader.ReadInt32 ();
  199. object [] args = new object [arg_count];
  200. for (int i = 0; i < arg_count; i++) {
  201. args [i] = DeserializeObject (reader);
  202. }
  203. MonoMethodMessage msg = new MonoMethodMessage (svr_type, method_name, args);
  204. msg.Uri = uri;
  205. return msg;
  206. }
  207. public IMethodReturnMessage DeserializeResponse (Stream serializationStream,
  208. IMethodCallMessage request)
  209. {
  210. BinaryReader reader = new BinaryReader (serializationStream);
  211. object return_value = DeserializeObject (reader);
  212. int arg_count = reader.ReadInt32 ();
  213. object [] out_args = new object [arg_count];
  214. for (int i = 0; i < arg_count; i++)
  215. out_args [i] = DeserializeObject (reader);
  216. return new ReturnMessage (return_value, out_args, arg_count, null, request);
  217. }
  218. public void SerializeRequest (Stream serializationStream, object graph)
  219. {
  220. if (serializationStream == null) {
  221. throw new ArgumentNullException ("serializationStream is null");
  222. }
  223. BinaryWriter writer = new BinaryWriter (serializationStream);
  224. IMethodCallMessage msg = graph as IMethodCallMessage;
  225. if (msg != null) {
  226. writer.Write (msg.MethodName);
  227. writer.Write ((int)msg.InArgCount);
  228. for (int i = 0; i < msg.InArgCount; i++)
  229. SerializeObject (writer, msg.GetInArg (i));
  230. return;
  231. }
  232. throw new NotSupportedException ();
  233. }
  234. public void SerializeResponse (Stream serializationStream, object graph)
  235. {
  236. if (serializationStream == null) {
  237. throw new ArgumentNullException ("serializationStream is null");
  238. }
  239. BinaryWriter writer = new BinaryWriter (serializationStream);
  240. IMethodReturnMessage res = graph as IMethodReturnMessage;
  241. if (res != null) {
  242. // this channel does not support serialization of exception,
  243. // so we simply let the transport decide what to do
  244. if (res.Exception != null)
  245. return;
  246. SerializeObject (writer, res.ReturnValue);
  247. writer.Write (res.OutArgCount);
  248. for (int i = 0; i < res.OutArgCount; i++)
  249. SerializeObject (writer, res.GetOutArg (i));
  250. return;
  251. }
  252. throw new NotSupportedException ();
  253. }
  254. }
  255. }