1 // System.Runtime.Remoting.Channels.Simple.SimpleMessageFormat.cs
4 // DietmarMaurer (dietmar@ximian.com)
6 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System
.Runtime
.Serialization
.Formatters
;
30 using System
.Runtime
.Serialization
;
31 using System
.Reflection
;
32 using System
.Collections
;
34 using System
.Runtime
.Remoting
.Messaging
;
36 namespace System
.Runtime
.Remoting
.Channels
.Simple
{
38 public sealed class SimpleMessageFormat
40 public enum MessageType
: byte {
47 public SimpleMessageFormat ()
51 public static void SendExceptionMessage (Stream network_stream
, string message
)
53 // we use the uri field to encode the message text
54 SendMessageStream (network_stream
, null, MessageType
.Exception
, message
);
57 public static void SendMessageStream (Stream network_stream
, MemoryStream data
,
58 MessageType msg_type
, string uri
)
63 data_len
= (int)data
.Length
;
70 int header_len
= 12 + uri_len
* 2;
71 int msg_len
= data_len
+ header_len
;
73 byte [] buffer
= new byte [msg_len
];
75 // magic header signature
79 buffer
[3] = (byte) msg_type
;
82 buffer
[4] = (byte) data_len
;
83 buffer
[5] = (byte) (data_len
>> 8);
84 buffer
[6] = (byte) (data_len
>> 16);
85 buffer
[7] = (byte) (data_len
>> 24);
88 buffer
[8] = (byte) uri_len
;
89 buffer
[9] = (byte) (uri_len
>> 8);
90 buffer
[10] = (byte) (uri_len
>> 16);
91 buffer
[11] = (byte) (uri_len
>> 24);
94 for (int i
= 0; i
< uri_len
; i
++) {
95 buffer
[12 + i
*2] = (byte) uri
[i
];
96 buffer
[13 + i
*2] = (byte) (uri
[i
] >> 8);
100 byte [] data_buffer
= data
.GetBuffer ();
101 for (int i
= 0; i
< data_len
; i
++)
102 buffer
[i
+ header_len
] = data_buffer
[i
];
105 network_stream
.Write (buffer
, 0, msg_len
);
108 public static MemoryStream
ReceiveMessageStream (Stream network_stream
,
109 out MessageType msg_type
,
114 msg_type
= MessageType
.Unknown
;
117 // search for message header (255, 0, 255, msg_type, msg_len)
120 int x
= network_stream
.ReadByte ();
123 x
= network_stream
.ReadByte ();
126 x
= network_stream
.ReadByte ();
132 msg_type
= (MessageType
)network_stream
.ReadByte ();
134 byte [] buffer
= new byte [8];
136 int bytes_read
= network_stream
.Read (buffer
, 0, 8);
140 data_len
= (buffer
[0] | (buffer
[1] << 8) |
141 (buffer
[2] << 16) | (buffer
[3] << 24));
143 uri_len
= (buffer
[4] | (buffer
[5] << 8) |
144 (buffer
[6] << 16) | (buffer
[7] << 24));
147 byte [] uri_buffer
= new byte [uri_len
* 2];
148 bytes_read
= network_stream
.Read (uri_buffer
, 0, uri_len
* 2);
149 if (bytes_read
!= (uri_len
* 2))
151 char [] uri_array
= new char [uri_len
];
152 for (int i
= 0; i
< uri_len
; i
++) {
153 uri_array
[i
] = (char) (uri_buffer
[i
* 2] | (uri_buffer
[(i
* 2) + 1] << 8));
155 uri
= new string (uri_array
);
160 if (msg_type
== MessageType
.Exception
)
161 throw new RemotingException ("\n" + uri
+ "\n" + "Rethrown at:\n");
163 byte [] stream_buffer
= new byte [data_len
];
164 if ((network_stream
.Read (stream_buffer
, 0, data_len
)) != data_len
)
165 throw new Exception ("packet size error");
167 return new MemoryStream (stream_buffer
, false);