blob: 2e86a7080903ff3a06d6a338a3a596ff2f50be55 [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Todd Lipcon53ae9f32009-12-07 00:42:38 +000018 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
Kevin Clarkab4460d2009-03-20 02:28:41 +000022 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
24using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using System.Text;
26using Thrift.Transport;
27
28namespace Thrift.Protocol
29{
30 public class TBinaryProtocol : TProtocol
31 {
32 protected const uint VERSION_MASK = 0xffff0000;
33 protected const uint VERSION_1 = 0x80010000;
34
35 protected bool strictRead_ = false;
36 protected bool strictWrite_ = true;
37
David Reiss7f42bcf2008-01-11 20:59:12 +000038 #region BinaryProtocol Factory
39 /**
40 * Factory
41 */
42 public class Factory : TProtocolFactory {
43
44 protected bool strictRead_ = false;
45 protected bool strictWrite_ = true;
46
47 public Factory()
48 :this(false, true)
49 {
50 }
51
52 public Factory(bool strictRead, bool strictWrite)
53 {
54 strictRead_ = strictRead;
55 strictWrite_ = strictWrite;
56 }
57
58 public TProtocol GetProtocol(TTransport trans) {
59 return new TBinaryProtocol(trans, strictRead_, strictWrite_);
60 }
61 }
62
63 #endregion
64
65 public TBinaryProtocol(TTransport trans)
66 : this(trans, false, true)
67 {
68 }
69
70 public TBinaryProtocol(TTransport trans, bool strictRead, bool strictWrite)
71 :base(trans)
72 {
73 strictRead_ = strictRead;
74 strictWrite_ = strictWrite;
75 }
76
77 #region Write Methods
78
79 public override void WriteMessageBegin(TMessage message)
80 {
81 if (strictWrite_)
82 {
83 uint version = VERSION_1 | (uint)(message.Type);
84 WriteI32((int)version);
85 WriteString(message.Name);
86 WriteI32(message.SeqID);
87 }
88 else
89 {
90 WriteString(message.Name);
Jens Geyerf509df92013-04-25 20:38:55 +020091 WriteByte((sbyte)message.Type);
David Reiss7f42bcf2008-01-11 20:59:12 +000092 WriteI32(message.SeqID);
93 }
94 }
95
96 public override void WriteMessageEnd()
97 {
98 }
99
100 public override void WriteStructBegin(TStruct struc)
101 {
102 }
103
104 public override void WriteStructEnd()
105 {
106 }
107
108 public override void WriteFieldBegin(TField field)
109 {
Jens Geyerf509df92013-04-25 20:38:55 +0200110 WriteByte((sbyte)field.Type);
David Reiss7f42bcf2008-01-11 20:59:12 +0000111 WriteI16(field.ID);
112 }
113
114 public override void WriteFieldEnd()
115 {
116 }
117
118 public override void WriteFieldStop()
119 {
Jens Geyerf509df92013-04-25 20:38:55 +0200120 WriteByte((sbyte)TType.Stop);
David Reiss7f42bcf2008-01-11 20:59:12 +0000121 }
122
123 public override void WriteMapBegin(TMap map)
124 {
Jens Geyerf509df92013-04-25 20:38:55 +0200125 WriteByte((sbyte)map.KeyType);
126 WriteByte((sbyte)map.ValueType);
David Reiss7f42bcf2008-01-11 20:59:12 +0000127 WriteI32(map.Count);
128 }
129
130 public override void WriteMapEnd()
131 {
132 }
133
134 public override void WriteListBegin(TList list)
135 {
Jens Geyerf509df92013-04-25 20:38:55 +0200136 WriteByte((sbyte)list.ElementType);
David Reiss7f42bcf2008-01-11 20:59:12 +0000137 WriteI32(list.Count);
138 }
139
140 public override void WriteListEnd()
141 {
142 }
143
144 public override void WriteSetBegin(TSet set)
145 {
Jens Geyerf509df92013-04-25 20:38:55 +0200146 WriteByte((sbyte)set.ElementType);
David Reiss7f42bcf2008-01-11 20:59:12 +0000147 WriteI32(set.Count);
148 }
149
150 public override void WriteSetEnd()
151 {
152 }
153
154 public override void WriteBool(bool b)
155 {
Jens Geyerf509df92013-04-25 20:38:55 +0200156 WriteByte(b ? (sbyte)1 : (sbyte)0);
David Reiss7f42bcf2008-01-11 20:59:12 +0000157 }
158
159 private byte[] bout = new byte[1];
Jens Geyerf509df92013-04-25 20:38:55 +0200160 public override void WriteByte(sbyte b)
David Reiss7f42bcf2008-01-11 20:59:12 +0000161 {
Jens Geyerf509df92013-04-25 20:38:55 +0200162 bout[0] = (byte)b;
David Reiss7f42bcf2008-01-11 20:59:12 +0000163 trans.Write(bout, 0, 1);
164 }
165
166 private byte[] i16out = new byte[2];
167 public override void WriteI16(short s)
168 {
169 i16out[0] = (byte)(0xff & (s >> 8));
170 i16out[1] = (byte)(0xff & s);
171 trans.Write(i16out, 0, 2);
172 }
173
174 private byte[] i32out = new byte[4];
175 public override void WriteI32(int i32)
176 {
177 i32out[0] = (byte)(0xff & (i32 >> 24));
178 i32out[1] = (byte)(0xff & (i32 >> 16));
179 i32out[2] = (byte)(0xff & (i32 >> 8));
180 i32out[3] = (byte)(0xff & i32);
181 trans.Write(i32out, 0, 4);
182 }
183
184 private byte[] i64out = new byte[8];
185 public override void WriteI64(long i64)
186 {
187 i64out[0] = (byte)(0xff & (i64 >> 56));
188 i64out[1] = (byte)(0xff & (i64 >> 48));
189 i64out[2] = (byte)(0xff & (i64 >> 40));
190 i64out[3] = (byte)(0xff & (i64 >> 32));
191 i64out[4] = (byte)(0xff & (i64 >> 24));
192 i64out[5] = (byte)(0xff & (i64 >> 16));
193 i64out[6] = (byte)(0xff & (i64 >> 8));
194 i64out[7] = (byte)(0xff & i64);
195 trans.Write(i64out, 0, 8);
196 }
197
198 public override void WriteDouble(double d)
199 {
Roger Meier284a9b52011-12-08 13:39:56 +0000200#if !SILVERLIGHT
David Reiss7f42bcf2008-01-11 20:59:12 +0000201 WriteI64(BitConverter.DoubleToInt64Bits(d));
Roger Meier284a9b52011-12-08 13:39:56 +0000202#else
203 var bytes = BitConverter.GetBytes(d);
204 WriteI64(BitConverter.ToInt64(bytes, 0));
205#endif
David Reiss7f42bcf2008-01-11 20:59:12 +0000206 }
207
David Reisscba57272008-02-06 22:09:44 +0000208 public override void WriteBinary(byte[] b)
David Reiss7f42bcf2008-01-11 20:59:12 +0000209 {
David Reiss7f42bcf2008-01-11 20:59:12 +0000210 WriteI32(b.Length);
211 trans.Write(b, 0, b.Length);
212 }
213
214 #endregion
215
216 #region ReadMethods
217
218 public override TMessage ReadMessageBegin()
219 {
220 TMessage message = new TMessage();
221 int size = ReadI32();
222 if (size < 0)
223 {
224 uint version = (uint)size & VERSION_MASK;
225 if (version != VERSION_1)
226 {
227 throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in ReadMessageBegin: " + version);
228 }
229 message.Type = (TMessageType)(size & 0x000000ff);
230 message.Name = ReadString();
231 message.SeqID = ReadI32();
232 }
233 else
234 {
235 if (strictRead_)
236 {
237 throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?");
238 }
239 message.Name = ReadStringBody(size);
240 message.Type = (TMessageType)ReadByte();
241 message.SeqID = ReadI32();
242 }
243 return message;
244 }
245
246 public override void ReadMessageEnd()
247 {
248 }
249
250 public override TStruct ReadStructBegin()
251 {
252 return new TStruct();
253 }
254
255 public override void ReadStructEnd()
256 {
257 }
258
259 public override TField ReadFieldBegin()
260 {
261 TField field = new TField();
262 field.Type = (TType)ReadByte();
263
264 if (field.Type != TType.Stop)
265 {
266 field.ID = ReadI16();
267 }
268
269 return field;
270 }
271
272 public override void ReadFieldEnd()
273 {
274 }
275
276 public override TMap ReadMapBegin()
277 {
278 TMap map = new TMap();
279 map.KeyType = (TType)ReadByte();
280 map.ValueType = (TType)ReadByte();
281 map.Count = ReadI32();
282
283 return map;
284 }
285
286 public override void ReadMapEnd()
287 {
288 }
289
290 public override TList ReadListBegin()
291 {
292 TList list = new TList();
293 list.ElementType = (TType)ReadByte();
294 list.Count = ReadI32();
295
296 return list;
297 }
298
299 public override void ReadListEnd()
300 {
301 }
302
303 public override TSet ReadSetBegin()
304 {
305 TSet set = new TSet();
306 set.ElementType = (TType)ReadByte();
307 set.Count = ReadI32();
308
309 return set;
310 }
311
312 public override void ReadSetEnd()
313 {
314 }
315
316 public override bool ReadBool()
317 {
318 return ReadByte() == 1;
319 }
320
321 private byte[] bin = new byte[1];
Jens Geyerf509df92013-04-25 20:38:55 +0200322 public override sbyte ReadByte()
David Reiss7f42bcf2008-01-11 20:59:12 +0000323 {
324 ReadAll(bin, 0, 1);
Jens Geyerf509df92013-04-25 20:38:55 +0200325 return (sbyte)bin[0];
David Reiss7f42bcf2008-01-11 20:59:12 +0000326 }
327
328 private byte[] i16in = new byte[2];
329 public override short ReadI16()
330 {
331 ReadAll(i16in, 0, 2);
332 return (short)(((i16in[0] & 0xff) << 8) | ((i16in[1] & 0xff)));
333 }
334
335 private byte[] i32in = new byte[4];
336 public override int ReadI32()
337 {
338 ReadAll(i32in, 0, 4);
339 return (int)(((i32in[0] & 0xff) << 24) | ((i32in[1] & 0xff) << 16) | ((i32in[2] & 0xff) << 8) | ((i32in[3] & 0xff)));
Jens Geyer1c99e702014-03-17 22:50:39 +0200340 }
341
342#pragma warning disable 675
343
Jens Geyeree353e62013-07-06 09:28:49 +0200344 private byte[] i64in = new byte[8];
David Reiss7f42bcf2008-01-11 20:59:12 +0000345 public override long ReadI64()
346 {
Jens Geyer1c99e702014-03-17 22:50:39 +0200347 ReadAll(i64in, 0, 8);
Jake Farrell98f93772012-10-20 16:47:51 +0000348 unchecked {
349 return (long)(
350 ((long)(i64in[0] & 0xff) << 56) |
351 ((long)(i64in[1] & 0xff) << 48) |
352 ((long)(i64in[2] & 0xff) << 40) |
353 ((long)(i64in[3] & 0xff) << 32) |
354 ((long)(i64in[4] & 0xff) << 24) |
355 ((long)(i64in[5] & 0xff) << 16) |
356 ((long)(i64in[6] & 0xff) << 8) |
357 ((long)(i64in[7] & 0xff)));
358 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200359 }
360
361#pragma warning restore 675
362
Jens Geyeree353e62013-07-06 09:28:49 +0200363 public override double ReadDouble()
David Reiss7f42bcf2008-01-11 20:59:12 +0000364 {
Roger Meier284a9b52011-12-08 13:39:56 +0000365#if !SILVERLIGHT
David Reiss7f42bcf2008-01-11 20:59:12 +0000366 return BitConverter.Int64BitsToDouble(ReadI64());
Roger Meier284a9b52011-12-08 13:39:56 +0000367#else
368 var value = ReadI64();
369 var bytes = BitConverter.GetBytes(value);
370 return BitConverter.ToDouble(bytes, 0);
371#endif
David Reiss7f42bcf2008-01-11 20:59:12 +0000372 }
373
David Reisscba57272008-02-06 22:09:44 +0000374 public override byte[] ReadBinary()
David Reiss7f42bcf2008-01-11 20:59:12 +0000375 {
376 int size = ReadI32();
David Reisscba57272008-02-06 22:09:44 +0000377 byte[] buf = new byte[size];
378 trans.ReadAll(buf, 0, size);
379 return buf;
David Reiss7f42bcf2008-01-11 20:59:12 +0000380 }
David Reisscba57272008-02-06 22:09:44 +0000381 private string ReadStringBody(int size)
David Reiss7f42bcf2008-01-11 20:59:12 +0000382 {
David Reiss7f42bcf2008-01-11 20:59:12 +0000383 byte[] buf = new byte[size];
384 trans.ReadAll(buf, 0, size);
Roger Meier284a9b52011-12-08 13:39:56 +0000385 return Encoding.UTF8.GetString(buf, 0, buf.Length);
David Reiss7f42bcf2008-01-11 20:59:12 +0000386 }
387
388 private int ReadAll(byte[] buf, int off, int len)
389 {
David Reiss7f42bcf2008-01-11 20:59:12 +0000390 return trans.ReadAll(buf, off, len);
391 }
392
393 #endregion
394 }
395}