File: TcpMessageIO.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (298 lines) | stat: -rw-r--r-- 9,119 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// System.Runtime.Remoting.Channels.Tcp.TcpMessageIO.cs
//
// Author: Lluis Sanchez Gual (lluis@ideary.com)
//
// (C) 2002 Lluis Sanchez Gual

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace System.Runtime.Remoting.Channels.Tcp 
{
	enum MessageStatus { MethodMessage = 0, CancelSignal = 1, Unknown = 10}

	internal class TcpMessageIO
	{
		static byte[][] _msgHeaders = 
			  {
				  new byte[] { (byte)'.', (byte)'N', (byte)'E', (byte)'T', 1, 0 },
				  new byte[] { 255, 255, 255, 255, 255, 255 }
			  };
							
		public static int DefaultStreamBufferSize = 1000;

		// Identifies an incoming message
		public static MessageStatus ReceiveMessageStatus (Stream networkStream, byte[] buffer)
		{
			try {
				StreamRead (networkStream, buffer, 6);
			} catch (Exception ex) {
				throw new RemotingException ("Tcp transport error.", ex);
			}

			try
			{
				bool[] isOnTrack = new bool[_msgHeaders.Length];
				bool atLeastOneOnTrack = true;
				int i = 0;

				while (atLeastOneOnTrack)
				{
					atLeastOneOnTrack = false;
					byte c = buffer [i];
					for (int n = 0; n<_msgHeaders.Length; n++)
					{
						if (i > 0 && !isOnTrack[n]) continue;

						isOnTrack[n] = (c == _msgHeaders[n][i]);
						if (isOnTrack[n] && (i == _msgHeaders[n].Length-1)) return (MessageStatus) n;
						atLeastOneOnTrack = atLeastOneOnTrack || isOnTrack[n];
					}
					i++;
				}
				return MessageStatus.Unknown;
			}
			catch (Exception ex) {
				throw new RemotingException ("Tcp transport error.", ex);
			}
		}
		
		static bool StreamRead (Stream networkStream, byte[] buffer, int count)
		{
			int nr = 0;
			do {
				int pr = networkStream.Read (buffer, nr, count - nr);
				if (pr == 0)
					throw new RemotingException ("Connection closed");
				nr += pr;
			} while (nr < count);
			return true;
		}

		public static void SendMessageStream (Stream networkStream, Stream data, ITransportHeaders requestHeaders, byte[] buffer)
		{
			SendMessageStream (networkStream, data, requestHeaders, buffer, false);
		}

		public static void SendMessageStream (Stream networkStream, Stream data, ITransportHeaders requestHeaders, byte[] buffer, bool isOneWay)
		{
			if (buffer == null) buffer = new byte[DefaultStreamBufferSize];

			// Writes the message start header
			byte[] dotnetHeader = _msgHeaders[(int) MessageStatus.MethodMessage];
			networkStream.Write(dotnetHeader, 0, dotnetHeader.Length);

			// Writes the header tag
			// 0x0000 - request stream
			// 0x0001 - OneWay request stream
			// 0x0002 - response stream
			if(requestHeaders[CommonTransportKeys.RequestUri]!=null) {
				buffer [0] = isOneWay ? (byte) 1 : (byte) 0;
			}
			else {
				buffer[0] = (byte) 2;
			}
			buffer [1] = (byte) 0 ;

			// Writes ID
			buffer [2] = (byte) 0;

			// Writes assemblyID????
			buffer [3] = (byte) 0;

			// Writes the length of the stream being sent (not including the headers)
			int num = (int)data.Length;
			buffer [4] = (byte) num;
			buffer [5] = (byte) (num >> 8);
			buffer [6] = (byte) (num >> 16);
			buffer [7] = (byte) (num >> 24);
			networkStream.Write(buffer, 0, 8);
	
			// Writes the message headers
			SendHeaders (networkStream, requestHeaders, buffer);

			if (data.Length == 0)
				return;

			// Writes the stream
			if (data is MemoryStream)
			{
				// The copy of the stream can be optimized. The internal
				// buffer of MemoryStream can be used.
				MemoryStream memStream = (MemoryStream)data;
				networkStream.Write (memStream.GetBuffer(), 0, (int)memStream.Length);
			}
			else
			{
				int nread = data.Read (buffer, 0, buffer.Length);
				while (nread > 0)
				{
					networkStream.Write (buffer, 0, nread);
					nread = data.Read (buffer, 0, buffer.Length);
				}
			}
		}
		
		static byte[] msgUriTransportKey = new byte[] { 4, 0, 1, 1 };
		static byte[] msgContentTypeTransportKey = new byte[] { 6, 0, 1, 1 };
		static byte[] msgDefaultTransportKey = new byte[] { 1, 0, 1 };
		static byte[] msgHeaderTerminator = new byte[] { 0, 0 };

		private static void SendHeaders(Stream networkStream, ITransportHeaders requestHeaders, byte[] buffer)
		{
			// Writes the headers as a sequence of strings
			if (networkStream != null)
			{
				IEnumerator e = requestHeaders.GetEnumerator();
				while (e.MoveNext())
				{
					DictionaryEntry hdr = (DictionaryEntry)e.Current;
					switch (hdr.Key.ToString())
					{
						case CommonTransportKeys.RequestUri: 
							networkStream.Write (msgUriTransportKey, 0, 4);
							break;
						case "Content-Type": 
							networkStream.Write (msgContentTypeTransportKey, 0, 4);
							break;
						default: 
							networkStream.Write (msgDefaultTransportKey, 0, 3);
							SendString (networkStream, hdr.Key.ToString(), buffer);
							networkStream.WriteByte (1);
							break;
					}
					SendString (networkStream, hdr.Value.ToString(), buffer);
				}
			}
			networkStream.Write (msgHeaderTerminator, 0, 2);	// End of headers
		}
		
		public static ITransportHeaders ReceiveHeaders (Stream networkStream, byte[] buffer)
		{
			StreamRead (networkStream, buffer, 2);
			
			byte headerType = buffer [0];
			TransportHeaders headers = new TransportHeaders ();

			while (headerType != 0)
			{
				string key;
				StreamRead (networkStream, buffer, 1);	// byte 1
				switch (headerType)
				{
					case 4: key = CommonTransportKeys.RequestUri; break;
					case 6: key = "Content-Type"; break;
					case 1: key = ReceiveString (networkStream, buffer); break;
					default: throw new NotSupportedException ("Unknown header code: " + headerType);
				}
				StreamRead (networkStream, buffer, 1);	// byte 1
				headers[key] = ReceiveString (networkStream, buffer);

				StreamRead (networkStream, buffer, 2);
				headerType = buffer [0];
			}

			return headers;
		}
		
		public static Stream ReceiveMessageStream (Stream networkStream, out ITransportHeaders headers, byte[] buffer)
		{
			headers = null;

			if (buffer == null) buffer = new byte[DefaultStreamBufferSize];

			// Reads header tag:  0 -> Stream with headers or 2 -> Response Stream
			// +
			// Gets the length of the data stream
			StreamRead (networkStream, buffer, 8);

			int byteCount = (buffer [4] | (buffer [5] << 8) |
				(buffer [6] << 16) | (buffer [7] << 24));

			// Reads the headers
			headers = ReceiveHeaders (networkStream, buffer);

			if (byteCount > 0) {
				byte[] resultBuffer = new byte[byteCount];
				StreamRead (networkStream, resultBuffer, byteCount);
				return new MemoryStream (resultBuffer);
			} else {
				return new MemoryStream ();
			}
		}		

		private static void SendString (Stream networkStream, string str, byte[] buffer)
		{
			// Allocates a buffer. Use the internal buffer if it is 
			// big enough. If not, create a new one.

			int maxBytes = Encoding.UTF8.GetMaxByteCount(str.Length)+4;	//+4 bytes for storing the string length
			if (maxBytes > buffer.Length)
				buffer = new byte[maxBytes];

			int num = Encoding.UTF8.GetBytes (str, 0, str.Length, buffer, 4);

			// store number of bytes (not number of chars!)

			buffer [0] = (byte) num;
			buffer [1] = (byte) (num >> 8);
			buffer [2] = (byte) (num >> 16);
			buffer [3] = (byte) (num >> 24);

			// Write the string bytes
			networkStream.Write (buffer, 0, num + 4);
		}

		private static string ReceiveString (Stream networkStream, byte[] buffer)
		{
			StreamRead (networkStream, buffer, 4);

			// Reads the number of bytes (not chars!)

			int byteCount = (buffer [0] | (buffer [1] << 8) |
				(buffer [2] << 16) | (buffer [3] << 24));

			if (byteCount == 0) return string.Empty;

			// Allocates a buffer of the correct size. Use the
			// internal buffer if it is big enough

			if (byteCount > buffer.Length)
				buffer = new byte[byteCount];

			// Reads the string

			StreamRead (networkStream, buffer, byteCount);
			char[] chars = Encoding.UTF8.GetChars (buffer, 0, byteCount);
	
			return new string (chars);
		}
		
	}
}