Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 19 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 23 | |
| 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using System.Text; |
| 26 | using Thrift.Transport; |
| 27 | |
| 28 | namespace Thrift.Protocol |
| 29 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 30 | public abstract class TProtocol : IDisposable |
| 31 | { |
Jens Geyer | 4018036 | 2015-07-09 23:02:46 +0200 | [diff] [blame] | 32 | private const int DEFAULT_RECURSION_DEPTH = 64; |
| 33 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 34 | protected TTransport trans; |
Jens Geyer | 4018036 | 2015-07-09 23:02:46 +0200 | [diff] [blame] | 35 | protected int recursionLimit; |
| 36 | protected int recursionDepth; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 37 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 38 | protected TProtocol(TTransport trans) |
| 39 | { |
| 40 | this.trans = trans; |
Jens Geyer | 4018036 | 2015-07-09 23:02:46 +0200 | [diff] [blame] | 41 | this.recursionLimit = DEFAULT_RECURSION_DEPTH; |
| 42 | this.recursionDepth = 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 43 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 44 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 45 | public TTransport Transport |
| 46 | { |
| 47 | get { return trans; } |
| 48 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 49 | |
Jens Geyer | 4018036 | 2015-07-09 23:02:46 +0200 | [diff] [blame] | 50 | public int RecursionLimit |
| 51 | { |
| 52 | get { return recursionLimit; } |
| 53 | set { recursionLimit = value; } |
| 54 | } |
| 55 | |
| 56 | public void IncrementRecursionDepth() |
| 57 | { |
| 58 | if (recursionDepth < recursionLimit) |
| 59 | ++recursionDepth; |
| 60 | else |
| 61 | throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); |
| 62 | } |
| 63 | |
| 64 | public void DecrementRecursionDepth() |
| 65 | { |
| 66 | --recursionDepth; |
| 67 | } |
| 68 | |
Jens Geyer | 102bca4 | 2013-06-25 22:21:29 +0200 | [diff] [blame] | 69 | #region " IDisposable Support " |
| 70 | private bool _IsDisposed; |
| 71 | |
| 72 | // IDisposable |
| 73 | public void Dispose() |
| 74 | { |
| 75 | Dispose(true); |
| 76 | } |
| 77 | |
| 78 | protected virtual void Dispose(bool disposing) |
| 79 | { |
| 80 | if (!_IsDisposed) |
| 81 | { |
| 82 | if (disposing) |
| 83 | { |
| 84 | if (trans is IDisposable) |
| 85 | (trans as IDisposable).Dispose(); |
| 86 | } |
| 87 | } |
| 88 | _IsDisposed = true; |
| 89 | } |
| 90 | #endregion |
| 91 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 92 | public abstract void WriteMessageBegin(TMessage message); |
| 93 | public abstract void WriteMessageEnd(); |
| 94 | public abstract void WriteStructBegin(TStruct struc); |
| 95 | public abstract void WriteStructEnd(); |
| 96 | public abstract void WriteFieldBegin(TField field); |
| 97 | public abstract void WriteFieldEnd(); |
| 98 | public abstract void WriteFieldStop(); |
| 99 | public abstract void WriteMapBegin(TMap map); |
| 100 | public abstract void WriteMapEnd(); |
| 101 | public abstract void WriteListBegin(TList list); |
| 102 | public abstract void WriteListEnd(); |
| 103 | public abstract void WriteSetBegin(TSet set); |
| 104 | public abstract void WriteSetEnd(); |
| 105 | public abstract void WriteBool(bool b); |
| 106 | public abstract void WriteByte(sbyte b); |
| 107 | public abstract void WriteI16(short i16); |
| 108 | public abstract void WriteI32(int i32); |
| 109 | public abstract void WriteI64(long i64); |
| 110 | public abstract void WriteDouble(double d); |
| 111 | public virtual void WriteString(string s) { |
| 112 | WriteBinary(Encoding.UTF8.GetBytes(s)); |
| 113 | } |
| 114 | public abstract void WriteBinary(byte[] b); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 115 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 116 | public abstract TMessage ReadMessageBegin(); |
| 117 | public abstract void ReadMessageEnd(); |
| 118 | public abstract TStruct ReadStructBegin(); |
| 119 | public abstract void ReadStructEnd(); |
| 120 | public abstract TField ReadFieldBegin(); |
| 121 | public abstract void ReadFieldEnd(); |
| 122 | public abstract TMap ReadMapBegin(); |
| 123 | public abstract void ReadMapEnd(); |
| 124 | public abstract TList ReadListBegin(); |
| 125 | public abstract void ReadListEnd(); |
| 126 | public abstract TSet ReadSetBegin(); |
| 127 | public abstract void ReadSetEnd(); |
| 128 | public abstract bool ReadBool(); |
| 129 | public abstract sbyte ReadByte(); |
| 130 | public abstract short ReadI16(); |
| 131 | public abstract int ReadI32(); |
| 132 | public abstract long ReadI64(); |
| 133 | public abstract double ReadDouble(); |
| 134 | public virtual string ReadString() { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 135 | var buf = ReadBinary(); |
| 136 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 137 | } |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 138 | public abstract byte[] ReadBinary(); |
| 139 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 140 | } |