SimpleMessageFormat.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // System.Runtime.Remoting.Channels.Simple.SimpleMessageFormat.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 SimpleMessageFormat
  35. {
  36. public enum MessageType : byte {
  37. Request = 0,
  38. Response = 1,
  39. Exception = 2,
  40. Unknown = 3
  41. }
  42. public SimpleMessageFormat ()
  43. {
  44. }
  45. public static void SendExceptionMessage (Stream network_stream, string message)
  46. {
  47. // we use the uri field to encode the message text
  48. SendMessageStream (network_stream, null, MessageType.Exception, message);
  49. }
  50. public static void SendMessageStream (Stream network_stream, MemoryStream data,
  51. MessageType msg_type, string uri)
  52. {
  53. int data_len = 0;
  54. if (data != null)
  55. data_len = (int)data.Length;
  56. int uri_len = 0;
  57. if (uri != null)
  58. uri_len = uri.Length;
  59. int header_len = 12 + uri_len * 2;
  60. int msg_len = data_len + header_len;
  61. byte [] buffer = new byte [msg_len];
  62. // magic header signature
  63. buffer [0] = 255;
  64. buffer [1] = 0;
  65. buffer [2] = 255;
  66. buffer [3] = (byte) msg_type;
  67. // data length
  68. buffer [4] = (byte) data_len;
  69. buffer [5] = (byte) (data_len >> 8);
  70. buffer [6] = (byte) (data_len >> 16);
  71. buffer [7] = (byte) (data_len >> 24);
  72. // uri length
  73. buffer [8] = (byte) uri_len;
  74. buffer [9] = (byte) (uri_len >> 8);
  75. buffer [10] = (byte) (uri_len >> 16);
  76. buffer [11] = (byte) (uri_len >> 24);
  77. // uri
  78. for (int i = 0; i < uri_len; i++) {
  79. buffer [12 + i*2] = (byte) uri [i];
  80. buffer [13 + i*2] = (byte) (uri [i] >> 8);
  81. }
  82. if (data_len > 0) {
  83. byte [] data_buffer = data.GetBuffer ();
  84. for (int i = 0; i < data_len; i++)
  85. buffer [i + header_len] = data_buffer [i];
  86. }
  87. network_stream.Write (buffer, 0, msg_len);
  88. }
  89. public static MemoryStream ReceiveMessageStream (Stream network_stream,
  90. out MessageType msg_type,
  91. out string uri)
  92. {
  93. int data_len = 0;
  94. int uri_len = 0;
  95. msg_type = MessageType.Unknown;
  96. uri = null;
  97. // search for message header (255, 0, 255, msg_type, msg_len)
  98. while (true) {
  99. while (true) {
  100. int x = network_stream.ReadByte ();
  101. if (x != 255)
  102. continue;
  103. x = network_stream.ReadByte ();
  104. if (x != 0)
  105. continue;
  106. x = network_stream.ReadByte ();
  107. if (x != 255)
  108. continue;
  109. break;
  110. }
  111. msg_type = (MessageType)network_stream.ReadByte ();
  112. byte [] buffer = new byte [8];
  113. int bytes_read = network_stream.Read (buffer, 0, 8);
  114. if (bytes_read != 8)
  115. continue;
  116. data_len = (buffer [0] | (buffer [1] << 8) |
  117. (buffer [2] << 16) | (buffer [3] << 24));
  118. uri_len = (buffer [4] | (buffer [5] << 8) |
  119. (buffer [6] << 16) | (buffer [7] << 24));
  120. if (uri_len > 0) {
  121. byte [] uri_buffer = new byte [uri_len * 2];
  122. bytes_read = network_stream.Read (uri_buffer, 0, uri_len * 2);
  123. if (bytes_read != (uri_len * 2))
  124. continue;
  125. char [] uri_array = new char [uri_len];
  126. for (int i = 0; i < uri_len; i++) {
  127. uri_array [i] = (char) (uri_buffer [i * 2] | (uri_buffer [(i * 2) + 1] << 8));
  128. }
  129. uri = new string (uri_array);
  130. }
  131. break;
  132. }
  133. if (msg_type == MessageType.Exception)
  134. throw new RemotingException ("\n" + uri + "\n" + "Rethrown at:\n");
  135. byte [] stream_buffer = new byte [data_len];
  136. if ((network_stream.Read (stream_buffer, 0, data_len)) != data_len)
  137. throw new Exception ("packet size error");
  138. return new MemoryStream (stream_buffer, false);
  139. }
  140. }
  141. }