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