Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Text; |
| 20 | using System.Threading; |
| 21 | using System.Threading.Tasks; |
| 22 | using Thrift.Protocol.Entities; |
| 23 | using Thrift.Transport; |
| 24 | |
| 25 | namespace Thrift.Protocol |
| 26 | { |
| 27 | // ReSharper disable once InconsistentNaming |
| 28 | public abstract class TProtocol : IDisposable |
| 29 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 30 | private bool _isDisposed; |
| 31 | protected int RecursionDepth; |
| 32 | |
| 33 | protected TTransport Trans; |
| 34 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 35 | protected static readonly TStruct AnonymousStruct = new TStruct(string.Empty); |
| 36 | protected static readonly TField StopField = new TField() { Type = TType.Stop }; |
| 37 | |
| 38 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 39 | protected TProtocol(TTransport trans) |
| 40 | { |
| 41 | Trans = trans; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 42 | RecursionLimit = trans.Configuration.RecursionLimit; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 43 | RecursionDepth = 0; |
| 44 | } |
| 45 | |
| 46 | public TTransport Transport => Trans; |
| 47 | |
| 48 | protected int RecursionLimit { get; set; } |
| 49 | |
| 50 | public void Dispose() |
| 51 | { |
| 52 | Dispose(true); |
| 53 | } |
| 54 | |
| 55 | public void IncrementRecursionDepth() |
| 56 | { |
| 57 | if (RecursionDepth < RecursionLimit) |
| 58 | { |
| 59 | ++RecursionDepth; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public void DecrementRecursionDepth() |
| 68 | { |
| 69 | --RecursionDepth; |
| 70 | } |
| 71 | |
| 72 | protected virtual void Dispose(bool disposing) |
| 73 | { |
| 74 | if (!_isDisposed) |
| 75 | { |
| 76 | if (disposing) |
| 77 | { |
| 78 | (Trans as IDisposable)?.Dispose(); |
| 79 | } |
| 80 | } |
| 81 | _isDisposed = true; |
| 82 | } |
| 83 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 84 | |
| 85 | protected void CheckReadBytesAvailable(TSet set) |
| 86 | { |
| 87 | Transport.CheckReadBytesAvailable(set.Count * GetMinSerializedSize(set.ElementType)); |
| 88 | } |
| 89 | |
| 90 | protected void CheckReadBytesAvailable(TList list) |
| 91 | { |
| 92 | Transport.CheckReadBytesAvailable(list.Count * GetMinSerializedSize(list.ElementType)); |
| 93 | } |
| 94 | |
| 95 | protected void CheckReadBytesAvailable(TMap map) |
| 96 | { |
| 97 | var elmSize = GetMinSerializedSize(map.KeyType) + GetMinSerializedSize(map.ValueType); |
| 98 | Transport.CheckReadBytesAvailable(map.Count * elmSize); |
| 99 | } |
| 100 | |
| 101 | // Returns the minimum amount of bytes needed to store the smallest possible instance of TType. |
| 102 | public abstract int GetMinSerializedSize(TType type); |
| 103 | |
| 104 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 105 | public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 106 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 107 | public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 108 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 109 | public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 110 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 111 | public abstract Task WriteStructEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 112 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 113 | public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken = default); |
| 114 | |
| 115 | public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken = default); |
| 116 | |
| 117 | public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 118 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 119 | public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 120 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 121 | public abstract Task WriteMapEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 122 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 123 | public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 124 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 125 | public abstract Task WriteListEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 126 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 127 | public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 128 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 129 | public abstract Task WriteSetEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 130 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 131 | public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 132 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 133 | public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 134 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 135 | public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 136 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 137 | public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 138 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 139 | public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 140 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 141 | public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 142 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 143 | public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken = default) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 144 | { |
| 145 | var bytes = Encoding.UTF8.GetBytes(s); |
| 146 | await WriteBinaryAsync(bytes, cancellationToken); |
| 147 | } |
| 148 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 149 | public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 150 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 151 | public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 152 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 153 | public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 154 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 155 | public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 156 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 157 | public abstract Task ReadStructEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 158 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 159 | public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 160 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 161 | public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 162 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 163 | public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 164 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 165 | public abstract Task ReadMapEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 166 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 167 | public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 168 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 169 | public abstract Task ReadListEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 170 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 171 | public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 172 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 173 | public abstract Task ReadSetEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 174 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 175 | public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 176 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 177 | public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 178 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 179 | public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 180 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 181 | public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 182 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 183 | public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 184 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 185 | public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 186 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 187 | public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken = default) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 188 | { |
| 189 | var buf = await ReadBinaryAsync(cancellationToken); |
| 190 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 191 | } |
| 192 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 193 | public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 194 | } |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 195 | } |