Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 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.Threading; |
| 19 | using System.Threading.Tasks; |
| 20 | using Thrift.Protocol.Entities; |
| 21 | |
| 22 | namespace Thrift.Protocol.Utilities |
| 23 | { |
| 24 | // ReSharper disable once InconsistentNaming |
| 25 | public static class TProtocolUtil |
| 26 | { |
| 27 | public static async Task SkipAsync(TProtocol protocol, TType type, CancellationToken cancellationToken) |
| 28 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame^] | 29 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 30 | |
| 31 | protocol.IncrementRecursionDepth(); |
| 32 | try |
| 33 | { |
| 34 | switch (type) |
| 35 | { |
| 36 | case TType.Bool: |
| 37 | await protocol.ReadBoolAsync(cancellationToken); |
| 38 | break; |
| 39 | case TType.Byte: |
| 40 | await protocol.ReadByteAsync(cancellationToken); |
| 41 | break; |
| 42 | case TType.I16: |
| 43 | await protocol.ReadI16Async(cancellationToken); |
| 44 | break; |
| 45 | case TType.I32: |
| 46 | await protocol.ReadI32Async(cancellationToken); |
| 47 | break; |
| 48 | case TType.I64: |
| 49 | await protocol.ReadI64Async(cancellationToken); |
| 50 | break; |
| 51 | case TType.Double: |
| 52 | await protocol.ReadDoubleAsync(cancellationToken); |
| 53 | break; |
| 54 | case TType.String: |
| 55 | // Don't try to decode the string, just skip it. |
| 56 | await protocol.ReadBinaryAsync(cancellationToken); |
| 57 | break; |
| 58 | case TType.Struct: |
| 59 | await protocol.ReadStructBeginAsync(cancellationToken); |
| 60 | while (true) |
| 61 | { |
| 62 | var field = await protocol.ReadFieldBeginAsync(cancellationToken); |
| 63 | if (field.Type == TType.Stop) |
| 64 | { |
| 65 | break; |
| 66 | } |
| 67 | await SkipAsync(protocol, field.Type, cancellationToken); |
| 68 | await protocol.ReadFieldEndAsync(cancellationToken); |
| 69 | } |
| 70 | await protocol.ReadStructEndAsync(cancellationToken); |
| 71 | break; |
| 72 | case TType.Map: |
| 73 | var map = await protocol.ReadMapBeginAsync(cancellationToken); |
| 74 | for (var i = 0; i < map.Count; i++) |
| 75 | { |
| 76 | await SkipAsync(protocol, map.KeyType, cancellationToken); |
| 77 | await SkipAsync(protocol, map.ValueType, cancellationToken); |
| 78 | } |
| 79 | await protocol.ReadMapEndAsync(cancellationToken); |
| 80 | break; |
| 81 | case TType.Set: |
| 82 | var set = await protocol.ReadSetBeginAsync(cancellationToken); |
| 83 | for (var i = 0; i < set.Count; i++) |
| 84 | { |
| 85 | await SkipAsync(protocol, set.ElementType, cancellationToken); |
| 86 | } |
| 87 | await protocol.ReadSetEndAsync(cancellationToken); |
| 88 | break; |
| 89 | case TType.List: |
| 90 | var list = await protocol.ReadListBeginAsync(cancellationToken); |
| 91 | for (var i = 0; i < list.Count; i++) |
| 92 | { |
| 93 | await SkipAsync(protocol, list.ElementType, cancellationToken); |
| 94 | } |
| 95 | await protocol.ReadListEndAsync(cancellationToken); |
| 96 | break; |
| 97 | default: |
| 98 | throw new TProtocolException(TProtocolException.INVALID_DATA, "Unknown data type " + type.ToString("d")); |
| 99 | } |
| 100 | } |
| 101 | finally |
| 102 | { |
| 103 | protocol.DecrementRecursionDepth(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |