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 | { |
| 30 | public const int DefaultRecursionDepth = 64; |
| 31 | private bool _isDisposed; |
| 32 | protected int RecursionDepth; |
| 33 | |
| 34 | protected TTransport Trans; |
| 35 | |
| 36 | protected TProtocol(TTransport trans) |
| 37 | { |
| 38 | Trans = trans; |
| 39 | RecursionLimit = DefaultRecursionDepth; |
| 40 | RecursionDepth = 0; |
| 41 | } |
| 42 | |
| 43 | public TTransport Transport => Trans; |
| 44 | |
| 45 | protected int RecursionLimit { get; set; } |
| 46 | |
| 47 | public void Dispose() |
| 48 | { |
| 49 | Dispose(true); |
| 50 | } |
| 51 | |
| 52 | public void IncrementRecursionDepth() |
| 53 | { |
| 54 | if (RecursionDepth < RecursionLimit) |
| 55 | { |
| 56 | ++RecursionDepth; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public void DecrementRecursionDepth() |
| 65 | { |
| 66 | --RecursionDepth; |
| 67 | } |
| 68 | |
| 69 | protected virtual void Dispose(bool disposing) |
| 70 | { |
| 71 | if (!_isDisposed) |
| 72 | { |
| 73 | if (disposing) |
| 74 | { |
| 75 | (Trans as IDisposable)?.Dispose(); |
| 76 | } |
| 77 | } |
| 78 | _isDisposed = true; |
| 79 | } |
| 80 | |
| 81 | public virtual async Task WriteMessageBeginAsync(TMessage message) |
| 82 | { |
| 83 | await WriteMessageBeginAsync(message, CancellationToken.None); |
| 84 | } |
| 85 | |
| 86 | public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken); |
| 87 | |
| 88 | public virtual async Task WriteMessageEndAsync() |
| 89 | { |
| 90 | await WriteMessageEndAsync(CancellationToken.None); |
| 91 | } |
| 92 | |
| 93 | public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken); |
| 94 | |
| 95 | public virtual async Task WriteStructBeginAsync(TStruct @struct) |
| 96 | { |
| 97 | await WriteStructBeginAsync(@struct, CancellationToken.None); |
| 98 | } |
| 99 | |
| 100 | public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken); |
| 101 | |
| 102 | public virtual async Task WriteStructEndAsync() |
| 103 | { |
| 104 | await WriteStructEndAsync(CancellationToken.None); |
| 105 | } |
| 106 | |
| 107 | public abstract Task WriteStructEndAsync(CancellationToken cancellationToken); |
| 108 | |
| 109 | public virtual async Task WriteFieldBeginAsync(TField field) |
| 110 | { |
| 111 | await WriteFieldBeginAsync(field, CancellationToken.None); |
| 112 | } |
| 113 | |
| 114 | public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken); |
| 115 | |
| 116 | public virtual async Task WriteFieldEndAsync() |
| 117 | { |
| 118 | await WriteFieldEndAsync(CancellationToken.None); |
| 119 | } |
| 120 | |
| 121 | public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken); |
| 122 | |
| 123 | public virtual async Task WriteFieldStopAsync() |
| 124 | { |
| 125 | await WriteFieldStopAsync(CancellationToken.None); |
| 126 | } |
| 127 | |
| 128 | public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken); |
| 129 | |
| 130 | public virtual async Task WriteMapBeginAsync(TMap map) |
| 131 | { |
| 132 | await WriteMapBeginAsync(map, CancellationToken.None); |
| 133 | } |
| 134 | |
| 135 | public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken); |
| 136 | |
| 137 | public virtual async Task WriteMapEndAsync() |
| 138 | { |
| 139 | await WriteMapEndAsync(CancellationToken.None); |
| 140 | } |
| 141 | |
| 142 | public abstract Task WriteMapEndAsync(CancellationToken cancellationToken); |
| 143 | |
| 144 | public virtual async Task WriteListBeginAsync(TList list) |
| 145 | { |
| 146 | await WriteListBeginAsync(list, CancellationToken.None); |
| 147 | } |
| 148 | |
| 149 | public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken); |
| 150 | |
| 151 | public virtual async Task WriteListEndAsync() |
| 152 | { |
| 153 | await WriteListEndAsync(CancellationToken.None); |
| 154 | } |
| 155 | |
| 156 | public abstract Task WriteListEndAsync(CancellationToken cancellationToken); |
| 157 | |
| 158 | public virtual async Task WriteSetBeginAsync(TSet set) |
| 159 | { |
| 160 | await WriteSetBeginAsync(set, CancellationToken.None); |
| 161 | } |
| 162 | |
| 163 | public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken); |
| 164 | |
| 165 | public virtual async Task WriteSetEndAsync() |
| 166 | { |
| 167 | await WriteSetEndAsync(CancellationToken.None); |
| 168 | } |
| 169 | |
| 170 | public abstract Task WriteSetEndAsync(CancellationToken cancellationToken); |
| 171 | |
| 172 | public virtual async Task WriteBoolAsync(bool b) |
| 173 | { |
| 174 | await WriteBoolAsync(b, CancellationToken.None); |
| 175 | } |
| 176 | |
| 177 | public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken); |
| 178 | |
| 179 | public virtual async Task WriteByteAsync(sbyte b) |
| 180 | { |
| 181 | await WriteByteAsync(b, CancellationToken.None); |
| 182 | } |
| 183 | |
| 184 | public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken); |
| 185 | |
| 186 | public virtual async Task WriteI16Async(short i16) |
| 187 | { |
| 188 | await WriteI16Async(i16, CancellationToken.None); |
| 189 | } |
| 190 | |
| 191 | public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken); |
| 192 | |
| 193 | public virtual async Task WriteI32Async(int i32) |
| 194 | { |
| 195 | await WriteI32Async(i32, CancellationToken.None); |
| 196 | } |
| 197 | |
| 198 | public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken); |
| 199 | |
| 200 | public virtual async Task WriteI64Async(long i64) |
| 201 | { |
| 202 | await WriteI64Async(i64, CancellationToken.None); |
| 203 | } |
| 204 | |
| 205 | public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken); |
| 206 | |
| 207 | public virtual async Task WriteDoubleAsync(double d) |
| 208 | { |
| 209 | await WriteDoubleAsync(d, CancellationToken.None); |
| 210 | } |
| 211 | |
| 212 | public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken); |
| 213 | |
| 214 | public virtual async Task WriteStringAsync(string s) |
| 215 | { |
| 216 | await WriteStringAsync(s, CancellationToken.None); |
| 217 | } |
| 218 | |
| 219 | public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken) |
| 220 | { |
| 221 | var bytes = Encoding.UTF8.GetBytes(s); |
| 222 | await WriteBinaryAsync(bytes, cancellationToken); |
| 223 | } |
| 224 | |
| 225 | public virtual async Task WriteBinaryAsync(byte[] bytes) |
| 226 | { |
| 227 | await WriteBinaryAsync(bytes, CancellationToken.None); |
| 228 | } |
| 229 | |
| 230 | public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken); |
| 231 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 232 | public virtual async ValueTask<TMessage> ReadMessageBeginAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 233 | { |
| 234 | return await ReadMessageBeginAsync(CancellationToken.None); |
| 235 | } |
| 236 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 237 | public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 238 | |
| 239 | public virtual async Task ReadMessageEndAsync() |
| 240 | { |
| 241 | await ReadMessageEndAsync(CancellationToken.None); |
| 242 | } |
| 243 | |
| 244 | public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken); |
| 245 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 246 | public virtual async ValueTask<TStruct> ReadStructBeginAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 247 | { |
| 248 | return await ReadStructBeginAsync(CancellationToken.None); |
| 249 | } |
| 250 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 251 | public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 252 | |
| 253 | public virtual async Task ReadStructEndAsync() |
| 254 | { |
| 255 | await ReadStructEndAsync(CancellationToken.None); |
| 256 | } |
| 257 | |
| 258 | public abstract Task ReadStructEndAsync(CancellationToken cancellationToken); |
| 259 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 260 | public virtual async ValueTask<TField> ReadFieldBeginAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 261 | { |
| 262 | return await ReadFieldBeginAsync(CancellationToken.None); |
| 263 | } |
| 264 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 265 | public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 266 | |
| 267 | public virtual async Task ReadFieldEndAsync() |
| 268 | { |
| 269 | await ReadFieldEndAsync(CancellationToken.None); |
| 270 | } |
| 271 | |
| 272 | public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken); |
| 273 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 274 | public virtual async ValueTask<TMap> ReadMapBeginAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 275 | { |
| 276 | return await ReadMapBeginAsync(CancellationToken.None); |
| 277 | } |
| 278 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 279 | public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 280 | |
| 281 | public virtual async Task ReadMapEndAsync() |
| 282 | { |
| 283 | await ReadMapEndAsync(CancellationToken.None); |
| 284 | } |
| 285 | |
| 286 | public abstract Task ReadMapEndAsync(CancellationToken cancellationToken); |
| 287 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 288 | public virtual async ValueTask<TList> ReadListBeginAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 289 | { |
| 290 | return await ReadListBeginAsync(CancellationToken.None); |
| 291 | } |
| 292 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 293 | public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 294 | |
| 295 | public virtual async Task ReadListEndAsync() |
| 296 | { |
| 297 | await ReadListEndAsync(CancellationToken.None); |
| 298 | } |
| 299 | |
| 300 | public abstract Task ReadListEndAsync(CancellationToken cancellationToken); |
| 301 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 302 | public virtual async ValueTask<TSet> ReadSetBeginAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 303 | { |
| 304 | return await ReadSetBeginAsync(CancellationToken.None); |
| 305 | } |
| 306 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 307 | public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 308 | |
| 309 | public virtual async Task ReadSetEndAsync() |
| 310 | { |
| 311 | await ReadSetEndAsync(CancellationToken.None); |
| 312 | } |
| 313 | |
| 314 | public abstract Task ReadSetEndAsync(CancellationToken cancellationToken); |
| 315 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 316 | public virtual async ValueTask<bool> ReadBoolAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 317 | { |
| 318 | return await ReadBoolAsync(CancellationToken.None); |
| 319 | } |
| 320 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 321 | public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 322 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 323 | public virtual async ValueTask<sbyte> ReadByteAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 324 | { |
| 325 | return await ReadByteAsync(CancellationToken.None); |
| 326 | } |
| 327 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 328 | public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 329 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 330 | public virtual async ValueTask<short> ReadI16Async() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 331 | { |
| 332 | return await ReadI16Async(CancellationToken.None); |
| 333 | } |
| 334 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 335 | public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 336 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 337 | public virtual async ValueTask<int> ReadI32Async() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 338 | { |
| 339 | return await ReadI32Async(CancellationToken.None); |
| 340 | } |
| 341 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 342 | public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 343 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 344 | public virtual async ValueTask<long> ReadI64Async() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 345 | { |
| 346 | return await ReadI64Async(CancellationToken.None); |
| 347 | } |
| 348 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 349 | public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 350 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 351 | public virtual async ValueTask<double> ReadDoubleAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 352 | { |
| 353 | return await ReadDoubleAsync(CancellationToken.None); |
| 354 | } |
| 355 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 356 | public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 357 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 358 | public virtual async ValueTask<string> ReadStringAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 359 | { |
| 360 | return await ReadStringAsync(CancellationToken.None); |
| 361 | } |
| 362 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 363 | public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 364 | { |
| 365 | var buf = await ReadBinaryAsync(cancellationToken); |
| 366 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 367 | } |
| 368 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 369 | public virtual async ValueTask<byte[]> ReadBinaryAsync() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 370 | { |
| 371 | return await ReadBinaryAsync(CancellationToken.None); |
| 372 | } |
| 373 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 374 | public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 375 | } |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame^] | 376 | } |