Revert "THRIFT-4982 Remove deprecated C# bindings from the code base"
Only compiler, test, lib and tutorial code.
diff --git a/lib/csharp/src/Protocol/TAbstractBase.cs b/lib/csharp/src/Protocol/TAbstractBase.cs
new file mode 100644
index 0000000..f5a61cd
--- /dev/null
+++ b/lib/csharp/src/Protocol/TAbstractBase.cs
@@ -0,0 +1,29 @@
+/**
+ * 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.
+ */
+
+namespace Thrift.Protocol
+{
+ public interface TAbstractBase
+ {
+ /// <summary>
+ /// Writes the objects out to the protocol.
+ /// </summary>
+ void Write(TProtocol tProtocol);
+ }
+}
diff --git a/lib/csharp/src/Protocol/TBase.cs b/lib/csharp/src/Protocol/TBase.cs
new file mode 100644
index 0000000..411e4d9
--- /dev/null
+++ b/lib/csharp/src/Protocol/TBase.cs
@@ -0,0 +1,29 @@
+/**
+ * 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.
+ */
+
+namespace Thrift.Protocol
+{
+ public interface TBase : TAbstractBase
+ {
+ /// <summary>
+ /// Reads the TObject from the given input protocol.
+ /// </summary>
+ void Read(TProtocol tProtocol);
+ }
+}
diff --git a/lib/csharp/src/Protocol/TBase64Utils.cs b/lib/csharp/src/Protocol/TBase64Utils.cs
new file mode 100644
index 0000000..9eaaebd
--- /dev/null
+++ b/lib/csharp/src/Protocol/TBase64Utils.cs
@@ -0,0 +1,100 @@
+/**
+ * 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;
+
+namespace Thrift.Protocol
+{
+ internal static class TBase64Utils
+ {
+ internal const string ENCODE_TABLE =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+ internal static void encode(byte[] src, int srcOff, int len, byte[] dst,
+ int dstOff)
+ {
+ dst[dstOff] = (byte)ENCODE_TABLE[(src[srcOff] >> 2) & 0x3F];
+ if (len == 3)
+ {
+ dst[dstOff + 1] =
+ (byte)ENCODE_TABLE[
+ ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
+ dst[dstOff + 2] =
+ (byte)ENCODE_TABLE[
+ ((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)];
+ dst[dstOff + 3] =
+ (byte)ENCODE_TABLE[src[srcOff + 2] & 0x3F];
+ }
+ else if (len == 2)
+ {
+ dst[dstOff + 1] =
+ (byte)ENCODE_TABLE[
+ ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
+ dst[dstOff + 2] =
+ (byte)ENCODE_TABLE[(src[srcOff + 1] << 2) & 0x3C];
+
+ }
+ else
+ { // len == 1) {
+ dst[dstOff + 1] =
+ (byte)ENCODE_TABLE[(src[srcOff] << 4) & 0x30];
+ }
+ }
+
+ private static int[] DECODE_TABLE = {
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
+ 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
+ -1, 0, 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,-1,-1,-1,-1,-1,
+ -1,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,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ };
+
+ internal static void decode(byte[] src, int srcOff, int len, byte[] dst,
+ int dstOff)
+ {
+ dst[dstOff] = (byte)
+ ((DECODE_TABLE[src[srcOff] & 0x0FF] << 2) |
+ (DECODE_TABLE[src[srcOff + 1] & 0x0FF] >> 4));
+ if (len > 2)
+ {
+ dst[dstOff + 1] = (byte)
+ (((DECODE_TABLE[src[srcOff + 1] & 0x0FF] << 4) & 0xF0) |
+ (DECODE_TABLE[src[srcOff + 2] & 0x0FF] >> 2));
+ if (len > 3)
+ {
+ dst[dstOff + 2] = (byte)
+ (((DECODE_TABLE[src[srcOff + 2] & 0x0FF] << 6) & 0xC0) |
+ DECODE_TABLE[src[srcOff + 3] & 0x0FF]);
+ }
+ }
+ }
+
+ }
+}
diff --git a/lib/csharp/src/Protocol/TBinaryProtocol.cs b/lib/csharp/src/Protocol/TBinaryProtocol.cs
new file mode 100644
index 0000000..a4faa94
--- /dev/null
+++ b/lib/csharp/src/Protocol/TBinaryProtocol.cs
@@ -0,0 +1,395 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+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;
+
+ #region BinaryProtocol 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((sbyte)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((sbyte)field.Type);
+ WriteI16(field.ID);
+ }
+
+ public override void WriteFieldEnd()
+ {
+ }
+
+ public override void WriteFieldStop()
+ {
+ WriteByte((sbyte)TType.Stop);
+ }
+
+ public override void WriteMapBegin(TMap map)
+ {
+ WriteByte((sbyte)map.KeyType);
+ WriteByte((sbyte)map.ValueType);
+ WriteI32(map.Count);
+ }
+
+ public override void WriteMapEnd()
+ {
+ }
+
+ public override void WriteListBegin(TList list)
+ {
+ WriteByte((sbyte)list.ElementType);
+ WriteI32(list.Count);
+ }
+
+ public override void WriteListEnd()
+ {
+ }
+
+ public override void WriteSetBegin(TSet set)
+ {
+ WriteByte((sbyte)set.ElementType);
+ WriteI32(set.Count);
+ }
+
+ public override void WriteSetEnd()
+ {
+ }
+
+ public override void WriteBool(bool b)
+ {
+ WriteByte(b ? (sbyte)1 : (sbyte)0);
+ }
+
+ private byte[] bout = new byte[1];
+ public override void WriteByte(sbyte b)
+ {
+ bout[0] = (byte)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)
+ {
+#if !SILVERLIGHT
+ WriteI64(BitConverter.DoubleToInt64Bits(d));
+#else
+ var bytes = BitConverter.GetBytes(d);
+ WriteI64(BitConverter.ToInt64(bytes, 0));
+#endif
+ }
+
+ 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 sbyte ReadByte()
+ {
+ ReadAll(bin, 0, 1);
+ return (sbyte)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)));
+ }
+
+#pragma warning disable 675
+
+ private byte[] i64in = new byte[8];
+ public override long ReadI64()
+ {
+ ReadAll(i64in, 0, 8);
+ unchecked
+ {
+ 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)));
+ }
+ }
+
+#pragma warning restore 675
+
+ public override double ReadDouble()
+ {
+#if !SILVERLIGHT
+ return BitConverter.Int64BitsToDouble(ReadI64());
+#else
+ var value = ReadI64();
+ var bytes = BitConverter.GetBytes(value);
+ return BitConverter.ToDouble(bytes, 0);
+#endif
+ }
+
+ public override byte[] ReadBinary()
+ {
+ int size = ReadI32();
+ byte[] buf = new byte[size];
+ trans.ReadAll(buf, 0, size);
+ return buf;
+ }
+ private string ReadStringBody(int size)
+ {
+ byte[] buf = new byte[size];
+ trans.ReadAll(buf, 0, size);
+ return Encoding.UTF8.GetString(buf, 0, buf.Length);
+ }
+
+ private int ReadAll(byte[] buf, int off, int len)
+ {
+ return trans.ReadAll(buf, off, len);
+ }
+
+ #endregion
+ }
+}
diff --git a/lib/csharp/src/Protocol/TCompactProtocol.cs b/lib/csharp/src/Protocol/TCompactProtocol.cs
new file mode 100644
index 0000000..ff67397
--- /dev/null
+++ b/lib/csharp/src/Protocol/TCompactProtocol.cs
@@ -0,0 +1,849 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Text;
+using Thrift.Transport;
+using System.Collections;
+using System.IO;
+using System.Collections.Generic;
+
+namespace Thrift.Protocol
+{
+ public class TCompactProtocol : TProtocol
+ {
+ private static TStruct ANONYMOUS_STRUCT = new TStruct("");
+ private static TField TSTOP = new TField("", TType.Stop, (short)0);
+
+ private static byte[] ttypeToCompactType = new byte[16];
+
+ private const byte PROTOCOL_ID = 0x82;
+ private const byte VERSION = 1;
+ private const byte VERSION_MASK = 0x1f; // 0001 1111
+ private const byte TYPE_MASK = 0xE0; // 1110 0000
+ private const byte TYPE_BITS = 0x07; // 0000 0111
+ private const int TYPE_SHIFT_AMOUNT = 5;
+
+ /// <summary>
+ /// All of the on-wire type codes.
+ /// </summary>
+ private static class Types
+ {
+ public const byte STOP = 0x00;
+ public const byte BOOLEAN_TRUE = 0x01;
+ public const byte BOOLEAN_FALSE = 0x02;
+ public const byte BYTE = 0x03;
+ public const byte I16 = 0x04;
+ public const byte I32 = 0x05;
+ public const byte I64 = 0x06;
+ public const byte DOUBLE = 0x07;
+ public const byte BINARY = 0x08;
+ public const byte LIST = 0x09;
+ public const byte SET = 0x0A;
+ public const byte MAP = 0x0B;
+ public const byte STRUCT = 0x0C;
+ }
+
+ /// <summary>
+ /// Used to keep track of the last field for the current and previous structs,
+ /// so we can do the delta stuff.
+ /// </summary>
+ private Stack<short> lastField_ = new Stack<short>(15);
+
+ private short lastFieldId_ = 0;
+
+ /// <summary>
+ /// If we encounter a boolean field begin, save the TField here so it can
+ /// have the value incorporated.
+ /// </summary>
+ private Nullable<TField> booleanField_;
+
+ /// <summary>
+ /// If we Read a field header, and it's a boolean field, save the boolean
+ /// value here so that ReadBool can use it.
+ /// </summary>
+ private Nullable<Boolean> boolValue_;
+
+
+ #region CompactProtocol Factory
+
+ public class Factory : TProtocolFactory
+ {
+ public Factory() { }
+
+ public TProtocol GetProtocol(TTransport trans)
+ {
+ return new TCompactProtocol(trans);
+ }
+ }
+
+ #endregion
+
+ public TCompactProtocol(TTransport trans)
+ : base(trans)
+ {
+ ttypeToCompactType[(int)TType.Stop] = Types.STOP;
+ ttypeToCompactType[(int)TType.Bool] = Types.BOOLEAN_TRUE;
+ ttypeToCompactType[(int)TType.Byte] = Types.BYTE;
+ ttypeToCompactType[(int)TType.I16] = Types.I16;
+ ttypeToCompactType[(int)TType.I32] = Types.I32;
+ ttypeToCompactType[(int)TType.I64] = Types.I64;
+ ttypeToCompactType[(int)TType.Double] = Types.DOUBLE;
+ ttypeToCompactType[(int)TType.String] = Types.BINARY;
+ ttypeToCompactType[(int)TType.List] = Types.LIST;
+ ttypeToCompactType[(int)TType.Set] = Types.SET;
+ ttypeToCompactType[(int)TType.Map] = Types.MAP;
+ ttypeToCompactType[(int)TType.Struct] = Types.STRUCT;
+ }
+
+ public void reset()
+ {
+ lastField_.Clear();
+ lastFieldId_ = 0;
+ }
+
+ #region Write Methods
+
+ /// <summary>
+ /// Writes a byte without any possibility of all that field header nonsense.
+ /// Used internally by other writing methods that know they need to Write a byte.
+ /// </summary>
+ private byte[] byteDirectBuffer = new byte[1];
+
+ private void WriteByteDirect(byte b)
+ {
+ byteDirectBuffer[0] = b;
+ trans.Write(byteDirectBuffer);
+ }
+
+ /// <summary>
+ /// Writes a byte without any possibility of all that field header nonsense.
+ /// </summary>
+ private void WriteByteDirect(int n)
+ {
+ WriteByteDirect((byte)n);
+ }
+
+ /// <summary>
+ /// Write an i32 as a varint. Results in 1-5 bytes on the wire.
+ /// TODO: make a permanent buffer like WriteVarint64?
+ /// </summary>
+ byte[] i32buf = new byte[5];
+
+ private void WriteVarint32(uint n)
+ {
+ int idx = 0;
+ while (true)
+ {
+ if ((n & ~0x7F) == 0)
+ {
+ i32buf[idx++] = (byte)n;
+ // WriteByteDirect((byte)n);
+ break;
+ // return;
+ }
+ else
+ {
+ i32buf[idx++] = (byte)((n & 0x7F) | 0x80);
+ // WriteByteDirect((byte)((n & 0x7F) | 0x80));
+ n >>= 7;
+ }
+ }
+ trans.Write(i32buf, 0, idx);
+ }
+
+ /// <summary>
+ /// Write a message header to the wire. Compact Protocol messages contain the
+ /// protocol version so we can migrate forwards in the future if need be.
+ /// </summary>
+ public override void WriteMessageBegin(TMessage message)
+ {
+ WriteByteDirect(PROTOCOL_ID);
+ WriteByteDirect((byte)((VERSION & VERSION_MASK) | ((((uint)message.Type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK)));
+ WriteVarint32((uint)message.SeqID);
+ WriteString(message.Name);
+ }
+
+ /// <summary>
+ /// Write a struct begin. This doesn't actually put anything on the wire. We
+ /// use it as an opportunity to put special placeholder markers on the field
+ /// stack so we can get the field id deltas correct.
+ /// </summary>
+ public override void WriteStructBegin(TStruct strct)
+ {
+ lastField_.Push(lastFieldId_);
+ lastFieldId_ = 0;
+ }
+
+ /// <summary>
+ /// Write a struct end. This doesn't actually put anything on the wire. We use
+ /// this as an opportunity to pop the last field from the current struct off
+ /// of the field stack.
+ /// </summary>
+ public override void WriteStructEnd()
+ {
+ lastFieldId_ = lastField_.Pop();
+ }
+
+ /// <summary>
+ /// Write a field header containing the field id and field type. If the
+ /// difference between the current field id and the last one is small (< 15),
+ /// then the field id will be encoded in the 4 MSB as a delta. Otherwise, the
+ /// field id will follow the type header as a zigzag varint.
+ /// </summary>
+ public override void WriteFieldBegin(TField field)
+ {
+ if (field.Type == TType.Bool)
+ {
+ // we want to possibly include the value, so we'll wait.
+ booleanField_ = field;
+ }
+ else
+ {
+ WriteFieldBeginInternal(field, 0xFF);
+ }
+ }
+
+ /// <summary>
+ /// The workhorse of WriteFieldBegin. It has the option of doing a
+ /// 'type override' of the type header. This is used specifically in the
+ /// boolean field case.
+ /// </summary>
+ private void WriteFieldBeginInternal(TField field, byte typeOverride)
+ {
+ // short lastField = lastField_.Pop();
+
+ // if there's a type override, use that.
+ byte typeToWrite = typeOverride == 0xFF ? getCompactType(field.Type) : typeOverride;
+
+ // check if we can use delta encoding for the field id
+ if (field.ID > lastFieldId_ && field.ID - lastFieldId_ <= 15)
+ {
+ // Write them together
+ WriteByteDirect((field.ID - lastFieldId_) << 4 | typeToWrite);
+ }
+ else
+ {
+ // Write them separate
+ WriteByteDirect(typeToWrite);
+ WriteI16(field.ID);
+ }
+
+ lastFieldId_ = field.ID;
+ // lastField_.push(field.id);
+ }
+
+ /// <summary>
+ /// Write the STOP symbol so we know there are no more fields in this struct.
+ /// </summary>
+ public override void WriteFieldStop()
+ {
+ WriteByteDirect(Types.STOP);
+ }
+
+ /// <summary>
+ /// Write a map header. If the map is empty, omit the key and value type
+ /// headers, as we don't need any additional information to skip it.
+ /// </summary>
+ public override void WriteMapBegin(TMap map)
+ {
+ if (map.Count == 0)
+ {
+ WriteByteDirect(0);
+ }
+ else
+ {
+ WriteVarint32((uint)map.Count);
+ WriteByteDirect(getCompactType(map.KeyType) << 4 | getCompactType(map.ValueType));
+ }
+ }
+
+ /// <summary>
+ /// Write a list header.
+ /// </summary>
+ public override void WriteListBegin(TList list)
+ {
+ WriteCollectionBegin(list.ElementType, list.Count);
+ }
+
+ /// <summary>
+ /// Write a set header.
+ /// </summary>
+ public override void WriteSetBegin(TSet set)
+ {
+ WriteCollectionBegin(set.ElementType, set.Count);
+ }
+
+ /// <summary>
+ /// Write a boolean value. Potentially, this could be a boolean field, in
+ /// which case the field header info isn't written yet. If so, decide what the
+ /// right type header is for the value and then Write the field header.
+ /// Otherwise, Write a single byte.
+ /// </summary>
+ public override void WriteBool(Boolean b)
+ {
+ if (booleanField_ != null)
+ {
+ // we haven't written the field header yet
+ WriteFieldBeginInternal(booleanField_.Value, b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE);
+ booleanField_ = null;
+ }
+ else
+ {
+ // we're not part of a field, so just Write the value.
+ WriteByteDirect(b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE);
+ }
+ }
+
+ /// <summary>
+ /// Write a byte. Nothing to see here!
+ /// </summary>
+ public override void WriteByte(sbyte b)
+ {
+ WriteByteDirect((byte)b);
+ }
+
+ /// <summary>
+ /// Write an I16 as a zigzag varint.
+ /// </summary>
+ public override void WriteI16(short i16)
+ {
+ WriteVarint32(intToZigZag(i16));
+ }
+
+ /// <summary>
+ /// Write an i32 as a zigzag varint.
+ /// </summary>
+ public override void WriteI32(int i32)
+ {
+ WriteVarint32(intToZigZag(i32));
+ }
+
+ /// <summary>
+ /// Write an i64 as a zigzag varint.
+ /// </summary>
+ public override void WriteI64(long i64)
+ {
+ WriteVarint64(longToZigzag(i64));
+ }
+
+ /// <summary>
+ /// Write a double to the wire as 8 bytes.
+ /// </summary>
+ public override void WriteDouble(double dub)
+ {
+ byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
+ fixedLongToBytes(BitConverter.DoubleToInt64Bits(dub), data, 0);
+ trans.Write(data);
+ }
+
+ /// <summary>
+ /// Write a string to the wire with a varint size preceding.
+ /// </summary>
+ public override void WriteString(string str)
+ {
+ byte[] bytes = UTF8Encoding.UTF8.GetBytes(str);
+ WriteBinary(bytes, 0, bytes.Length);
+ }
+
+ /// <summary>
+ /// Write a byte array, using a varint for the size.
+ /// </summary>
+ public override void WriteBinary(byte[] bin)
+ {
+ WriteBinary(bin, 0, bin.Length);
+ }
+
+ private void WriteBinary(byte[] buf, int offset, int length)
+ {
+ WriteVarint32((uint)length);
+ trans.Write(buf, offset, length);
+ }
+
+ //
+ // These methods are called by structs, but don't actually have any wire
+ // output or purpose.
+ //
+
+ public override void WriteMessageEnd() { }
+ public override void WriteMapEnd() { }
+ public override void WriteListEnd() { }
+ public override void WriteSetEnd() { }
+ public override void WriteFieldEnd() { }
+
+ //
+ // Internal writing methods
+ //
+
+ /// <summary>
+ /// Abstract method for writing the start of lists and sets. List and sets on
+ /// the wire differ only by the type indicator.
+ /// </summary>
+ protected void WriteCollectionBegin(TType elemType, int size)
+ {
+ if (size <= 14)
+ {
+ WriteByteDirect(size << 4 | getCompactType(elemType));
+ }
+ else
+ {
+ WriteByteDirect(0xf0 | getCompactType(elemType));
+ WriteVarint32((uint)size);
+ }
+ }
+
+ /// <summary>
+ /// Write an i64 as a varint. Results in 1-10 bytes on the wire.
+ /// </summary>
+ byte[] varint64out = new byte[10];
+ private void WriteVarint64(ulong n)
+ {
+ int idx = 0;
+ while (true)
+ {
+ if ((n & ~(ulong)0x7FL) == 0)
+ {
+ varint64out[idx++] = (byte)n;
+ break;
+ }
+ else
+ {
+ varint64out[idx++] = ((byte)((n & 0x7F) | 0x80));
+ n >>= 7;
+ }
+ }
+ trans.Write(varint64out, 0, idx);
+ }
+
+ /// <summary>
+ /// Convert l into a zigzag long. This allows negative numbers to be
+ /// represented compactly as a varint.
+ /// </summary>
+ private ulong longToZigzag(long n)
+ {
+ return (ulong)(n << 1) ^ (ulong)(n >> 63);
+ }
+
+ /// <summary>
+ /// Convert n into a zigzag int. This allows negative numbers to be
+ /// represented compactly as a varint.
+ /// </summary>
+ private uint intToZigZag(int n)
+ {
+ return (uint)(n << 1) ^ (uint)(n >> 31);
+ }
+
+ /// <summary>
+ /// Convert a long into little-endian bytes in buf starting at off and going
+ /// until off+7.
+ /// </summary>
+ private void fixedLongToBytes(long n, byte[] buf, int off)
+ {
+ buf[off + 0] = (byte)(n & 0xff);
+ buf[off + 1] = (byte)((n >> 8) & 0xff);
+ buf[off + 2] = (byte)((n >> 16) & 0xff);
+ buf[off + 3] = (byte)((n >> 24) & 0xff);
+ buf[off + 4] = (byte)((n >> 32) & 0xff);
+ buf[off + 5] = (byte)((n >> 40) & 0xff);
+ buf[off + 6] = (byte)((n >> 48) & 0xff);
+ buf[off + 7] = (byte)((n >> 56) & 0xff);
+ }
+
+ #endregion
+
+ #region ReadMethods
+
+ /// <summary>
+ /// Read a message header.
+ /// </summary>
+ public override TMessage ReadMessageBegin()
+ {
+ byte protocolId = (byte)ReadByte();
+ if (protocolId != PROTOCOL_ID)
+ {
+ throw new TProtocolException("Expected protocol id " + PROTOCOL_ID.ToString("X") + " but got " + protocolId.ToString("X"));
+ }
+ byte versionAndType = (byte)ReadByte();
+ byte version = (byte)(versionAndType & VERSION_MASK);
+ if (version != VERSION)
+ {
+ throw new TProtocolException("Expected version " + VERSION + " but got " + version);
+ }
+ byte type = (byte)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
+ int seqid = (int)ReadVarint32();
+ string messageName = ReadString();
+ return new TMessage(messageName, (TMessageType)type, seqid);
+ }
+
+ /// <summary>
+ /// Read a struct begin. There's nothing on the wire for this, but it is our
+ /// opportunity to push a new struct begin marker onto the field stack.
+ /// </summary>
+ public override TStruct ReadStructBegin()
+ {
+ lastField_.Push(lastFieldId_);
+ lastFieldId_ = 0;
+ return ANONYMOUS_STRUCT;
+ }
+
+ /// <summary>
+ /// Doesn't actually consume any wire data, just removes the last field for
+ /// this struct from the field stack.
+ /// </summary>
+ public override void ReadStructEnd()
+ {
+ // consume the last field we Read off the wire.
+ lastFieldId_ = lastField_.Pop();
+ }
+
+ /// <summary>
+ /// Read a field header off the wire.
+ /// </summary>
+ public override TField ReadFieldBegin()
+ {
+ byte type = (byte)ReadByte();
+
+ // if it's a stop, then we can return immediately, as the struct is over.
+ if (type == Types.STOP)
+ {
+ return TSTOP;
+ }
+
+ short fieldId;
+
+ // mask off the 4 MSB of the type header. it could contain a field id delta.
+ short modifier = (short)((type & 0xf0) >> 4);
+ if (modifier == 0)
+ {
+ // not a delta. look ahead for the zigzag varint field id.
+ fieldId = ReadI16();
+ }
+ else
+ {
+ // has a delta. add the delta to the last Read field id.
+ fieldId = (short)(lastFieldId_ + modifier);
+ }
+
+ TField field = new TField("", getTType((byte)(type & 0x0f)), fieldId);
+
+ // if this happens to be a boolean field, the value is encoded in the type
+ if (isBoolType(type))
+ {
+ // save the boolean value in a special instance variable.
+ boolValue_ = (byte)(type & 0x0f) == Types.BOOLEAN_TRUE ? true : false;
+ }
+
+ // push the new field onto the field stack so we can keep the deltas going.
+ lastFieldId_ = field.ID;
+ return field;
+ }
+
+ /// <summary>
+ /// Read a map header off the wire. If the size is zero, skip Reading the key
+ /// and value type. This means that 0-length maps will yield TMaps without the
+ /// "correct" types.
+ /// </summary>
+ public override TMap ReadMapBegin()
+ {
+ int size = (int)ReadVarint32();
+ byte keyAndValueType = size == 0 ? (byte)0 : (byte)ReadByte();
+ return new TMap(getTType((byte)(keyAndValueType >> 4)), getTType((byte)(keyAndValueType & 0xf)), size);
+ }
+
+ /// <summary>
+ /// Read a list header off the wire. If the list size is 0-14, the size will
+ /// be packed into the element type header. If it's a longer list, the 4 MSB
+ /// of the element type header will be 0xF, and a varint will follow with the
+ /// true size.
+ /// </summary>
+ public override TList ReadListBegin()
+ {
+ byte size_and_type = (byte)ReadByte();
+ int size = (size_and_type >> 4) & 0x0f;
+ if (size == 15)
+ {
+ size = (int)ReadVarint32();
+ }
+ TType type = getTType(size_and_type);
+ return new TList(type, size);
+ }
+
+ /// <summary>
+ /// Read a set header off the wire. If the set size is 0-14, the size will
+ /// be packed into the element type header. If it's a longer set, the 4 MSB
+ /// of the element type header will be 0xF, and a varint will follow with the
+ /// true size.
+ /// </summary>
+ public override TSet ReadSetBegin()
+ {
+ return new TSet(ReadListBegin());
+ }
+
+ /// <summary>
+ /// Read a boolean off the wire. If this is a boolean field, the value should
+ /// already have been Read during ReadFieldBegin, so we'll just consume the
+ /// pre-stored value. Otherwise, Read a byte.
+ /// </summary>
+ public override Boolean ReadBool()
+ {
+ if (boolValue_ != null)
+ {
+ bool result = boolValue_.Value;
+ boolValue_ = null;
+ return result;
+ }
+ return ReadByte() == Types.BOOLEAN_TRUE;
+ }
+
+ byte[] byteRawBuf = new byte[1];
+ /// <summary>
+ /// Read a single byte off the wire. Nothing interesting here.
+ /// </summary>
+ public override sbyte ReadByte()
+ {
+ trans.ReadAll(byteRawBuf, 0, 1);
+ return (sbyte)byteRawBuf[0];
+ }
+
+ /// <summary>
+ /// Read an i16 from the wire as a zigzag varint.
+ /// </summary>
+ public override short ReadI16()
+ {
+ return (short)zigzagToInt(ReadVarint32());
+ }
+
+ /// <summary>
+ /// Read an i32 from the wire as a zigzag varint.
+ /// </summary>
+ public override int ReadI32()
+ {
+ return zigzagToInt(ReadVarint32());
+ }
+
+ /// <summary>
+ /// Read an i64 from the wire as a zigzag varint.
+ /// </summary>
+ public override long ReadI64()
+ {
+ return zigzagToLong(ReadVarint64());
+ }
+
+ /// <summary>
+ /// No magic here - just Read a double off the wire.
+ /// </summary>
+ public override double ReadDouble()
+ {
+ byte[] longBits = new byte[8];
+ trans.ReadAll(longBits, 0, 8);
+ return BitConverter.Int64BitsToDouble(bytesToLong(longBits));
+ }
+
+ /// <summary>
+ /// Reads a byte[] (via ReadBinary), and then UTF-8 decodes it.
+ /// </summary>
+ public override string ReadString()
+ {
+ int length = (int)ReadVarint32();
+
+ if (length == 0)
+ {
+ return "";
+ }
+
+ return Encoding.UTF8.GetString(ReadBinary(length));
+ }
+
+ /// <summary>
+ /// Read a byte[] from the wire.
+ /// </summary>
+ public override byte[] ReadBinary()
+ {
+ int length = (int)ReadVarint32();
+ if (length == 0) return new byte[0];
+
+ byte[] buf = new byte[length];
+ trans.ReadAll(buf, 0, length);
+ return buf;
+ }
+
+ /// <summary>
+ /// Read a byte[] of a known length from the wire.
+ /// </summary>
+ private byte[] ReadBinary(int length)
+ {
+ if (length == 0) return new byte[0];
+
+ byte[] buf = new byte[length];
+ trans.ReadAll(buf, 0, length);
+ return buf;
+ }
+
+ //
+ // These methods are here for the struct to call, but don't have any wire
+ // encoding.
+ //
+ public override void ReadMessageEnd() { }
+ public override void ReadFieldEnd() { }
+ public override void ReadMapEnd() { }
+ public override void ReadListEnd() { }
+ public override void ReadSetEnd() { }
+
+ //
+ // Internal Reading methods
+ //
+
+ /// <summary>
+ /// Read an i32 from the wire as a varint. The MSB of each byte is set
+ /// if there is another byte to follow. This can Read up to 5 bytes.
+ /// </summary>
+ private uint ReadVarint32()
+ {
+ uint result = 0;
+ int shift = 0;
+ while (true)
+ {
+ byte b = (byte)ReadByte();
+ result |= (uint)(b & 0x7f) << shift;
+ if ((b & 0x80) != 0x80) break;
+ shift += 7;
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Read an i64 from the wire as a proper varint. The MSB of each byte is set
+ /// if there is another byte to follow. This can Read up to 10 bytes.
+ /// </summary>
+ private ulong ReadVarint64()
+ {
+ int shift = 0;
+ ulong result = 0;
+ while (true)
+ {
+ byte b = (byte)ReadByte();
+ result |= (ulong)(b & 0x7f) << shift;
+ if ((b & 0x80) != 0x80) break;
+ shift += 7;
+ }
+
+ return result;
+ }
+
+ #endregion
+
+ //
+ // encoding helpers
+ //
+
+ /// <summary>
+ /// Convert from zigzag int to int.
+ /// </summary>
+ private int zigzagToInt(uint n)
+ {
+ return (int)(n >> 1) ^ (-(int)(n & 1));
+ }
+
+ /// <summary>
+ /// Convert from zigzag long to long.
+ /// </summary>
+ private long zigzagToLong(ulong n)
+ {
+ return (long)(n >> 1) ^ (-(long)(n & 1));
+ }
+
+ /// <summary>
+ /// Note that it's important that the mask bytes are long literals,
+ /// otherwise they'll default to ints, and when you shift an int left 56 bits,
+ /// you just get a messed up int.
+ /// </summary>
+ private long bytesToLong(byte[] bytes)
+ {
+ return
+ ((bytes[7] & 0xffL) << 56) |
+ ((bytes[6] & 0xffL) << 48) |
+ ((bytes[5] & 0xffL) << 40) |
+ ((bytes[4] & 0xffL) << 32) |
+ ((bytes[3] & 0xffL) << 24) |
+ ((bytes[2] & 0xffL) << 16) |
+ ((bytes[1] & 0xffL) << 8) |
+ ((bytes[0] & 0xffL));
+ }
+
+ //
+ // type testing and converting
+ //
+
+ private Boolean isBoolType(byte b)
+ {
+ int lowerNibble = b & 0x0f;
+ return lowerNibble == Types.BOOLEAN_TRUE || lowerNibble == Types.BOOLEAN_FALSE;
+ }
+
+ /// <summary>
+ /// Given a TCompactProtocol.Types constant, convert it to its corresponding
+ /// TType value.
+ /// </summary>
+ private TType getTType(byte type)
+ {
+ switch ((byte)(type & 0x0f))
+ {
+ case Types.STOP:
+ return TType.Stop;
+ case Types.BOOLEAN_FALSE:
+ case Types.BOOLEAN_TRUE:
+ return TType.Bool;
+ case Types.BYTE:
+ return TType.Byte;
+ case Types.I16:
+ return TType.I16;
+ case Types.I32:
+ return TType.I32;
+ case Types.I64:
+ return TType.I64;
+ case Types.DOUBLE:
+ return TType.Double;
+ case Types.BINARY:
+ return TType.String;
+ case Types.LIST:
+ return TType.List;
+ case Types.SET:
+ return TType.Set;
+ case Types.MAP:
+ return TType.Map;
+ case Types.STRUCT:
+ return TType.Struct;
+ default:
+ throw new TProtocolException("don't know what type: " + (byte)(type & 0x0f));
+ }
+ }
+
+ /// <summary>
+ /// Given a TType value, find the appropriate TCompactProtocol.Types constant.
+ /// </summary>
+ private byte getCompactType(TType ttype)
+ {
+ return ttypeToCompactType[(int)ttype];
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TField.cs b/lib/csharp/src/Protocol/TField.cs
new file mode 100644
index 0000000..8179557
--- /dev/null
+++ b/lib/csharp/src/Protocol/TField.cs
@@ -0,0 +1,62 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Thrift.Protocol
+{
+ public struct TField
+ {
+ private string name;
+ private TType type;
+ private short id;
+
+ public TField(string name, TType type, short id)
+ :this()
+ {
+ this.name = name;
+ this.type = type;
+ this.id = id;
+ }
+
+ public string Name
+ {
+ get { return name; }
+ set { name = value; }
+ }
+
+ public TType Type
+ {
+ get { return type; }
+ set { type = value; }
+ }
+
+ public short ID
+ {
+ get { return id; }
+ set { id = value; }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TJSONProtocol.cs b/lib/csharp/src/Protocol/TJSONProtocol.cs
new file mode 100644
index 0000000..9dbdea9
--- /dev/null
+++ b/lib/csharp/src/Protocol/TJSONProtocol.cs
@@ -0,0 +1,1124 @@
+/**
+ * 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.IO;
+using System.Text;
+using System.Collections.Generic;
+
+using Thrift.Transport;
+using System.Globalization;
+
+namespace Thrift.Protocol
+{
+ /// <summary>
+ /// JSON protocol implementation for thrift.
+ /// <para/>
+ /// This is a full-featured protocol supporting Write and Read.
+ /// <para/>
+ /// Please see the C++ class header for a detailed description of the
+ /// protocol's wire format.
+ /// <para/>
+ /// Adapted from the Java version.
+ /// </summary>
+ public class TJSONProtocol : TProtocol
+ {
+ /// <summary>
+ /// Factory for JSON protocol objects.
+ /// </summary>
+ public class Factory : TProtocolFactory
+ {
+ public TProtocol GetProtocol(TTransport trans)
+ {
+ return new TJSONProtocol(trans);
+ }
+ }
+
+ private static byte[] COMMA = new byte[] { (byte)',' };
+ private static byte[] COLON = new byte[] { (byte)':' };
+ private static byte[] LBRACE = new byte[] { (byte)'{' };
+ private static byte[] RBRACE = new byte[] { (byte)'}' };
+ private static byte[] LBRACKET = new byte[] { (byte)'[' };
+ private static byte[] RBRACKET = new byte[] { (byte)']' };
+ private static byte[] QUOTE = new byte[] { (byte)'"' };
+ private static byte[] BACKSLASH = new byte[] { (byte)'\\' };
+
+ private byte[] ESCSEQ = new byte[] { (byte)'\\', (byte)'u', (byte)'0', (byte)'0' };
+
+ private const long VERSION = 1;
+ private byte[] JSON_CHAR_TABLE = {
+ 0, 0, 0, 0, 0, 0, 0, 0,(byte)'b',(byte)'t',(byte)'n', 0,(byte)'f',(byte)'r', 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 1, 1,(byte)'"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ };
+
+ private char[] ESCAPE_CHARS = "\"\\/bfnrt".ToCharArray();
+
+ private byte[] ESCAPE_CHAR_VALS = {
+ (byte)'"', (byte)'\\', (byte)'/', (byte)'\b', (byte)'\f', (byte)'\n', (byte)'\r', (byte)'\t',
+ };
+
+ private const int DEF_STRING_SIZE = 16;
+
+ private static byte[] NAME_BOOL = new byte[] { (byte)'t', (byte)'f' };
+ private static byte[] NAME_BYTE = new byte[] { (byte)'i', (byte)'8' };
+ private static byte[] NAME_I16 = new byte[] { (byte)'i', (byte)'1', (byte)'6' };
+ private static byte[] NAME_I32 = new byte[] { (byte)'i', (byte)'3', (byte)'2' };
+ private static byte[] NAME_I64 = new byte[] { (byte)'i', (byte)'6', (byte)'4' };
+ private static byte[] NAME_DOUBLE = new byte[] { (byte)'d', (byte)'b', (byte)'l' };
+ private static byte[] NAME_STRUCT = new byte[] { (byte)'r', (byte)'e', (byte)'c' };
+ private static byte[] NAME_STRING = new byte[] { (byte)'s', (byte)'t', (byte)'r' };
+ private static byte[] NAME_MAP = new byte[] { (byte)'m', (byte)'a', (byte)'p' };
+ private static byte[] NAME_LIST = new byte[] { (byte)'l', (byte)'s', (byte)'t' };
+ private static byte[] NAME_SET = new byte[] { (byte)'s', (byte)'e', (byte)'t' };
+
+ private static byte[] GetTypeNameForTypeID(TType typeID)
+ {
+ switch (typeID)
+ {
+ case TType.Bool:
+ return NAME_BOOL;
+ case TType.Byte:
+ return NAME_BYTE;
+ case TType.I16:
+ return NAME_I16;
+ case TType.I32:
+ return NAME_I32;
+ case TType.I64:
+ return NAME_I64;
+ case TType.Double:
+ return NAME_DOUBLE;
+ case TType.String:
+ return NAME_STRING;
+ case TType.Struct:
+ return NAME_STRUCT;
+ case TType.Map:
+ return NAME_MAP;
+ case TType.Set:
+ return NAME_SET;
+ case TType.List:
+ return NAME_LIST;
+ default:
+ throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED,
+ "Unrecognized type");
+ }
+ }
+
+ private static TType GetTypeIDForTypeName(byte[] name)
+ {
+ TType result = TType.Stop;
+ if (name.Length > 1)
+ {
+ switch (name[0])
+ {
+ case (byte)'d':
+ result = TType.Double;
+ break;
+ case (byte)'i':
+ switch (name[1])
+ {
+ case (byte)'8':
+ result = TType.Byte;
+ break;
+ case (byte)'1':
+ result = TType.I16;
+ break;
+ case (byte)'3':
+ result = TType.I32;
+ break;
+ case (byte)'6':
+ result = TType.I64;
+ break;
+ }
+ break;
+ case (byte)'l':
+ result = TType.List;
+ break;
+ case (byte)'m':
+ result = TType.Map;
+ break;
+ case (byte)'r':
+ result = TType.Struct;
+ break;
+ case (byte)'s':
+ if (name[1] == (byte)'t')
+ {
+ result = TType.String;
+ }
+ else if (name[1] == (byte)'e')
+ {
+ result = TType.Set;
+ }
+ break;
+ case (byte)'t':
+ result = TType.Bool;
+ break;
+ }
+ }
+ if (result == TType.Stop)
+ {
+ throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED,
+ "Unrecognized type");
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Base class for tracking JSON contexts that may require
+ /// inserting/Reading additional JSON syntax characters
+ /// This base context does nothing.
+ /// </summary>
+ protected class JSONBaseContext
+ {
+ protected TJSONProtocol proto;
+
+ public JSONBaseContext(TJSONProtocol proto)
+ {
+ this.proto = proto;
+ }
+
+ public virtual void Write() { }
+
+ public virtual void Read() { }
+
+ public virtual bool EscapeNumbers() { return false; }
+ }
+
+ /// <summary>
+ /// Context for JSON lists. Will insert/Read commas before each item except
+ /// for the first one
+ /// </summary>
+ protected class JSONListContext : JSONBaseContext
+ {
+ public JSONListContext(TJSONProtocol protocol)
+ : base(protocol)
+ {
+
+ }
+
+ private bool first = true;
+
+ public override void Write()
+ {
+ if (first)
+ {
+ first = false;
+ }
+ else
+ {
+ proto.trans.Write(COMMA);
+ }
+ }
+
+ public override void Read()
+ {
+ if (first)
+ {
+ first = false;
+ }
+ else
+ {
+ proto.ReadJSONSyntaxChar(COMMA);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Context for JSON records. Will insert/Read colons before the value portion
+ /// of each record pair, and commas before each key except the first. In
+ /// addition, will indicate that numbers in the key position need to be
+ /// escaped in quotes (since JSON keys must be strings).
+ /// </summary>
+ protected class JSONPairContext : JSONBaseContext
+ {
+ public JSONPairContext(TJSONProtocol proto)
+ : base(proto)
+ {
+
+ }
+
+ private bool first = true;
+ private bool colon = true;
+
+ public override void Write()
+ {
+ if (first)
+ {
+ first = false;
+ colon = true;
+ }
+ else
+ {
+ proto.trans.Write(colon ? COLON : COMMA);
+ colon = !colon;
+ }
+ }
+
+ public override void Read()
+ {
+ if (first)
+ {
+ first = false;
+ colon = true;
+ }
+ else
+ {
+ proto.ReadJSONSyntaxChar(colon ? COLON : COMMA);
+ colon = !colon;
+ }
+ }
+
+ public override bool EscapeNumbers()
+ {
+ return colon;
+ }
+ }
+
+ /// <summary>
+ /// Holds up to one byte from the transport
+ /// </summary>
+ protected class LookaheadReader
+ {
+ protected TJSONProtocol proto;
+
+ public LookaheadReader(TJSONProtocol proto)
+ {
+ this.proto = proto;
+ }
+
+ private bool hasData;
+ private byte[] data = new byte[1];
+
+ /// <summary>
+ /// Return and consume the next byte to be Read, either taking it from the
+ /// data buffer if present or getting it from the transport otherwise.
+ /// </summary>
+ public byte Read()
+ {
+ if (hasData)
+ {
+ hasData = false;
+ }
+ else
+ {
+ proto.trans.ReadAll(data, 0, 1);
+ }
+ return data[0];
+ }
+
+ /// <summary>
+ /// Return the next byte to be Read without consuming, filling the data
+ /// buffer if it has not been filled alReady.
+ /// </summary>
+ public byte Peek()
+ {
+ if (!hasData)
+ {
+ proto.trans.ReadAll(data, 0, 1);
+ }
+ hasData = true;
+ return data[0];
+ }
+ }
+
+ // Default encoding
+ protected Encoding utf8Encoding = UTF8Encoding.UTF8;
+
+ // Stack of nested contexts that we may be in
+ protected Stack<JSONBaseContext> contextStack = new Stack<JSONBaseContext>();
+
+ // Current context that we are in
+ protected JSONBaseContext context;
+
+ // Reader that manages a 1-byte buffer
+ protected LookaheadReader reader;
+
+ /// <summary>
+ /// Push a new JSON context onto the stack.
+ /// </summary>
+ protected void PushContext(JSONBaseContext c)
+ {
+ contextStack.Push(context);
+ context = c;
+ }
+
+ /// <summary>
+ /// Pop the last JSON context off the stack
+ /// </summary>
+ protected void PopContext()
+ {
+ context = contextStack.Pop();
+ }
+
+ /// <summary>
+ /// TJSONProtocol Constructor
+ /// </summary>
+ public TJSONProtocol(TTransport trans)
+ : base(trans)
+ {
+ context = new JSONBaseContext(this);
+ reader = new LookaheadReader(this);
+ }
+
+ // Temporary buffer used by several methods
+ private byte[] tempBuffer = new byte[4];
+
+ /// <summary>
+ /// Read a byte that must match b[0]; otherwise an exception is thrown.
+ /// Marked protected to avoid synthetic accessor in JSONListContext.Read
+ /// and JSONPairContext.Read
+ /// </summary>
+ protected void ReadJSONSyntaxChar(byte[] b)
+ {
+ byte ch = reader.Read();
+ if (ch != b[0])
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Unexpected character:" + (char)ch);
+ }
+ }
+
+ /// <summary>
+ /// Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its
+ /// corresponding hex value
+ /// </summary>
+ private static byte HexVal(byte ch)
+ {
+ if ((ch >= '0') && (ch <= '9'))
+ {
+ return (byte)((char)ch - '0');
+ }
+ else if ((ch >= 'a') && (ch <= 'f'))
+ {
+ ch += 10;
+ return (byte)((char)ch - 'a');
+ }
+ else
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Expected hex character");
+ }
+ }
+
+ /// <summary>
+ /// Convert a byte containing a hex value to its corresponding hex character
+ /// </summary>
+ private static byte HexChar(byte val)
+ {
+ val &= 0x0F;
+ if (val < 10)
+ {
+ return (byte)((char)val + '0');
+ }
+ else
+ {
+ val -= 10;
+ return (byte)((char)val + 'a');
+ }
+ }
+
+ /// <summary>
+ /// Write the bytes in array buf as a JSON characters, escaping as needed
+ /// </summary>
+ private void WriteJSONString(byte[] b)
+ {
+ context.Write();
+ trans.Write(QUOTE);
+ int len = b.Length;
+ for (int i = 0; i < len; i++)
+ {
+ if ((b[i] & 0x00FF) >= 0x30)
+ {
+ if (b[i] == BACKSLASH[0])
+ {
+ trans.Write(BACKSLASH);
+ trans.Write(BACKSLASH);
+ }
+ else
+ {
+ trans.Write(b, i, 1);
+ }
+ }
+ else
+ {
+ tempBuffer[0] = JSON_CHAR_TABLE[b[i]];
+ if (tempBuffer[0] == 1)
+ {
+ trans.Write(b, i, 1);
+ }
+ else if (tempBuffer[0] > 1)
+ {
+ trans.Write(BACKSLASH);
+ trans.Write(tempBuffer, 0, 1);
+ }
+ else
+ {
+ trans.Write(ESCSEQ);
+ tempBuffer[0] = HexChar((byte)(b[i] >> 4));
+ tempBuffer[1] = HexChar(b[i]);
+ trans.Write(tempBuffer, 0, 2);
+ }
+ }
+ }
+ trans.Write(QUOTE);
+ }
+
+ /// <summary>
+ /// Write out number as a JSON value. If the context dictates so, it will be
+ /// wrapped in quotes to output as a JSON string.
+ /// </summary>
+ private void WriteJSONInteger(long num)
+ {
+ context.Write();
+ string str = num.ToString();
+
+ bool escapeNum = context.EscapeNumbers();
+ if (escapeNum)
+ trans.Write(QUOTE);
+
+ trans.Write(utf8Encoding.GetBytes(str));
+
+ if (escapeNum)
+ trans.Write(QUOTE);
+ }
+
+ /// <summary>
+ /// Write out a double as a JSON value. If it is NaN or infinity or if the
+ /// context dictates escaping, Write out as JSON string.
+ /// </summary>
+ private void WriteJSONDouble(double num)
+ {
+ context.Write();
+ string str = num.ToString("G17", CultureInfo.InvariantCulture);
+ bool special = false;
+
+ switch (str[0])
+ {
+ case 'N': // NaN
+ case 'I': // Infinity
+ special = true;
+ break;
+ case '-':
+ if (str[1] == 'I')
+ { // -Infinity
+ special = true;
+ }
+ break;
+ }
+
+ bool escapeNum = special || context.EscapeNumbers();
+
+ if (escapeNum)
+ trans.Write(QUOTE);
+
+ trans.Write(utf8Encoding.GetBytes(str));
+
+ if (escapeNum)
+ trans.Write(QUOTE);
+ }
+ /// <summary>
+ /// Write out contents of byte array b as a JSON string with base-64 encoded
+ /// data
+ /// </summary>
+ private void WriteJSONBase64(byte[] b)
+ {
+ context.Write();
+ trans.Write(QUOTE);
+
+ int len = b.Length;
+ int off = 0;
+
+ while (len >= 3)
+ {
+ // Encode 3 bytes at a time
+ TBase64Utils.encode(b, off, 3, tempBuffer, 0);
+ trans.Write(tempBuffer, 0, 4);
+ off += 3;
+ len -= 3;
+ }
+ if (len > 0)
+ {
+ // Encode remainder
+ TBase64Utils.encode(b, off, len, tempBuffer, 0);
+ trans.Write(tempBuffer, 0, len + 1);
+ }
+
+ trans.Write(QUOTE);
+ }
+
+ private void WriteJSONObjectStart()
+ {
+ context.Write();
+ trans.Write(LBRACE);
+ PushContext(new JSONPairContext(this));
+ }
+
+ private void WriteJSONObjectEnd()
+ {
+ PopContext();
+ trans.Write(RBRACE);
+ }
+
+ private void WriteJSONArrayStart()
+ {
+ context.Write();
+ trans.Write(LBRACKET);
+ PushContext(new JSONListContext(this));
+ }
+
+ private void WriteJSONArrayEnd()
+ {
+ PopContext();
+ trans.Write(RBRACKET);
+ }
+
+ public override void WriteMessageBegin(TMessage message)
+ {
+ WriteJSONArrayStart();
+ WriteJSONInteger(VERSION);
+
+ byte[] b = utf8Encoding.GetBytes(message.Name);
+ WriteJSONString(b);
+
+ WriteJSONInteger((long)message.Type);
+ WriteJSONInteger(message.SeqID);
+ }
+
+ public override void WriteMessageEnd()
+ {
+ WriteJSONArrayEnd();
+ }
+
+ public override void WriteStructBegin(TStruct str)
+ {
+ WriteJSONObjectStart();
+ }
+
+ public override void WriteStructEnd()
+ {
+ WriteJSONObjectEnd();
+ }
+
+ public override void WriteFieldBegin(TField field)
+ {
+ WriteJSONInteger(field.ID);
+ WriteJSONObjectStart();
+ WriteJSONString(GetTypeNameForTypeID(field.Type));
+ }
+
+ public override void WriteFieldEnd()
+ {
+ WriteJSONObjectEnd();
+ }
+
+ public override void WriteFieldStop() { }
+
+ public override void WriteMapBegin(TMap map)
+ {
+ WriteJSONArrayStart();
+ WriteJSONString(GetTypeNameForTypeID(map.KeyType));
+ WriteJSONString(GetTypeNameForTypeID(map.ValueType));
+ WriteJSONInteger(map.Count);
+ WriteJSONObjectStart();
+ }
+
+ public override void WriteMapEnd()
+ {
+ WriteJSONObjectEnd();
+ WriteJSONArrayEnd();
+ }
+
+ public override void WriteListBegin(TList list)
+ {
+ WriteJSONArrayStart();
+ WriteJSONString(GetTypeNameForTypeID(list.ElementType));
+ WriteJSONInteger(list.Count);
+ }
+
+ public override void WriteListEnd()
+ {
+ WriteJSONArrayEnd();
+ }
+
+ public override void WriteSetBegin(TSet set)
+ {
+ WriteJSONArrayStart();
+ WriteJSONString(GetTypeNameForTypeID(set.ElementType));
+ WriteJSONInteger(set.Count);
+ }
+
+ public override void WriteSetEnd()
+ {
+ WriteJSONArrayEnd();
+ }
+
+ public override void WriteBool(bool b)
+ {
+ WriteJSONInteger(b ? (long)1 : (long)0);
+ }
+
+ public override void WriteByte(sbyte b)
+ {
+ WriteJSONInteger((long)b);
+ }
+
+ public override void WriteI16(short i16)
+ {
+ WriteJSONInteger((long)i16);
+ }
+
+ public override void WriteI32(int i32)
+ {
+ WriteJSONInteger((long)i32);
+ }
+
+ public override void WriteI64(long i64)
+ {
+ WriteJSONInteger(i64);
+ }
+
+ public override void WriteDouble(double dub)
+ {
+ WriteJSONDouble(dub);
+ }
+
+ public override void WriteString(string str)
+ {
+ byte[] b = utf8Encoding.GetBytes(str);
+ WriteJSONString(b);
+ }
+
+ public override void WriteBinary(byte[] bin)
+ {
+ WriteJSONBase64(bin);
+ }
+
+ /**
+ * Reading methods.
+ */
+
+ /// <summary>
+ /// Read in a JSON string, unescaping as appropriate.. Skip Reading from the
+ /// context if skipContext is true.
+ /// </summary>
+ private byte[] ReadJSONString(bool skipContext)
+ {
+ MemoryStream buffer = new MemoryStream();
+ List<char> codeunits = new List<char>();
+
+
+ if (!skipContext)
+ {
+ context.Read();
+ }
+ ReadJSONSyntaxChar(QUOTE);
+ while (true)
+ {
+ byte ch = reader.Read();
+ if (ch == QUOTE[0])
+ {
+ break;
+ }
+
+ // escaped?
+ if (ch != ESCSEQ[0])
+ {
+ buffer.Write(new byte[] { (byte)ch }, 0, 1);
+ continue;
+ }
+
+ // distinguish between \uXXXX and \?
+ ch = reader.Read();
+ if (ch != ESCSEQ[1]) // control chars like \n
+ {
+ int off = Array.IndexOf(ESCAPE_CHARS, (char)ch);
+ if (off == -1)
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Expected control char");
+ }
+ ch = ESCAPE_CHAR_VALS[off];
+ buffer.Write(new byte[] { (byte)ch }, 0, 1);
+ continue;
+ }
+
+
+ // it's \uXXXX
+ trans.ReadAll(tempBuffer, 0, 4);
+ var wch = (short)((HexVal((byte)tempBuffer[0]) << 12) +
+ (HexVal((byte)tempBuffer[1]) << 8) +
+ (HexVal((byte)tempBuffer[2]) << 4) +
+ HexVal(tempBuffer[3]));
+ if (Char.IsHighSurrogate((char)wch))
+ {
+ if (codeunits.Count > 0)
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Expected low surrogate char");
+ }
+ codeunits.Add((char)wch);
+ }
+ else if (Char.IsLowSurrogate((char)wch))
+ {
+ if (codeunits.Count == 0)
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Expected high surrogate char");
+ }
+ codeunits.Add((char)wch);
+ var tmp = utf8Encoding.GetBytes(codeunits.ToArray());
+ buffer.Write(tmp, 0, tmp.Length);
+ codeunits.Clear();
+ }
+ else
+ {
+ var tmp = utf8Encoding.GetBytes(new char[] { (char)wch });
+ buffer.Write(tmp, 0, tmp.Length);
+ }
+ }
+
+
+ if (codeunits.Count > 0)
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Expected low surrogate char");
+ }
+
+ return buffer.ToArray();
+ }
+
+ /// <summary>
+ /// Return true if the given byte could be a valid part of a JSON number.
+ /// </summary>
+ private bool IsJSONNumeric(byte b)
+ {
+ switch (b)
+ {
+ case (byte)'+':
+ case (byte)'-':
+ case (byte)'.':
+ case (byte)'0':
+ case (byte)'1':
+ case (byte)'2':
+ case (byte)'3':
+ case (byte)'4':
+ case (byte)'5':
+ case (byte)'6':
+ case (byte)'7':
+ case (byte)'8':
+ case (byte)'9':
+ case (byte)'E':
+ case (byte)'e':
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Read in a sequence of characters that are all valid in JSON numbers. Does
+ /// not do a complete regex check to validate that this is actually a number.
+ /// </summary>
+ private string ReadJSONNumericChars()
+ {
+ StringBuilder strbld = new StringBuilder();
+ while (true)
+ {
+ byte ch = reader.Peek();
+ if (!IsJSONNumeric(ch))
+ {
+ break;
+ }
+ strbld.Append((char)reader.Read());
+ }
+ return strbld.ToString();
+ }
+
+ /// <summary>
+ /// Read in a JSON number. If the context dictates, Read in enclosing quotes.
+ /// </summary>
+ private long ReadJSONInteger()
+ {
+ context.Read();
+ if (context.EscapeNumbers())
+ {
+ ReadJSONSyntaxChar(QUOTE);
+ }
+
+ string str = ReadJSONNumericChars();
+ if (context.EscapeNumbers())
+ {
+ ReadJSONSyntaxChar(QUOTE);
+ }
+
+ try
+ {
+ return Int64.Parse(str);
+ }
+ catch (FormatException fex)
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Bad data encounted in numeric data", fex);
+ }
+ }
+
+ /// <summary>
+ /// Read in a JSON double value. Throw if the value is not wrapped in quotes
+ /// when expected or if wrapped in quotes when not expected.
+ /// </summary>
+ private double ReadJSONDouble()
+ {
+ context.Read();
+ if (reader.Peek() == QUOTE[0])
+ {
+ byte[] arr = ReadJSONString(true);
+ double dub = Double.Parse(utf8Encoding.GetString(arr, 0, arr.Length), CultureInfo.InvariantCulture);
+
+ if (!context.EscapeNumbers() && !Double.IsNaN(dub) && !Double.IsInfinity(dub))
+ {
+ // Throw exception -- we should not be in a string in this case
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Numeric data unexpectedly quoted");
+ }
+ return dub;
+ }
+ else
+ {
+ if (context.EscapeNumbers())
+ {
+ // This will throw - we should have had a quote if escapeNum == true
+ ReadJSONSyntaxChar(QUOTE);
+ }
+ try
+ {
+ return Double.Parse(ReadJSONNumericChars(), CultureInfo.InvariantCulture);
+ }
+ catch (FormatException fex)
+ {
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Bad data encounted in numeric data", fex);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Read in a JSON string containing base-64 encoded data and decode it.
+ /// </summary>
+ private byte[] ReadJSONBase64()
+ {
+ byte[] b = ReadJSONString(false);
+ int len = b.Length;
+ int off = 0;
+ int size = 0;
+ // reduce len to ignore fill bytes
+ while ((len > 0) && (b[len - 1] == '='))
+ {
+ --len;
+ }
+ // read & decode full byte triplets = 4 source bytes
+ while (len > 4)
+ {
+ // Decode 4 bytes at a time
+ TBase64Utils.decode(b, off, 4, b, size); // NB: decoded in place
+ off += 4;
+ len -= 4;
+ size += 3;
+ }
+ // Don't decode if we hit the end or got a single leftover byte (invalid
+ // base64 but legal for skip of regular string type)
+ if (len > 1)
+ {
+ // Decode remainder
+ TBase64Utils.decode(b, off, len, b, size); // NB: decoded in place
+ size += len - 1;
+ }
+ // Sadly we must copy the byte[] (any way around this?)
+ byte[] result = new byte[size];
+ Array.Copy(b, 0, result, 0, size);
+ return result;
+ }
+
+ private void ReadJSONObjectStart()
+ {
+ context.Read();
+ ReadJSONSyntaxChar(LBRACE);
+ PushContext(new JSONPairContext(this));
+ }
+
+ private void ReadJSONObjectEnd()
+ {
+ ReadJSONSyntaxChar(RBRACE);
+ PopContext();
+ }
+
+ private void ReadJSONArrayStart()
+ {
+ context.Read();
+ ReadJSONSyntaxChar(LBRACKET);
+ PushContext(new JSONListContext(this));
+ }
+
+ private void ReadJSONArrayEnd()
+ {
+ ReadJSONSyntaxChar(RBRACKET);
+ PopContext();
+ }
+
+ public override TMessage ReadMessageBegin()
+ {
+ TMessage message = new TMessage();
+ ReadJSONArrayStart();
+ if (ReadJSONInteger() != VERSION)
+ {
+ throw new TProtocolException(TProtocolException.BAD_VERSION,
+ "Message contained bad version.");
+ }
+
+ var buf = ReadJSONString(false);
+ message.Name = utf8Encoding.GetString(buf, 0, buf.Length);
+ message.Type = (TMessageType)ReadJSONInteger();
+ message.SeqID = (int)ReadJSONInteger();
+ return message;
+ }
+
+ public override void ReadMessageEnd()
+ {
+ ReadJSONArrayEnd();
+ }
+
+ public override TStruct ReadStructBegin()
+ {
+ ReadJSONObjectStart();
+ return new TStruct();
+ }
+
+ public override void ReadStructEnd()
+ {
+ ReadJSONObjectEnd();
+ }
+
+ public override TField ReadFieldBegin()
+ {
+ TField field = new TField();
+ byte ch = reader.Peek();
+ if (ch == RBRACE[0])
+ {
+ field.Type = TType.Stop;
+ }
+ else
+ {
+ field.ID = (short)ReadJSONInteger();
+ ReadJSONObjectStart();
+ field.Type = GetTypeIDForTypeName(ReadJSONString(false));
+ }
+ return field;
+ }
+
+ public override void ReadFieldEnd()
+ {
+ ReadJSONObjectEnd();
+ }
+
+ public override TMap ReadMapBegin()
+ {
+ TMap map = new TMap();
+ ReadJSONArrayStart();
+ map.KeyType = GetTypeIDForTypeName(ReadJSONString(false));
+ map.ValueType = GetTypeIDForTypeName(ReadJSONString(false));
+ map.Count = (int)ReadJSONInteger();
+ ReadJSONObjectStart();
+ return map;
+ }
+
+ public override void ReadMapEnd()
+ {
+ ReadJSONObjectEnd();
+ ReadJSONArrayEnd();
+ }
+
+ public override TList ReadListBegin()
+ {
+ TList list = new TList();
+ ReadJSONArrayStart();
+ list.ElementType = GetTypeIDForTypeName(ReadJSONString(false));
+ list.Count = (int)ReadJSONInteger();
+ return list;
+ }
+
+ public override void ReadListEnd()
+ {
+ ReadJSONArrayEnd();
+ }
+
+ public override TSet ReadSetBegin()
+ {
+ TSet set = new TSet();
+ ReadJSONArrayStart();
+ set.ElementType = GetTypeIDForTypeName(ReadJSONString(false));
+ set.Count = (int)ReadJSONInteger();
+ return set;
+ }
+
+ public override void ReadSetEnd()
+ {
+ ReadJSONArrayEnd();
+ }
+
+ public override bool ReadBool()
+ {
+ return (ReadJSONInteger() == 0 ? false : true);
+ }
+
+ public override sbyte ReadByte()
+ {
+ return (sbyte)ReadJSONInteger();
+ }
+
+ public override short ReadI16()
+ {
+ return (short)ReadJSONInteger();
+ }
+
+ public override int ReadI32()
+ {
+ return (int)ReadJSONInteger();
+ }
+
+ public override long ReadI64()
+ {
+ return (long)ReadJSONInteger();
+ }
+
+ public override double ReadDouble()
+ {
+ return ReadJSONDouble();
+ }
+
+ public override string ReadString()
+ {
+ var buf = ReadJSONString(false);
+ return utf8Encoding.GetString(buf, 0, buf.Length);
+ }
+
+ public override byte[] ReadBinary()
+ {
+ return ReadJSONBase64();
+ }
+
+ }
+}
diff --git a/lib/csharp/src/Protocol/TList.cs b/lib/csharp/src/Protocol/TList.cs
new file mode 100644
index 0000000..0c8f214
--- /dev/null
+++ b/lib/csharp/src/Protocol/TList.cs
@@ -0,0 +1,54 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Thrift.Protocol
+{
+ public struct TList
+ {
+ private TType elementType;
+ private int count;
+
+ public TList(TType elementType, int count)
+ :this()
+ {
+ this.elementType = elementType;
+ this.count = count;
+ }
+
+ public TType ElementType
+ {
+ get { return elementType; }
+ set { elementType = value; }
+ }
+
+ public int Count
+ {
+ get { return count; }
+ set { count = value; }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TMap.cs b/lib/csharp/src/Protocol/TMap.cs
new file mode 100644
index 0000000..aba9d3a
--- /dev/null
+++ b/lib/csharp/src/Protocol/TMap.cs
@@ -0,0 +1,62 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Thrift.Protocol
+{
+ public struct TMap
+ {
+ private TType keyType;
+ private TType valueType;
+ private int count;
+
+ public TMap(TType keyType, TType valueType, int count)
+ :this()
+ {
+ this.keyType = keyType;
+ this.valueType = valueType;
+ this.count = count;
+ }
+
+ public TType KeyType
+ {
+ get { return keyType; }
+ set { keyType = value; }
+ }
+
+ public TType ValueType
+ {
+ get { return valueType; }
+ set { valueType = value; }
+ }
+
+ public int Count
+ {
+ get { return count; }
+ set { count = value; }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TMessage.cs b/lib/csharp/src/Protocol/TMessage.cs
new file mode 100644
index 0000000..348263c
--- /dev/null
+++ b/lib/csharp/src/Protocol/TMessage.cs
@@ -0,0 +1,62 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Thrift.Protocol
+{
+ public struct TMessage
+ {
+ private string name;
+ private TMessageType type;
+ private int seqID;
+
+ public TMessage(string name, TMessageType type, int seqid)
+ :this()
+ {
+ this.name = name;
+ this.type = type;
+ this.seqID = seqid;
+ }
+
+ public string Name
+ {
+ get { return name; }
+ set { name = value; }
+ }
+
+ public TMessageType Type
+ {
+ get { return type; }
+ set { type = value; }
+ }
+
+ public int SeqID
+ {
+ get { return seqID; }
+ set { seqID = value; }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TMessageType.cs b/lib/csharp/src/Protocol/TMessageType.cs
new file mode 100644
index 0000000..c7091fe
--- /dev/null
+++ b/lib/csharp/src/Protocol/TMessageType.cs
@@ -0,0 +1,31 @@
+/**
+ * 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;
+
+namespace Thrift.Protocol
+{
+ public enum TMessageType
+ {
+ Call = 1,
+ Reply = 2,
+ Exception = 3,
+ Oneway = 4
+ }
+}
diff --git a/lib/csharp/src/Protocol/TMultiplexedProcessor.cs b/lib/csharp/src/Protocol/TMultiplexedProcessor.cs
new file mode 100644
index 0000000..aa91c52
--- /dev/null
+++ b/lib/csharp/src/Protocol/TMultiplexedProcessor.cs
@@ -0,0 +1,183 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Text;
+using Thrift.Transport;
+using System.Collections.Generic;
+using System.IO;
+
+namespace Thrift.Protocol
+{
+ /// <summary>
+ /// <see cref="TMultiplexedProcessor"/> is a <see cref="TProcessor"/> allowing a single <see cref="Thrift.Server.TServer"/>
+ /// to provide multiple services.
+ /// <para/>
+ /// To do so, you instantiate the processor and then register additional processors with it,
+ /// as shown in the following example:
+ /// <para/>
+ /// <code>
+ /// TMultiplexedProcessor processor = new TMultiplexedProcessor();
+ ///
+ /// processor.registerProcessor(
+ /// "Calculator",
+ /// new Calculator.Processor(new CalculatorHandler()));
+ ///
+ /// processor.registerProcessor(
+ /// "WeatherReport",
+ /// new WeatherReport.Processor(new WeatherReportHandler()));
+ ///
+ /// TServerTransport t = new TServerSocket(9090);
+ /// TSimpleServer server = new TSimpleServer(processor, t);
+ ///
+ /// server.serve();
+ /// </code>
+ /// </summary>
+ public class TMultiplexedProcessor : TProcessor
+ {
+ private Dictionary<string, TProcessor> ServiceProcessorMap = new Dictionary<string, TProcessor>();
+
+ /// <summary>
+ /// 'Register' a service with this TMultiplexedProcessor. This allows us to broker
+ /// requests to individual services by using the service name to select them at request time.
+ ///
+ /// Args:
+ /// - serviceName Name of a service, has to be identical to the name
+ /// declared in the Thrift IDL, e.g. "WeatherReport".
+ /// - processor Implementation of a service, usually referred to as "handlers",
+ /// e.g. WeatherReportHandler implementing WeatherReport.Iface.
+ /// </summary>
+ public void RegisterProcessor(string serviceName, TProcessor processor)
+ {
+ ServiceProcessorMap.Add(serviceName, processor);
+ }
+
+
+ private void Fail(TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype, string etxt)
+ {
+ TApplicationException appex = new TApplicationException(extype, etxt);
+
+ TMessage newMessage = new TMessage(message.Name, TMessageType.Exception, message.SeqID);
+
+ oprot.WriteMessageBegin(newMessage);
+ appex.Write(oprot);
+ oprot.WriteMessageEnd();
+ oprot.Transport.Flush();
+ }
+
+
+ /// <summary>
+ /// This implementation of process performs the following steps:
+ ///
+ /// - Read the beginning of the message.
+ /// - Extract the service name from the message.
+ /// - Using the service name to locate the appropriate processor.
+ /// - Dispatch to the processor, with a decorated instance of TProtocol
+ /// that allows readMessageBegin() to return the original TMessage.
+ /// <para/>
+ /// Throws an exception if
+ /// - the message type is not CALL or ONEWAY,
+ /// - the service name was not found in the message, or
+ /// - the service name has not been RegisterProcessor()ed.
+ /// </summary>
+ public bool Process(TProtocol iprot, TProtocol oprot)
+ {
+ /* Use the actual underlying protocol (e.g. TBinaryProtocol) to read the
+ message header. This pulls the message "off the wire", which we'll
+ deal with at the end of this method. */
+
+ try
+ {
+ TMessage message = iprot.ReadMessageBegin();
+
+ if ((message.Type != TMessageType.Call) && (message.Type != TMessageType.Oneway))
+ {
+ Fail(oprot, message,
+ TApplicationException.ExceptionType.InvalidMessageType,
+ "Message type CALL or ONEWAY expected");
+ return false;
+ }
+
+ // Extract the service name
+ int index = message.Name.IndexOf(TMultiplexedProtocol.SEPARATOR);
+ if (index < 0)
+ {
+ Fail(oprot, message,
+ TApplicationException.ExceptionType.InvalidProtocol,
+ "Service name not found in message name: " + message.Name + ". " +
+ "Did you forget to use a TMultiplexProtocol in your client?");
+ return false;
+ }
+
+ // Create a new TMessage, something that can be consumed by any TProtocol
+ string serviceName = message.Name.Substring(0, index);
+ TProcessor actualProcessor;
+ if (!ServiceProcessorMap.TryGetValue(serviceName, out actualProcessor))
+ {
+ Fail(oprot, message,
+ TApplicationException.ExceptionType.InternalError,
+ "Service name not found: " + serviceName + ". " +
+ "Did you forget to call RegisterProcessor()?");
+ return false;
+ }
+
+ // Create a new TMessage, removing the service name
+ TMessage newMessage = new TMessage(
+ message.Name.Substring(serviceName.Length + TMultiplexedProtocol.SEPARATOR.Length),
+ message.Type,
+ message.SeqID);
+
+ // Dispatch processing to the stored processor
+ return actualProcessor.Process(new StoredMessageProtocol(iprot, newMessage), oprot);
+
+ }
+ catch (IOException)
+ {
+ return false; // similar to all other processors
+ }
+
+ }
+
+ /// <summary>
+ /// Our goal was to work with any protocol. In order to do that, we needed
+ /// to allow them to call readMessageBegin() and get a TMessage in exactly
+ /// the standard format, without the service name prepended to TMessage.name.
+ /// </summary>
+ private class StoredMessageProtocol : TProtocolDecorator
+ {
+ TMessage MsgBegin;
+
+ public StoredMessageProtocol(TProtocol protocol, TMessage messageBegin)
+ : base(protocol)
+ {
+ this.MsgBegin = messageBegin;
+ }
+
+ public override TMessage ReadMessageBegin()
+ {
+ return MsgBegin;
+ }
+ }
+
+ }
+}
diff --git a/lib/csharp/src/Protocol/TMultiplexedProtocol.cs b/lib/csharp/src/Protocol/TMultiplexedProtocol.cs
new file mode 100644
index 0000000..1bd420f
--- /dev/null
+++ b/lib/csharp/src/Protocol/TMultiplexedProtocol.cs
@@ -0,0 +1,103 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Text;
+using Thrift.Transport;
+using System.Collections.Generic;
+
+namespace Thrift.Protocol
+{
+
+ /// <summary>
+ /// TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift
+ /// client to communicate with a multiplexing Thrift server, by prepending the service name
+ /// to the function name during function calls.
+ /// <para/>
+ /// NOTE: THIS IS NOT TO BE USED BY SERVERS.
+ /// On the server, use TMultiplexedProcessor to handle requests from a multiplexing client.
+ /// <para/>
+ /// This example uses a single socket transport to invoke two services:
+ /// <code>
+ /// TSocket transport = new TSocket("localhost", 9090);
+ /// transport.open();
+ ///
+ /// TBinaryProtocol protocol = new TBinaryProtocol(transport);
+ ///
+ /// TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator");
+ /// Calculator.Client service = new Calculator.Client(mp);
+ ///
+ /// TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport");
+ /// WeatherReport.Client service2 = new WeatherReport.Client(mp2);
+ ///
+ /// System.out.println(service.add(2,2));
+ /// System.out.println(service2.getTemperature());
+ /// </code>
+ /// </summary>
+ public class TMultiplexedProtocol : TProtocolDecorator
+ {
+
+ /// <summary>
+ /// Used to delimit the service name from the function name.
+ /// </summary>
+ public static string SEPARATOR = ":";
+
+ private string ServiceName;
+
+ /// <summary>
+ /// Wrap the specified protocol, allowing it to be used to communicate with a
+ /// multiplexing server. The <paramref name="serviceName"/> is required as it is
+ /// prepended to the message header so that the multiplexing server can broker
+ /// the function call to the proper service.
+ /// </summary>
+ /// <param name="protocol">Your communication protocol of choice, e.g. <see cref="TBinaryProtocol"/>.</param>
+ /// <param name="serviceName">The service name of the service communicating via this protocol.</param>
+ public TMultiplexedProtocol(TProtocol protocol, string serviceName)
+ : base(protocol)
+ {
+ ServiceName = serviceName;
+ }
+
+ /// <summary>
+ /// Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR.
+ /// </summary>
+ /// <param name="tMessage">The original message.</param>
+ public override void WriteMessageBegin(TMessage tMessage)
+ {
+ switch (tMessage.Type)
+ {
+ case TMessageType.Call:
+ case TMessageType.Oneway:
+ base.WriteMessageBegin(new TMessage(
+ ServiceName + SEPARATOR + tMessage.Name,
+ tMessage.Type,
+ tMessage.SeqID));
+ break;
+
+ default:
+ base.WriteMessageBegin(tMessage);
+ break;
+ }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TProtocol.cs b/lib/csharp/src/Protocol/TProtocol.cs
new file mode 100644
index 0000000..dd7a6e0
--- /dev/null
+++ b/lib/csharp/src/Protocol/TProtocol.cs
@@ -0,0 +1,142 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Text;
+using Thrift.Transport;
+
+namespace Thrift.Protocol
+{
+ public abstract class TProtocol : IDisposable
+ {
+ private const int DEFAULT_RECURSION_DEPTH = 64;
+
+ protected TTransport trans;
+ protected int recursionLimit;
+ protected int recursionDepth;
+
+ protected TProtocol(TTransport trans)
+ {
+ this.trans = trans;
+ this.recursionLimit = DEFAULT_RECURSION_DEPTH;
+ this.recursionDepth = 0;
+ }
+
+ public TTransport Transport
+ {
+ get { return trans; }
+ }
+
+ public int RecursionLimit
+ {
+ get { return recursionLimit; }
+ set { recursionLimit = value; }
+ }
+
+ public void IncrementRecursionDepth()
+ {
+ if (recursionDepth < recursionLimit)
+ ++recursionDepth;
+ else
+ throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded");
+ }
+
+ public void DecrementRecursionDepth()
+ {
+ --recursionDepth;
+ }
+
+ #region " IDisposable Support "
+ private bool _IsDisposed;
+
+ // IDisposable
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!_IsDisposed)
+ {
+ if (disposing)
+ {
+ if (trans is IDisposable)
+ (trans as IDisposable).Dispose();
+ }
+ }
+ _IsDisposed = true;
+ }
+ #endregion
+
+ public abstract void WriteMessageBegin(TMessage message);
+ public abstract void WriteMessageEnd();
+ public abstract void WriteStructBegin(TStruct struc);
+ public abstract void WriteStructEnd();
+ public abstract void WriteFieldBegin(TField field);
+ public abstract void WriteFieldEnd();
+ public abstract void WriteFieldStop();
+ public abstract void WriteMapBegin(TMap map);
+ public abstract void WriteMapEnd();
+ public abstract void WriteListBegin(TList list);
+ public abstract void WriteListEnd();
+ public abstract void WriteSetBegin(TSet set);
+ public abstract void WriteSetEnd();
+ public abstract void WriteBool(bool b);
+ public abstract void WriteByte(sbyte b);
+ public abstract void WriteI16(short i16);
+ public abstract void WriteI32(int i32);
+ public abstract void WriteI64(long i64);
+ public abstract void WriteDouble(double d);
+ public virtual void WriteString(string s)
+ {
+ WriteBinary(Encoding.UTF8.GetBytes(s));
+ }
+ public abstract void WriteBinary(byte[] b);
+
+ public abstract TMessage ReadMessageBegin();
+ public abstract void ReadMessageEnd();
+ public abstract TStruct ReadStructBegin();
+ public abstract void ReadStructEnd();
+ public abstract TField ReadFieldBegin();
+ public abstract void ReadFieldEnd();
+ public abstract TMap ReadMapBegin();
+ public abstract void ReadMapEnd();
+ public abstract TList ReadListBegin();
+ public abstract void ReadListEnd();
+ public abstract TSet ReadSetBegin();
+ public abstract void ReadSetEnd();
+ public abstract bool ReadBool();
+ public abstract sbyte ReadByte();
+ public abstract short ReadI16();
+ public abstract int ReadI32();
+ public abstract long ReadI64();
+ public abstract double ReadDouble();
+ public virtual string ReadString()
+ {
+ var buf = ReadBinary();
+ return Encoding.UTF8.GetString(buf, 0, buf.Length);
+ }
+ public abstract byte[] ReadBinary();
+ }
+}
diff --git a/lib/csharp/src/Protocol/TProtocolDecorator.cs b/lib/csharp/src/Protocol/TProtocolDecorator.cs
new file mode 100644
index 0000000..8600002
--- /dev/null
+++ b/lib/csharp/src/Protocol/TProtocolDecorator.cs
@@ -0,0 +1,261 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Text;
+using Thrift.Transport;
+using System.Collections.Generic;
+
+namespace Thrift.Protocol
+{
+ /// <summary>
+ /// <see cref="TProtocolDecorator"/> forwards all requests to an enclosed <see cref="TProtocol"/> instance,
+ /// providing a way to author concise concrete decorator subclasses. While it has
+ /// no abstract methods, it is marked abstract as a reminder that by itself,
+ /// it does not modify the behaviour of the enclosed <see cref="TProtocol"/>.
+ /// <para/>
+ /// See p.175 of Design Patterns (by Gamma et al.)
+ /// </summary>
+ /// <seealso cref="TMultiplexedProtocol"/>
+ public abstract class TProtocolDecorator : TProtocol
+ {
+ private TProtocol WrappedProtocol;
+
+ /// <summary>
+ /// Encloses the specified protocol.
+ /// </summary>
+ /// <param name="protocol">All operations will be forward to this protocol. Must be non-null.</param>
+ public TProtocolDecorator(TProtocol protocol)
+ : base(protocol.Transport)
+ {
+
+ WrappedProtocol = protocol;
+ }
+
+ public override void WriteMessageBegin(TMessage tMessage)
+ {
+ WrappedProtocol.WriteMessageBegin(tMessage);
+ }
+
+ public override void WriteMessageEnd()
+ {
+ WrappedProtocol.WriteMessageEnd();
+ }
+
+ public override void WriteStructBegin(TStruct tStruct)
+ {
+ WrappedProtocol.WriteStructBegin(tStruct);
+ }
+
+ public override void WriteStructEnd()
+ {
+ WrappedProtocol.WriteStructEnd();
+ }
+
+ public override void WriteFieldBegin(TField tField)
+ {
+ WrappedProtocol.WriteFieldBegin(tField);
+ }
+
+ public override void WriteFieldEnd()
+ {
+ WrappedProtocol.WriteFieldEnd();
+ }
+
+ public override void WriteFieldStop()
+ {
+ WrappedProtocol.WriteFieldStop();
+ }
+
+ public override void WriteMapBegin(TMap tMap)
+ {
+ WrappedProtocol.WriteMapBegin(tMap);
+ }
+
+ public override void WriteMapEnd()
+ {
+ WrappedProtocol.WriteMapEnd();
+ }
+
+ public override void WriteListBegin(TList tList)
+ {
+ WrappedProtocol.WriteListBegin(tList);
+ }
+
+ public override void WriteListEnd()
+ {
+ WrappedProtocol.WriteListEnd();
+ }
+
+ public override void WriteSetBegin(TSet tSet)
+ {
+ WrappedProtocol.WriteSetBegin(tSet);
+ }
+
+ public override void WriteSetEnd()
+ {
+ WrappedProtocol.WriteSetEnd();
+ }
+
+ public override void WriteBool(bool b)
+ {
+ WrappedProtocol.WriteBool(b);
+ }
+
+ public override void WriteByte(sbyte b)
+ {
+ WrappedProtocol.WriteByte(b);
+ }
+
+ public override void WriteI16(short i)
+ {
+ WrappedProtocol.WriteI16(i);
+ }
+
+ public override void WriteI32(int i)
+ {
+ WrappedProtocol.WriteI32(i);
+ }
+
+ public override void WriteI64(long l)
+ {
+ WrappedProtocol.WriteI64(l);
+ }
+
+ public override void WriteDouble(double v)
+ {
+ WrappedProtocol.WriteDouble(v);
+ }
+
+ public override void WriteString(string s)
+ {
+ WrappedProtocol.WriteString(s);
+ }
+
+ public override void WriteBinary(byte[] bytes)
+ {
+ WrappedProtocol.WriteBinary(bytes);
+ }
+
+ public override TMessage ReadMessageBegin()
+ {
+ return WrappedProtocol.ReadMessageBegin();
+ }
+
+ public override void ReadMessageEnd()
+ {
+ WrappedProtocol.ReadMessageEnd();
+ }
+
+ public override TStruct ReadStructBegin()
+ {
+ return WrappedProtocol.ReadStructBegin();
+ }
+
+ public override void ReadStructEnd()
+ {
+ WrappedProtocol.ReadStructEnd();
+ }
+
+ public override TField ReadFieldBegin()
+ {
+ return WrappedProtocol.ReadFieldBegin();
+ }
+
+ public override void ReadFieldEnd()
+ {
+ WrappedProtocol.ReadFieldEnd();
+ }
+
+ public override TMap ReadMapBegin()
+ {
+ return WrappedProtocol.ReadMapBegin();
+ }
+
+ public override void ReadMapEnd()
+ {
+ WrappedProtocol.ReadMapEnd();
+ }
+
+ public override TList ReadListBegin()
+ {
+ return WrappedProtocol.ReadListBegin();
+ }
+
+ public override void ReadListEnd()
+ {
+ WrappedProtocol.ReadListEnd();
+ }
+
+ public override TSet ReadSetBegin()
+ {
+ return WrappedProtocol.ReadSetBegin();
+ }
+
+ public override void ReadSetEnd()
+ {
+ WrappedProtocol.ReadSetEnd();
+ }
+
+ public override bool ReadBool()
+ {
+ return WrappedProtocol.ReadBool();
+ }
+
+ public override sbyte ReadByte()
+ {
+ return WrappedProtocol.ReadByte();
+ }
+
+ public override short ReadI16()
+ {
+ return WrappedProtocol.ReadI16();
+ }
+
+ public override int ReadI32()
+ {
+ return WrappedProtocol.ReadI32();
+ }
+
+ public override long ReadI64()
+ {
+ return WrappedProtocol.ReadI64();
+ }
+
+ public override double ReadDouble()
+ {
+ return WrappedProtocol.ReadDouble();
+ }
+
+ public override string ReadString()
+ {
+ return WrappedProtocol.ReadString();
+ }
+
+ public override byte[] ReadBinary()
+ {
+ return WrappedProtocol.ReadBinary();
+ }
+ }
+
+}
diff --git a/lib/csharp/src/Protocol/TProtocolException.cs b/lib/csharp/src/Protocol/TProtocolException.cs
new file mode 100644
index 0000000..7bef236
--- /dev/null
+++ b/lib/csharp/src/Protocol/TProtocolException.cs
@@ -0,0 +1,67 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+
+namespace Thrift.Protocol
+{
+ public class TProtocolException : TException
+ {
+ public const int UNKNOWN = 0;
+ public const int INVALID_DATA = 1;
+ public const int NEGATIVE_SIZE = 2;
+ public const int SIZE_LIMIT = 3;
+ public const int BAD_VERSION = 4;
+ public const int NOT_IMPLEMENTED = 5;
+ public const int DEPTH_LIMIT = 6;
+
+ protected int type_ = UNKNOWN;
+
+ public TProtocolException()
+ : base()
+ {
+ }
+
+ public TProtocolException(int type, Exception inner = null)
+ : base(string.Empty, inner)
+ {
+ type_ = type;
+ }
+
+ public TProtocolException(int type, string message, Exception inner = null)
+ : base(message, inner)
+ {
+ type_ = type;
+ }
+
+ public TProtocolException(string message, Exception inner = null)
+ : base(message, inner)
+ {
+ }
+
+ public int getType()
+ {
+ return type_;
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TProtocolFactory.cs b/lib/csharp/src/Protocol/TProtocolFactory.cs
new file mode 100644
index 0000000..71360a1
--- /dev/null
+++ b/lib/csharp/src/Protocol/TProtocolFactory.cs
@@ -0,0 +1,33 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using Thrift.Transport;
+
+namespace Thrift.Protocol
+{
+ public interface TProtocolFactory
+ {
+ TProtocol GetProtocol(TTransport trans);
+ }
+}
diff --git a/lib/csharp/src/Protocol/TProtocolUtil.cs b/lib/csharp/src/Protocol/TProtocolUtil.cs
new file mode 100644
index 0000000..d995c6c
--- /dev/null
+++ b/lib/csharp/src/Protocol/TProtocolUtil.cs
@@ -0,0 +1,108 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+
+namespace Thrift.Protocol
+{
+ public static class TProtocolUtil
+ {
+ public static void Skip(TProtocol prot, TType type)
+ {
+ prot.IncrementRecursionDepth();
+ try
+ {
+ switch (type)
+ {
+ case TType.Bool:
+ prot.ReadBool();
+ break;
+ case TType.Byte:
+ prot.ReadByte();
+ break;
+ case TType.I16:
+ prot.ReadI16();
+ break;
+ case TType.I32:
+ prot.ReadI32();
+ break;
+ case TType.I64:
+ prot.ReadI64();
+ break;
+ case TType.Double:
+ prot.ReadDouble();
+ break;
+ case TType.String:
+ // Don't try to decode the string, just skip it.
+ prot.ReadBinary();
+ break;
+ case TType.Struct:
+ prot.ReadStructBegin();
+ while (true)
+ {
+ TField field = prot.ReadFieldBegin();
+ if (field.Type == TType.Stop)
+ {
+ break;
+ }
+ Skip(prot, field.Type);
+ prot.ReadFieldEnd();
+ }
+ prot.ReadStructEnd();
+ break;
+ case TType.Map:
+ TMap map = prot.ReadMapBegin();
+ for (int i = 0; i < map.Count; i++)
+ {
+ Skip(prot, map.KeyType);
+ Skip(prot, map.ValueType);
+ }
+ prot.ReadMapEnd();
+ break;
+ case TType.Set:
+ TSet set = prot.ReadSetBegin();
+ for (int i = 0; i < set.Count; i++)
+ {
+ Skip(prot, set.ElementType);
+ }
+ prot.ReadSetEnd();
+ break;
+ case TType.List:
+ TList list = prot.ReadListBegin();
+ for (int i = 0; i < list.Count; i++)
+ {
+ Skip(prot, list.ElementType);
+ }
+ prot.ReadListEnd();
+ break;
+ default:
+ throw new TProtocolException(TProtocolException.INVALID_DATA, "Unknown data type " + type.ToString("d"));
+ }
+ }
+ finally
+ {
+ prot.DecrementRecursionDepth();
+ }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TSet.cs b/lib/csharp/src/Protocol/TSet.cs
new file mode 100644
index 0000000..a918ab5
--- /dev/null
+++ b/lib/csharp/src/Protocol/TSet.cs
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Thrift.Protocol
+{
+ public struct TSet
+ {
+ private TType elementType;
+ private int count;
+
+ public TSet(TType elementType, int count)
+ :this()
+ {
+ this.elementType = elementType;
+ this.count = count;
+ }
+
+ public TSet(TList list)
+ : this(list.ElementType, list.Count)
+ {
+ }
+
+ public TType ElementType
+ {
+ get { return elementType; }
+ set { elementType = value; }
+ }
+
+ public int Count
+ {
+ get { return count; }
+ set { count = value; }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TStruct.cs b/lib/csharp/src/Protocol/TStruct.cs
new file mode 100644
index 0000000..f4844a4
--- /dev/null
+++ b/lib/csharp/src/Protocol/TStruct.cs
@@ -0,0 +1,46 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Thrift.Protocol
+{
+ public struct TStruct
+ {
+ private string name;
+
+ public TStruct(string name)
+ :this()
+ {
+ this.name = name;
+ }
+
+ public string Name
+ {
+ get { return name; }
+ set { name = value; }
+ }
+ }
+}
diff --git a/lib/csharp/src/Protocol/TType.cs b/lib/csharp/src/Protocol/TType.cs
new file mode 100644
index 0000000..9ce915e
--- /dev/null
+++ b/lib/csharp/src/Protocol/TType.cs
@@ -0,0 +1,44 @@
+/**
+ * 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.
+ *
+ * Contains some contributions under the Thrift Software License.
+ * Please see doc/old-thrift-license.txt in the Thrift distribution for
+ * details.
+ */
+
+using System;
+
+namespace Thrift.Protocol
+{
+ public enum TType : byte
+ {
+ Stop = 0,
+ Void = 1,
+ Bool = 2,
+ Byte = 3,
+ Double = 4,
+ I16 = 6,
+ I32 = 8,
+ I64 = 10,
+ String = 11,
+ Struct = 12,
+ Map = 13,
+ Set = 14,
+ List = 15
+ }
+}