blob: 36f03d0e2438c5a05d93879d0e4bc55e449991f5 [file] [log] [blame]
David Reiss7f42bcf2008-01-11 20:59:12 +00001//
2// TBinaryProtocol.cs
3//
4// Begin: Aug 19, 2007
5// Authors:
6// Todd Berman <tberman@imeem.com>
7// Will Palmeri <will@imeem.com>
8//
9// Distributed under the Thrift Software License
10//
11// See accompanying file LICENSE or visit the Thrift site at:
12// http://developers.facebook.com/thrift/using
13
14using System;
15using System.Collections.Generic;
16using System.Text;
17using Thrift.Transport;
18
19namespace Thrift.Protocol
20{
21 public class TBinaryProtocol : TProtocol
22 {
23 protected const uint VERSION_MASK = 0xffff0000;
24 protected const uint VERSION_1 = 0x80010000;
25
26 protected bool strictRead_ = false;
27 protected bool strictWrite_ = true;
28
29 protected int readLength_;
30 protected bool checkReadLength_ = false;
31
32
33 #region BinaryProtocol Factory
34 /**
35 * Factory
36 */
37 public class Factory : TProtocolFactory {
38
39 protected bool strictRead_ = false;
40 protected bool strictWrite_ = true;
41
42 public Factory()
43 :this(false, true)
44 {
45 }
46
47 public Factory(bool strictRead, bool strictWrite)
48 {
49 strictRead_ = strictRead;
50 strictWrite_ = strictWrite;
51 }
52
53 public TProtocol GetProtocol(TTransport trans) {
54 return new TBinaryProtocol(trans, strictRead_, strictWrite_);
55 }
56 }
57
58 #endregion
59
60 public TBinaryProtocol(TTransport trans)
61 : this(trans, false, true)
62 {
63 }
64
65 public TBinaryProtocol(TTransport trans, bool strictRead, bool strictWrite)
66 :base(trans)
67 {
68 strictRead_ = strictRead;
69 strictWrite_ = strictWrite;
70 }
71
72 #region Write Methods
73
74 public override void WriteMessageBegin(TMessage message)
75 {
76 if (strictWrite_)
77 {
78 uint version = VERSION_1 | (uint)(message.Type);
79 WriteI32((int)version);
80 WriteString(message.Name);
81 WriteI32(message.SeqID);
82 }
83 else
84 {
85 WriteString(message.Name);
86 WriteByte((byte)message.Type);
87 WriteI32(message.SeqID);
88 }
89 }
90
91 public override void WriteMessageEnd()
92 {
93 }
94
95 public override void WriteStructBegin(TStruct struc)
96 {
97 }
98
99 public override void WriteStructEnd()
100 {
101 }
102
103 public override void WriteFieldBegin(TField field)
104 {
105 WriteByte((byte)field.Type);
106 WriteI16(field.ID);
107 }
108
109 public override void WriteFieldEnd()
110 {
111 }
112
113 public override void WriteFieldStop()
114 {
115 WriteByte((byte)TType.Stop);
116 }
117
118 public override void WriteMapBegin(TMap map)
119 {
120 WriteByte((byte)map.KeyType);
121 WriteByte((byte)map.ValueType);
122 WriteI32(map.Count);
123 }
124
125 public override void WriteMapEnd()
126 {
127 }
128
129 public override void WriteListBegin(TList list)
130 {
131 WriteByte((byte)list.ElementType);
132 WriteI32(list.Count);
133 }
134
135 public override void WriteListEnd()
136 {
137 }
138
139 public override void WriteSetBegin(TSet set)
140 {
141 WriteByte((byte)set.ElementType);
142 WriteI32(set.Count);
143 }
144
145 public override void WriteSetEnd()
146 {
147 }
148
149 public override void WriteBool(bool b)
150 {
151 WriteByte(b ? (byte)1 : (byte)0);
152 }
153
154 private byte[] bout = new byte[1];
155 public override void WriteByte(byte b)
156 {
157 bout[0] = b;
158 trans.Write(bout, 0, 1);
159 }
160
161 private byte[] i16out = new byte[2];
162 public override void WriteI16(short s)
163 {
164 i16out[0] = (byte)(0xff & (s >> 8));
165 i16out[1] = (byte)(0xff & s);
166 trans.Write(i16out, 0, 2);
167 }
168
169 private byte[] i32out = new byte[4];
170 public override void WriteI32(int i32)
171 {
172 i32out[0] = (byte)(0xff & (i32 >> 24));
173 i32out[1] = (byte)(0xff & (i32 >> 16));
174 i32out[2] = (byte)(0xff & (i32 >> 8));
175 i32out[3] = (byte)(0xff & i32);
176 trans.Write(i32out, 0, 4);
177 }
178
179 private byte[] i64out = new byte[8];
180 public override void WriteI64(long i64)
181 {
182 i64out[0] = (byte)(0xff & (i64 >> 56));
183 i64out[1] = (byte)(0xff & (i64 >> 48));
184 i64out[2] = (byte)(0xff & (i64 >> 40));
185 i64out[3] = (byte)(0xff & (i64 >> 32));
186 i64out[4] = (byte)(0xff & (i64 >> 24));
187 i64out[5] = (byte)(0xff & (i64 >> 16));
188 i64out[6] = (byte)(0xff & (i64 >> 8));
189 i64out[7] = (byte)(0xff & i64);
190 trans.Write(i64out, 0, 8);
191 }
192
193 public override void WriteDouble(double d)
194 {
195 WriteI64(BitConverter.DoubleToInt64Bits(d));
196 }
197
198 public override void WriteString(string s)
199 {
200 byte[] b = Encoding.UTF8.GetBytes(s);
201 WriteI32(b.Length);
202 trans.Write(b, 0, b.Length);
203 }
204
205 #endregion
206
207 #region ReadMethods
208
209 public override TMessage ReadMessageBegin()
210 {
211 TMessage message = new TMessage();
212 int size = ReadI32();
213 if (size < 0)
214 {
215 uint version = (uint)size & VERSION_MASK;
216 if (version != VERSION_1)
217 {
218 throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in ReadMessageBegin: " + version);
219 }
220 message.Type = (TMessageType)(size & 0x000000ff);
221 message.Name = ReadString();
222 message.SeqID = ReadI32();
223 }
224 else
225 {
226 if (strictRead_)
227 {
228 throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?");
229 }
230 message.Name = ReadStringBody(size);
231 message.Type = (TMessageType)ReadByte();
232 message.SeqID = ReadI32();
233 }
234 return message;
235 }
236
237 public override void ReadMessageEnd()
238 {
239 }
240
241 public override TStruct ReadStructBegin()
242 {
243 return new TStruct();
244 }
245
246 public override void ReadStructEnd()
247 {
248 }
249
250 public override TField ReadFieldBegin()
251 {
252 TField field = new TField();
253 field.Type = (TType)ReadByte();
254
255 if (field.Type != TType.Stop)
256 {
257 field.ID = ReadI16();
258 }
259
260 return field;
261 }
262
263 public override void ReadFieldEnd()
264 {
265 }
266
267 public override TMap ReadMapBegin()
268 {
269 TMap map = new TMap();
270 map.KeyType = (TType)ReadByte();
271 map.ValueType = (TType)ReadByte();
272 map.Count = ReadI32();
273
274 return map;
275 }
276
277 public override void ReadMapEnd()
278 {
279 }
280
281 public override TList ReadListBegin()
282 {
283 TList list = new TList();
284 list.ElementType = (TType)ReadByte();
285 list.Count = ReadI32();
286
287 return list;
288 }
289
290 public override void ReadListEnd()
291 {
292 }
293
294 public override TSet ReadSetBegin()
295 {
296 TSet set = new TSet();
297 set.ElementType = (TType)ReadByte();
298 set.Count = ReadI32();
299
300 return set;
301 }
302
303 public override void ReadSetEnd()
304 {
305 }
306
307 public override bool ReadBool()
308 {
309 return ReadByte() == 1;
310 }
311
312 private byte[] bin = new byte[1];
313 public override byte ReadByte()
314 {
315 ReadAll(bin, 0, 1);
316 return bin[0];
317 }
318
319 private byte[] i16in = new byte[2];
320 public override short ReadI16()
321 {
322 ReadAll(i16in, 0, 2);
323 return (short)(((i16in[0] & 0xff) << 8) | ((i16in[1] & 0xff)));
324 }
325
326 private byte[] i32in = new byte[4];
327 public override int ReadI32()
328 {
329 ReadAll(i32in, 0, 4);
330 return (int)(((i32in[0] & 0xff) << 24) | ((i32in[1] & 0xff) << 16) | ((i32in[2] & 0xff) << 8) | ((i32in[3] & 0xff)));
331 }
332
333 private byte[] i64in = new byte[8];
334 public override long ReadI64()
335 {
336 ReadAll(i64in, 0, 8);
337 return (long)(((long)(i64in[0] & 0xff) << 56) | ((long)(i64in[1] & 0xff) << 48) | ((long)(i64in[2] & 0xff) << 40) | ((long)(i64in[3] & 0xff) << 32) |
338 ((long)(i64in[4] & 0xff) << 24) | ((long)(i64in[5] & 0xff) << 16) | ((long)(i64in[6] & 0xff) << 8) | ((long)(i64in[7] & 0xff)));
339 }
340
341 public override double ReadDouble()
342 {
343 return BitConverter.Int64BitsToDouble(ReadI64());
344 }
345
346 public void SetReadLength(int readLength)
347 {
348 readLength_ = readLength;
349 checkReadLength_ = true;
350 }
351
352 protected void CheckReadLength(int length)
353 {
354 if (checkReadLength_)
355 {
356 readLength_ -= length;
357 if (readLength_ < 0)
358 {
359 throw new Exception("Message length exceeded: " + length);
360 }
361 }
362 }
363
364 public override string ReadString()
365 {
366 int size = ReadI32();
367 return ReadStringBody(size);
368 }
369
370 public string ReadStringBody(int size)
371 {
372 CheckReadLength(size);
373 byte[] buf = new byte[size];
374 trans.ReadAll(buf, 0, size);
375 return Encoding.UTF8.GetString(buf);
376 }
377
378 private int ReadAll(byte[] buf, int off, int len)
379 {
380 CheckReadLength(len);
381 return trans.ReadAll(buf, off, len);
382 }
383
384 #endregion
385 }
386}