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); |
Jens Geyer | 561bc9a | 2022-01-26 22:38:04 +0100 | [diff] [blame] | 53 | GC.SuppressFinalize(this); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public void IncrementRecursionDepth() |
| 57 | { |
| 58 | if (RecursionDepth < RecursionLimit) |
| 59 | { |
| 60 | ++RecursionDepth; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public void DecrementRecursionDepth() |
| 69 | { |
| 70 | --RecursionDepth; |
| 71 | } |
| 72 | |
| 73 | protected virtual void Dispose(bool disposing) |
| 74 | { |
| 75 | if (!_isDisposed) |
| 76 | { |
| 77 | if (disposing) |
| 78 | { |
| 79 | (Trans as IDisposable)?.Dispose(); |
| 80 | } |
| 81 | } |
| 82 | _isDisposed = true; |
| 83 | } |
| 84 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 85 | |
| 86 | protected void CheckReadBytesAvailable(TSet set) |
| 87 | { |
| 88 | Transport.CheckReadBytesAvailable(set.Count * GetMinSerializedSize(set.ElementType)); |
| 89 | } |
| 90 | |
| 91 | protected void CheckReadBytesAvailable(TList list) |
| 92 | { |
| 93 | Transport.CheckReadBytesAvailable(list.Count * GetMinSerializedSize(list.ElementType)); |
| 94 | } |
| 95 | |
| 96 | protected void CheckReadBytesAvailable(TMap map) |
| 97 | { |
| 98 | var elmSize = GetMinSerializedSize(map.KeyType) + GetMinSerializedSize(map.ValueType); |
| 99 | Transport.CheckReadBytesAvailable(map.Count * elmSize); |
| 100 | } |
| 101 | |
| 102 | // Returns the minimum amount of bytes needed to store the smallest possible instance of TType. |
| 103 | public abstract int GetMinSerializedSize(TType type); |
| 104 | |
| 105 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 106 | public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 107 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 108 | public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 109 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 110 | public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 111 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 112 | public abstract Task WriteStructEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 113 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 114 | public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken = default); |
| 115 | |
| 116 | public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken = default); |
| 117 | |
| 118 | public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 120 | public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 121 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 122 | public abstract Task WriteMapEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 123 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 124 | public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 125 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 126 | public abstract Task WriteListEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 127 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 128 | public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 129 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 130 | public abstract Task WriteSetEndAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 131 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 132 | public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 133 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 134 | public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 135 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 136 | public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 137 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 138 | public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 139 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 140 | public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 141 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 142 | public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 143 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 144 | public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken = default) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 145 | { |
| 146 | var bytes = Encoding.UTF8.GetBytes(s); |
| 147 | await WriteBinaryAsync(bytes, cancellationToken); |
| 148 | } |
| 149 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 150 | public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken = default); |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 151 | public abstract Task WriteUuidAsync(Guid uuid, 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 ValueTask<TMessage> ReadMessageBeginAsync(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 Task ReadMessageEndAsync(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 ValueTask<TStruct> ReadStructBeginAsync(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 Task ReadStructEndAsync(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 ValueTask<TField> ReadFieldBeginAsync(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 Task ReadFieldEndAsync(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 ValueTask<TMap> ReadMapBeginAsync(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 Task ReadMapEndAsync(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 ValueTask<TList> ReadListBeginAsync(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 Task ReadListEndAsync(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 ValueTask<TSet> ReadSetBeginAsync(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 Task ReadSetEndAsync(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<bool> ReadBoolAsync(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<sbyte> ReadByteAsync(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<short> ReadI16Async(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<int> ReadI32Async(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<long> ReadI64Async(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 abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 188 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 189 | public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken = default) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 190 | { |
| 191 | var buf = await ReadBinaryAsync(cancellationToken); |
| 192 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 193 | } |
| 194 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 195 | public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken = default); |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 196 | |
| 197 | public abstract ValueTask<Guid> ReadUuidAsync(CancellationToken cancellationToken = default); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 198 | } |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 199 | } |