summaryrefslogtreecommitdiff
path: root/lib/csharp/src/Protocol/TBinaryProtocol.cs
blob: 14ca43b745b180a4606b90721546b2a581841435 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

using System;
using System.Text;
using Thrift.Transport;

namespace Thrift.Protocol
{
	public class TBinaryProtocol : TProtocol
	{
		protected const uint VERSION_MASK = 0xffff0000;
		protected const uint VERSION_1 = 0x80010000;

		protected bool strictRead_ = false;
		protected bool strictWrite_ = true;

		protected int readLength_;
		protected bool checkReadLength_ = false;


		#region BinaryProtocol Factory
		 /**
		  * Factory
		  */
		  public class Factory : TProtocolFactory {

			  protected bool strictRead_ = false;
			  protected bool strictWrite_ = true;

			  public Factory()
				  :this(false, true)
			  {
			  }

			  public Factory(bool strictRead, bool strictWrite)
			  {
				  strictRead_ = strictRead;
				  strictWrite_ = strictWrite;
			  }

			public TProtocol GetProtocol(TTransport trans) {
			  return new TBinaryProtocol(trans, strictRead_, strictWrite_);
			}
		  }

		#endregion

		public TBinaryProtocol(TTransport trans)
			: this(trans, false, true)
		{
		}

		public TBinaryProtocol(TTransport trans, bool strictRead, bool strictWrite)
			:base(trans)
		{
			strictRead_ = strictRead;
			strictWrite_ = strictWrite;
		}

		#region Write Methods

		public override void WriteMessageBegin(TMessage message)
		{
			if (strictWrite_)
			{
				uint version = VERSION_1 | (uint)(message.Type);
				WriteI32((int)version);
				WriteString(message.Name);
				WriteI32(message.SeqID);
			}
			else
			{
				WriteString(message.Name);
				WriteByte((byte)message.Type);
				WriteI32(message.SeqID);
			}
		}

		public override void WriteMessageEnd()
		{
		}

		public override void WriteStructBegin(TStruct struc)
		{
		}

		public override void WriteStructEnd()
		{
		}

		public override void WriteFieldBegin(TField field)
		{
			WriteByte((byte)field.Type);
			WriteI16(field.ID);
		}

		public override void WriteFieldEnd()
		{
		}

		public override void WriteFieldStop()
		{
			WriteByte((byte)TType.Stop);
		}

		public override void WriteMapBegin(TMap map)
		{
			WriteByte((byte)map.KeyType);
			WriteByte((byte)map.ValueType);
			WriteI32(map.Count);
		}

		public override void WriteMapEnd()
		{
		}

		public override void WriteListBegin(TList list)
		{
			WriteByte((byte)list.ElementType);
			WriteI32(list.Count);
		}

		public override void WriteListEnd()
		{
		}

		public override void WriteSetBegin(TSet set)
		{
			WriteByte((byte)set.ElementType);
			WriteI32(set.Count);
		}

		public override void WriteSetEnd()
		{
		}

		public override void WriteBool(bool b)
		{
			WriteByte(b ? (byte)1 : (byte)0);
		}

		private byte[] bout = new byte[1];
		public override void WriteByte(byte b)
		{
			bout[0] = b;
			trans.Write(bout, 0, 1);
		}

		private byte[] i16out = new byte[2];
		public override void WriteI16(short s)
		{
			i16out[0] = (byte)(0xff & (s >> 8));
			i16out[1] = (byte)(0xff & s);
			trans.Write(i16out, 0, 2);
		}

		private byte[] i32out = new byte[4];
		public override void WriteI32(int i32)
		{
			i32out[0] = (byte)(0xff & (i32 >> 24));
			i32out[1] = (byte)(0xff & (i32 >> 16));
			i32out[2] = (byte)(0xff & (i32 >> 8));
			i32out[3] = (byte)(0xff & i32);
			trans.Write(i32out, 0, 4);
		}

		private byte[] i64out = new byte[8];
		public override void WriteI64(long i64)
		{
			i64out[0] = (byte)(0xff & (i64 >> 56));
			i64out[1] = (byte)(0xff & (i64 >> 48));
			i64out[2] = (byte)(0xff & (i64 >> 40));
			i64out[3] = (byte)(0xff & (i64 >> 32));
			i64out[4] = (byte)(0xff & (i64 >> 24));
			i64out[5] = (byte)(0xff & (i64 >> 16));
			i64out[6] = (byte)(0xff & (i64 >> 8));
			i64out[7] = (byte)(0xff & i64);
			trans.Write(i64out, 0, 8);
		}

		public override void WriteDouble(double d)
		{
			WriteI64(BitConverter.DoubleToInt64Bits(d));
		}

		public override void WriteBinary(byte[] b)
		{
			WriteI32(b.Length);
			trans.Write(b, 0, b.Length);
		}

		#endregion

		#region ReadMethods

		public override TMessage ReadMessageBegin()
		{
			TMessage message = new TMessage();
			int size = ReadI32();
			if (size < 0)
			{
				uint version = (uint)size & VERSION_MASK;
				if (version != VERSION_1)
				{
					throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in ReadMessageBegin: " + version);
				}
				message.Type = (TMessageType)(size & 0x000000ff);
				message.Name = ReadString();
				message.SeqID = ReadI32();
			}
			else
			{
				if (strictRead_)
				{
					throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?");
				}
				message.Name = ReadStringBody(size);
				message.Type = (TMessageType)ReadByte();
				message.SeqID = ReadI32();
			}
			return message;
		}

		public override void ReadMessageEnd()
		{
		}

		public override TStruct ReadStructBegin()
		{
			return new TStruct();
		}

		public override void ReadStructEnd()
		{
		}

		public override TField ReadFieldBegin()
		{
			TField field = new TField();
			field.Type = (TType)ReadByte();

			if (field.Type != TType.Stop)
			{
				field.ID = ReadI16();
			}

			return field;
		}

		public override void ReadFieldEnd()
		{
		}

		public override TMap ReadMapBegin()
		{
			TMap map = new TMap();
			map.KeyType = (TType)ReadByte();
			map.ValueType = (TType)ReadByte();
			map.Count = ReadI32();

			return map;
		}

		public override void ReadMapEnd()
		{
		}

		public override TList ReadListBegin()
		{
			TList list = new TList();
			list.ElementType = (TType)ReadByte();
			list.Count = ReadI32();

			return list;
		}

		public override void ReadListEnd()
		{
		}

		public override TSet ReadSetBegin()
		{
			TSet set = new TSet();
			set.ElementType = (TType)ReadByte();
			set.Count = ReadI32();

			return set;
		}

		public override void ReadSetEnd()
		{
		}

		public override bool ReadBool()
		{
			return ReadByte() == 1;
		}

		private byte[] bin = new byte[1];
		public override byte ReadByte()
		{
			ReadAll(bin, 0, 1);
			return bin[0];
		}

		private byte[] i16in = new byte[2];
		public override short ReadI16()
		{
			ReadAll(i16in, 0, 2);
			return (short)(((i16in[0] & 0xff) << 8) | ((i16in[1] & 0xff)));
		}

		private byte[] i32in = new byte[4];
		public override int ReadI32()
		{
			ReadAll(i32in, 0, 4);
			return (int)(((i32in[0] & 0xff) << 24) | ((i32in[1] & 0xff) << 16) | ((i32in[2] & 0xff) << 8) | ((i32in[3] & 0xff)));
		}

		private byte[] i64in = new byte[8];
		public override long ReadI64()
		{
			ReadAll(i64in, 0, 8);
			return (long)(((long)(i64in[0] & 0xff) << 56) | ((long)(i64in[1] & 0xff) << 48) | ((long)(i64in[2] & 0xff) << 40) | ((long)(i64in[3] & 0xff) << 32) |
				((long)(i64in[4] & 0xff) << 24) | ((long)(i64in[5] & 0xff) << 16) | ((long)(i64in[6] & 0xff) << 8) | ((long)(i64in[7] & 0xff)));
		}

		public override double ReadDouble()
		{
			return BitConverter.Int64BitsToDouble(ReadI64());
		}

		public void SetReadLength(int readLength)
		{
			readLength_ = readLength;
			checkReadLength_ = true;
		}

		protected void CheckReadLength(int length)
		{
			if (checkReadLength_)
			{
				readLength_ -= length;
				if (readLength_ < 0)
				{
					throw new Exception("Message length exceeded: " + length);
				}
			}
		}

		public override byte[] ReadBinary()
		{
			int size = ReadI32();
			CheckReadLength(size);
			byte[] buf = new byte[size];
			trans.ReadAll(buf, 0, size);
			return buf;
		}
		private  string ReadStringBody(int size)
		{
			CheckReadLength(size);
			byte[] buf = new byte[size];
			trans.ReadAll(buf, 0, size);
			return Encoding.UTF8.GetString(buf);
		}

		private int ReadAll(byte[] buf, int off, int len)
		{
			CheckReadLength(len);
			return trans.ReadAll(buf, off, len);
		}

		#endregion
	}
}