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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
26 using Thrift.Transport;
27 using System.Collections;
29 using System.Collections.Generic;
31 namespace Thrift.Protocol
33 public class TCompactProtocol : TProtocol
35 private static TStruct ANONYMOUS_STRUCT = new TStruct("");
36 private static TField TSTOP = new TField("", TType.Stop, (short)0);
38 private static byte[] ttypeToCompactType = new byte[16];
40 private const byte PROTOCOL_ID = 0x82;
41 private const byte VERSION = 1;
42 private const byte VERSION_MASK = 0x1f; // 0001 1111
43 private const byte TYPE_MASK = 0xE0; // 1110 0000
44 private const byte TYPE_BITS = 0x07; // 0000 0111
45 private const int TYPE_SHIFT_AMOUNT = 5;
48 /// All of the on-wire type codes.
50 private static class Types
52 public const byte STOP = 0x00;
53 public const byte BOOLEAN_TRUE = 0x01;
54 public const byte BOOLEAN_FALSE = 0x02;
55 public const byte BYTE = 0x03;
56 public const byte I16 = 0x04;
57 public const byte I32 = 0x05;
58 public const byte I64 = 0x06;
59 public const byte DOUBLE = 0x07;
60 public const byte BINARY = 0x08;
61 public const byte LIST = 0x09;
62 public const byte SET = 0x0A;
63 public const byte MAP = 0x0B;
64 public const byte STRUCT = 0x0C;
68 /// Used to keep track of the last field for the current and previous structs,
69 /// so we can do the delta stuff.
71 private Stack<short> lastField_ = new Stack<short>(15);
73 private short lastFieldId_ = 0;
76 /// If we encounter a boolean field begin, save the TField here so it can
77 /// have the value incorporated.
79 private Nullable<TField> booleanField_;
82 /// If we Read a field header, and it's a boolean field, save the boolean
83 /// value here so that ReadBool can use it.
85 private Nullable<Boolean> boolValue_;
88 #region CompactProtocol Factory
90 public class Factory : TProtocolFactory
94 public TProtocol GetProtocol(TTransport trans)
96 return new TCompactProtocol(trans);
102 public TCompactProtocol(TTransport trans)
105 ttypeToCompactType[(int)TType.Stop] = Types.STOP;
106 ttypeToCompactType[(int)TType.Bool] = Types.BOOLEAN_TRUE;
107 ttypeToCompactType[(int)TType.Byte] = Types.BYTE;
108 ttypeToCompactType[(int)TType.I16] = Types.I16;
109 ttypeToCompactType[(int)TType.I32] = Types.I32;
110 ttypeToCompactType[(int)TType.I64] = Types.I64;
111 ttypeToCompactType[(int)TType.Double] = Types.DOUBLE;
112 ttypeToCompactType[(int)TType.String] = Types.BINARY;
113 ttypeToCompactType[(int)TType.List] = Types.LIST;
114 ttypeToCompactType[(int)TType.Set] = Types.SET;
115 ttypeToCompactType[(int)TType.Map] = Types.MAP;
116 ttypeToCompactType[(int)TType.Struct] = Types.STRUCT;
125 #region Write Methods
128 /// Writes a byte without any possibility of all that field header nonsense.
129 /// Used internally by other writing methods that know they need to Write a byte.
131 private byte[] byteDirectBuffer = new byte[1];
133 private void WriteByteDirect(byte b)
135 byteDirectBuffer[0] = b;
136 trans.Write(byteDirectBuffer);
140 /// Writes a byte without any possibility of all that field header nonsense.
142 private void WriteByteDirect(int n)
144 WriteByteDirect((byte)n);
148 /// Write an i32 as a varint. Results in 1-5 bytes on the wire.
149 /// TODO: make a permanent buffer like WriteVarint64?
151 byte[] i32buf = new byte[5];
153 private void WriteVarint32(uint n)
158 if ((n & ~0x7F) == 0)
160 i32buf[idx++] = (byte)n;
161 // WriteByteDirect((byte)n);
167 i32buf[idx++] = (byte)((n & 0x7F) | 0x80);
168 // WriteByteDirect((byte)((n & 0x7F) | 0x80));
172 trans.Write(i32buf, 0, idx);
176 /// Write a message header to the wire. Compact Protocol messages contain the
177 /// protocol version so we can migrate forwards in the future if need be.
179 public override void WriteMessageBegin(TMessage message)
181 WriteByteDirect(PROTOCOL_ID);
182 WriteByteDirect((byte)((VERSION & VERSION_MASK) | ((((uint)message.Type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK)));
183 WriteVarint32((uint)message.SeqID);
184 WriteString(message.Name);
188 /// Write a struct begin. This doesn't actually put anything on the wire. We
189 /// use it as an opportunity to put special placeholder markers on the field
190 /// stack so we can get the field id deltas correct.
192 public override void WriteStructBegin(TStruct strct)
194 lastField_.Push(lastFieldId_);
199 /// Write a struct end. This doesn't actually put anything on the wire. We use
200 /// this as an opportunity to pop the last field from the current struct off
201 /// of the field stack.
203 public override void WriteStructEnd()
205 lastFieldId_ = lastField_.Pop();
209 /// Write a field header containing the field id and field type. If the
210 /// difference between the current field id and the last one is small (< 15),
211 /// then the field id will be encoded in the 4 MSB as a delta. Otherwise, the
212 /// field id will follow the type header as a zigzag varint.
214 public override void WriteFieldBegin(TField field)
216 if (field.Type == TType.Bool)
218 // we want to possibly include the value, so we'll wait.
219 booleanField_ = field;
223 WriteFieldBeginInternal(field, 0xFF);
228 /// The workhorse of WriteFieldBegin. It has the option of doing a
229 /// 'type override' of the type header. This is used specifically in the
230 /// boolean field case.
232 private void WriteFieldBeginInternal(TField field, byte typeOverride)
234 // short lastField = lastField_.Pop();
236 // if there's a type override, use that.
237 byte typeToWrite = typeOverride == 0xFF ? getCompactType(field.Type) : typeOverride;
239 // check if we can use delta encoding for the field id
240 if (field.ID > lastFieldId_ && field.ID - lastFieldId_ <= 15)
242 // Write them together
243 WriteByteDirect((field.ID - lastFieldId_) << 4 | typeToWrite);
247 // Write them separate
248 WriteByteDirect(typeToWrite);
252 lastFieldId_ = field.ID;
253 // lastField_.push(field.id);
257 /// Write the STOP symbol so we know there are no more fields in this struct.
259 public override void WriteFieldStop()
261 WriteByteDirect(Types.STOP);
265 /// Write a map header. If the map is empty, omit the key and value type
266 /// headers, as we don't need any additional information to skip it.
268 public override void WriteMapBegin(TMap map)
276 WriteVarint32((uint)map.Count);
277 WriteByteDirect(getCompactType(map.KeyType) << 4 | getCompactType(map.ValueType));
282 /// Write a list header.
284 public override void WriteListBegin(TList list)
286 WriteCollectionBegin(list.ElementType, list.Count);
290 /// Write a set header.
292 public override void WriteSetBegin(TSet set)
294 WriteCollectionBegin(set.ElementType, set.Count);
298 /// Write a boolean value. Potentially, this could be a boolean field, in
299 /// which case the field header info isn't written yet. If so, decide what the
300 /// right type header is for the value and then Write the field header.
301 /// Otherwise, Write a single byte.
303 public override void WriteBool(Boolean b)
305 if (booleanField_ != null)
307 // we haven't written the field header yet
308 WriteFieldBeginInternal(booleanField_.Value, b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE);
309 booleanField_ = null;
313 // we're not part of a field, so just Write the value.
314 WriteByteDirect(b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE);
319 /// Write a byte. Nothing to see here!
321 public override void WriteByte(sbyte b)
323 WriteByteDirect((byte)b);
327 /// Write an I16 as a zigzag varint.
329 public override void WriteI16(short i16)
331 WriteVarint32(intToZigZag(i16));
335 /// Write an i32 as a zigzag varint.
337 public override void WriteI32(int i32)
339 WriteVarint32(intToZigZag(i32));
343 /// Write an i64 as a zigzag varint.
345 public override void WriteI64(long i64)
347 WriteVarint64(longToZigzag(i64));
351 /// Write a double to the wire as 8 bytes.
353 public override void WriteDouble(double dub)
355 byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
356 fixedLongToBytes(BitConverter.DoubleToInt64Bits(dub), data, 0);
361 /// Write a string to the wire with a varint size preceding.
363 public override void WriteString(string str)
365 byte[] bytes = UTF8Encoding.UTF8.GetBytes(str);
366 WriteBinary(bytes, 0, bytes.Length);
370 /// Write a byte array, using a varint for the size.
372 public override void WriteBinary(byte[] bin)
374 WriteBinary(bin, 0, bin.Length);
377 private void WriteBinary(byte[] buf, int offset, int length)
379 WriteVarint32((uint)length);
380 trans.Write(buf, offset, length);
384 // These methods are called by structs, but don't actually have any wire
385 // output or purpose.
388 public override void WriteMessageEnd() { }
389 public override void WriteMapEnd() { }
390 public override void WriteListEnd() { }
391 public override void WriteSetEnd() { }
392 public override void WriteFieldEnd() { }
395 // Internal writing methods
399 /// Abstract method for writing the start of lists and sets. List and sets on
400 /// the wire differ only by the type indicator.
402 protected void WriteCollectionBegin(TType elemType, int size)
406 WriteByteDirect(size << 4 | getCompactType(elemType));
410 WriteByteDirect(0xf0 | getCompactType(elemType));
411 WriteVarint32((uint)size);
416 /// Write an i64 as a varint. Results in 1-10 bytes on the wire.
418 byte[] varint64out = new byte[10];
419 private void WriteVarint64(ulong n)
424 if ((n & ~(ulong)0x7FL) == 0)
426 varint64out[idx++] = (byte)n;
431 varint64out[idx++] = ((byte)((n & 0x7F) | 0x80));
435 trans.Write(varint64out, 0, idx);
439 /// Convert l into a zigzag long. This allows negative numbers to be
440 /// represented compactly as a varint.
442 private ulong longToZigzag(long n)
444 return (ulong)(n << 1) ^ (ulong)(n >> 63);
448 /// Convert n into a zigzag int. This allows negative numbers to be
449 /// represented compactly as a varint.
451 private uint intToZigZag(int n)
453 return (uint)(n << 1) ^ (uint)(n >> 31);
457 /// Convert a long into little-endian bytes in buf starting at off and going
460 private void fixedLongToBytes(long n, byte[] buf, int off)
462 buf[off + 0] = (byte)(n & 0xff);
463 buf[off + 1] = (byte)((n >> 8) & 0xff);
464 buf[off + 2] = (byte)((n >> 16) & 0xff);
465 buf[off + 3] = (byte)((n >> 24) & 0xff);
466 buf[off + 4] = (byte)((n >> 32) & 0xff);
467 buf[off + 5] = (byte)((n >> 40) & 0xff);
468 buf[off + 6] = (byte)((n >> 48) & 0xff);
469 buf[off + 7] = (byte)((n >> 56) & 0xff);
477 /// Read a message header.
479 public override TMessage ReadMessageBegin()
481 byte protocolId = (byte)ReadByte();
482 if (protocolId != PROTOCOL_ID)
484 throw new TProtocolException("Expected protocol id " + PROTOCOL_ID.ToString("X") + " but got " + protocolId.ToString("X"));
486 byte versionAndType = (byte)ReadByte();
487 byte version = (byte)(versionAndType & VERSION_MASK);
488 if (version != VERSION)
490 throw new TProtocolException("Expected version " + VERSION + " but got " + version);
492 byte type = (byte)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
493 int seqid = (int)ReadVarint32();
494 string messageName = ReadString();
495 return new TMessage(messageName, (TMessageType)type, seqid);
499 /// Read a struct begin. There's nothing on the wire for this, but it is our
500 /// opportunity to push a new struct begin marker onto the field stack.
502 public override TStruct ReadStructBegin()
504 lastField_.Push(lastFieldId_);
506 return ANONYMOUS_STRUCT;
510 /// Doesn't actually consume any wire data, just removes the last field for
511 /// this struct from the field stack.
513 public override void ReadStructEnd()
515 // consume the last field we Read off the wire.
516 lastFieldId_ = lastField_.Pop();
520 /// Read a field header off the wire.
522 public override TField ReadFieldBegin()
524 byte type = (byte)ReadByte();
526 // if it's a stop, then we can return immediately, as the struct is over.
527 if (type == Types.STOP)
534 // mask off the 4 MSB of the type header. it could contain a field id delta.
535 short modifier = (short)((type & 0xf0) >> 4);
538 // not a delta. look ahead for the zigzag varint field id.
543 // has a delta. add the delta to the last Read field id.
544 fieldId = (short)(lastFieldId_ + modifier);
547 TField field = new TField("", getTType((byte)(type & 0x0f)), fieldId);
549 // if this happens to be a boolean field, the value is encoded in the type
550 if (isBoolType(type))
552 // save the boolean value in a special instance variable.
553 boolValue_ = (byte)(type & 0x0f) == Types.BOOLEAN_TRUE ? true : false;
556 // push the new field onto the field stack so we can keep the deltas going.
557 lastFieldId_ = field.ID;
562 /// Read a map header off the wire. If the size is zero, skip Reading the key
563 /// and value type. This means that 0-length maps will yield TMaps without the
566 public override TMap ReadMapBegin()
568 int size = (int)ReadVarint32();
569 byte keyAndValueType = size == 0 ? (byte)0 : (byte)ReadByte();
570 return new TMap(getTType((byte)(keyAndValueType >> 4)), getTType((byte)(keyAndValueType & 0xf)), size);
574 /// Read a list header off the wire. If the list size is 0-14, the size will
575 /// be packed into the element type header. If it's a longer list, the 4 MSB
576 /// of the element type header will be 0xF, and a varint will follow with the
579 public override TList ReadListBegin()
581 byte size_and_type = (byte)ReadByte();
582 int size = (size_and_type >> 4) & 0x0f;
585 size = (int)ReadVarint32();
587 TType type = getTType(size_and_type);
588 return new TList(type, size);
592 /// Read a set header off the wire. If the set size is 0-14, the size will
593 /// be packed into the element type header. If it's a longer set, the 4 MSB
594 /// of the element type header will be 0xF, and a varint will follow with the
597 public override TSet ReadSetBegin()
599 return new TSet(ReadListBegin());
603 /// Read a boolean off the wire. If this is a boolean field, the value should
604 /// already have been Read during ReadFieldBegin, so we'll just consume the
605 /// pre-stored value. Otherwise, Read a byte.
607 public override Boolean ReadBool()
609 if (boolValue_ != null)
611 bool result = boolValue_.Value;
615 return ReadByte() == Types.BOOLEAN_TRUE;
618 byte[] byteRawBuf = new byte[1];
620 /// Read a single byte off the wire. Nothing interesting here.
622 public override sbyte ReadByte()
624 trans.ReadAll(byteRawBuf, 0, 1);
625 return (sbyte)byteRawBuf[0];
629 /// Read an i16 from the wire as a zigzag varint.
631 public override short ReadI16()
633 return (short)zigzagToInt(ReadVarint32());
637 /// Read an i32 from the wire as a zigzag varint.
639 public override int ReadI32()
641 return zigzagToInt(ReadVarint32());
645 /// Read an i64 from the wire as a zigzag varint.
647 public override long ReadI64()
649 return zigzagToLong(ReadVarint64());
653 /// No magic here - just Read a double off the wire.
655 public override double ReadDouble()
657 byte[] longBits = new byte[8];
658 trans.ReadAll(longBits, 0, 8);
659 return BitConverter.Int64BitsToDouble(bytesToLong(longBits));
663 /// Reads a byte[] (via ReadBinary), and then UTF-8 decodes it.
665 public override string ReadString()
667 int length = (int)ReadVarint32();
674 return Encoding.UTF8.GetString(ReadBinary(length));
678 /// Read a byte[] from the wire.
680 public override byte[] ReadBinary()
682 int length = (int)ReadVarint32();
683 if (length == 0) return new byte[0];
685 byte[] buf = new byte[length];
686 trans.ReadAll(buf, 0, length);
691 /// Read a byte[] of a known length from the wire.
693 private byte[] ReadBinary(int length)
695 if (length == 0) return new byte[0];
697 byte[] buf = new byte[length];
698 trans.ReadAll(buf, 0, length);
703 // These methods are here for the struct to call, but don't have any wire
706 public override void ReadMessageEnd() { }
707 public override void ReadFieldEnd() { }
708 public override void ReadMapEnd() { }
709 public override void ReadListEnd() { }
710 public override void ReadSetEnd() { }
713 // Internal Reading methods
717 /// Read an i32 from the wire as a varint. The MSB of each byte is set
718 /// if there is another byte to follow. This can Read up to 5 bytes.
720 private uint ReadVarint32()
726 byte b = (byte)ReadByte();
727 result |= (uint)(b & 0x7f) << shift;
728 if ((b & 0x80) != 0x80) break;
735 /// Read an i64 from the wire as a proper varint. The MSB of each byte is set
736 /// if there is another byte to follow. This can Read up to 10 bytes.
738 private ulong ReadVarint64()
744 byte b = (byte)ReadByte();
745 result |= (ulong)(b & 0x7f) << shift;
746 if ((b & 0x80) != 0x80) break;
760 /// Convert from zigzag int to int.
762 private int zigzagToInt(uint n)
764 return (int)(n >> 1) ^ (-(int)(n & 1));
768 /// Convert from zigzag long to long.
770 private long zigzagToLong(ulong n)
772 return (long)(n >> 1) ^ (-(long)(n & 1));
776 /// Note that it's important that the mask bytes are long literals,
777 /// otherwise they'll default to ints, and when you shift an int left 56 bits,
778 /// you just get a messed up int.
780 private long bytesToLong(byte[] bytes)
783 ((bytes[7] & 0xffL) << 56) |
784 ((bytes[6] & 0xffL) << 48) |
785 ((bytes[5] & 0xffL) << 40) |
786 ((bytes[4] & 0xffL) << 32) |
787 ((bytes[3] & 0xffL) << 24) |
788 ((bytes[2] & 0xffL) << 16) |
789 ((bytes[1] & 0xffL) << 8) |
790 ((bytes[0] & 0xffL));
794 // type testing and converting
797 private Boolean isBoolType(byte b)
799 int lowerNibble = b & 0x0f;
800 return lowerNibble == Types.BOOLEAN_TRUE || lowerNibble == Types.BOOLEAN_FALSE;
804 /// Given a TCompactProtocol.Types constant, convert it to its corresponding
807 private TType getTType(byte type)
809 switch ((byte)(type & 0x0f))
813 case Types.BOOLEAN_FALSE:
814 case Types.BOOLEAN_TRUE:
837 throw new TProtocolException("don't know what type: " + (byte)(type & 0x0f));
842 /// Given a TType value, find the appropriate TCompactProtocol.Types constant.
844 private byte getCompactType(TType ttype)
846 return ttypeToCompactType[(int)ttype];