blob: 5275c9cd18ce3c57d4304542ecec161d869af3f3 [file] [log] [blame]
Jens Geyer5a17b132019-05-26 15:53:37 +02001// Licensed to the Apache Software Foundation(ASF) under one
Jens Geyeraa0c8b32019-01-28 23:27:45 +01002// 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
18using System;
19using System.Text;
20using System.Threading;
21using System.Threading.Tasks;
22using Thrift.Protocol.Entities;
23using Thrift.Transport;
24
25namespace Thrift.Protocol
26{
27 // ReSharper disable once InconsistentNaming
28 public abstract class TProtocol : IDisposable
29 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +010030 private bool _isDisposed;
31 protected int RecursionDepth;
32
33 protected TTransport Trans;
34
35 protected TProtocol(TTransport trans)
36 {
37 Trans = trans;
Jens Geyereacd1d42019-11-20 19:03:14 +010038 RecursionLimit = trans.Configuration.RecursionLimit;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010039 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
Jens Geyer50806452019-11-23 01:55:58 +010080
81 protected void CheckReadBytesAvailable(TSet set)
82 {
83 Transport.CheckReadBytesAvailable(set.Count * GetMinSerializedSize(set.ElementType));
84 }
85
86 protected void CheckReadBytesAvailable(TList list)
87 {
88 Transport.CheckReadBytesAvailable(list.Count * GetMinSerializedSize(list.ElementType));
89 }
90
91 protected void CheckReadBytesAvailable(TMap map)
92 {
93 var elmSize = GetMinSerializedSize(map.KeyType) + GetMinSerializedSize(map.ValueType);
94 Transport.CheckReadBytesAvailable(map.Count * elmSize);
95 }
96
97 // Returns the minimum amount of bytes needed to store the smallest possible instance of TType.
98 public abstract int GetMinSerializedSize(TType type);
99
100
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100101 public virtual async Task WriteMessageBeginAsync(TMessage message)
102 {
103 await WriteMessageBeginAsync(message, CancellationToken.None);
104 }
105
106 public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken);
107
108 public virtual async Task WriteMessageEndAsync()
109 {
110 await WriteMessageEndAsync(CancellationToken.None);
111 }
112
113 public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken);
114
115 public virtual async Task WriteStructBeginAsync(TStruct @struct)
116 {
117 await WriteStructBeginAsync(@struct, CancellationToken.None);
118 }
119
120 public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken);
121
122 public virtual async Task WriteStructEndAsync()
123 {
124 await WriteStructEndAsync(CancellationToken.None);
125 }
126
127 public abstract Task WriteStructEndAsync(CancellationToken cancellationToken);
128
129 public virtual async Task WriteFieldBeginAsync(TField field)
130 {
131 await WriteFieldBeginAsync(field, CancellationToken.None);
132 }
133
134 public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken);
135
136 public virtual async Task WriteFieldEndAsync()
137 {
138 await WriteFieldEndAsync(CancellationToken.None);
139 }
140
141 public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken);
142
143 public virtual async Task WriteFieldStopAsync()
144 {
145 await WriteFieldStopAsync(CancellationToken.None);
146 }
147
148 public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken);
149
150 public virtual async Task WriteMapBeginAsync(TMap map)
151 {
152 await WriteMapBeginAsync(map, CancellationToken.None);
153 }
154
155 public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken);
156
157 public virtual async Task WriteMapEndAsync()
158 {
159 await WriteMapEndAsync(CancellationToken.None);
160 }
161
162 public abstract Task WriteMapEndAsync(CancellationToken cancellationToken);
163
164 public virtual async Task WriteListBeginAsync(TList list)
165 {
166 await WriteListBeginAsync(list, CancellationToken.None);
167 }
168
169 public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken);
170
171 public virtual async Task WriteListEndAsync()
172 {
173 await WriteListEndAsync(CancellationToken.None);
174 }
175
176 public abstract Task WriteListEndAsync(CancellationToken cancellationToken);
177
178 public virtual async Task WriteSetBeginAsync(TSet set)
179 {
180 await WriteSetBeginAsync(set, CancellationToken.None);
181 }
182
183 public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken);
184
185 public virtual async Task WriteSetEndAsync()
186 {
187 await WriteSetEndAsync(CancellationToken.None);
188 }
189
190 public abstract Task WriteSetEndAsync(CancellationToken cancellationToken);
191
192 public virtual async Task WriteBoolAsync(bool b)
193 {
194 await WriteBoolAsync(b, CancellationToken.None);
195 }
196
197 public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken);
198
199 public virtual async Task WriteByteAsync(sbyte b)
200 {
201 await WriteByteAsync(b, CancellationToken.None);
202 }
203
204 public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken);
205
206 public virtual async Task WriteI16Async(short i16)
207 {
208 await WriteI16Async(i16, CancellationToken.None);
209 }
210
211 public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken);
212
213 public virtual async Task WriteI32Async(int i32)
214 {
215 await WriteI32Async(i32, CancellationToken.None);
216 }
217
218 public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken);
219
220 public virtual async Task WriteI64Async(long i64)
221 {
222 await WriteI64Async(i64, CancellationToken.None);
223 }
224
225 public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken);
226
227 public virtual async Task WriteDoubleAsync(double d)
228 {
229 await WriteDoubleAsync(d, CancellationToken.None);
230 }
231
232 public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken);
233
234 public virtual async Task WriteStringAsync(string s)
235 {
236 await WriteStringAsync(s, CancellationToken.None);
237 }
238
239 public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken)
240 {
241 var bytes = Encoding.UTF8.GetBytes(s);
242 await WriteBinaryAsync(bytes, cancellationToken);
243 }
244
245 public virtual async Task WriteBinaryAsync(byte[] bytes)
246 {
247 await WriteBinaryAsync(bytes, CancellationToken.None);
248 }
249
250 public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken);
251
Jens Geyer5a17b132019-05-26 15:53:37 +0200252 public virtual async ValueTask<TMessage> ReadMessageBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100253 {
254 return await ReadMessageBeginAsync(CancellationToken.None);
255 }
256
Jens Geyer5a17b132019-05-26 15:53:37 +0200257 public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100258
259 public virtual async Task ReadMessageEndAsync()
260 {
261 await ReadMessageEndAsync(CancellationToken.None);
262 }
263
264 public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken);
265
Jens Geyer5a17b132019-05-26 15:53:37 +0200266 public virtual async ValueTask<TStruct> ReadStructBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100267 {
268 return await ReadStructBeginAsync(CancellationToken.None);
269 }
270
Jens Geyer5a17b132019-05-26 15:53:37 +0200271 public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100272
273 public virtual async Task ReadStructEndAsync()
274 {
275 await ReadStructEndAsync(CancellationToken.None);
276 }
277
278 public abstract Task ReadStructEndAsync(CancellationToken cancellationToken);
279
Jens Geyer5a17b132019-05-26 15:53:37 +0200280 public virtual async ValueTask<TField> ReadFieldBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100281 {
282 return await ReadFieldBeginAsync(CancellationToken.None);
283 }
284
Jens Geyer5a17b132019-05-26 15:53:37 +0200285 public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100286
287 public virtual async Task ReadFieldEndAsync()
288 {
289 await ReadFieldEndAsync(CancellationToken.None);
290 }
291
292 public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken);
293
Jens Geyer5a17b132019-05-26 15:53:37 +0200294 public virtual async ValueTask<TMap> ReadMapBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100295 {
296 return await ReadMapBeginAsync(CancellationToken.None);
297 }
298
Jens Geyer5a17b132019-05-26 15:53:37 +0200299 public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100300
301 public virtual async Task ReadMapEndAsync()
302 {
303 await ReadMapEndAsync(CancellationToken.None);
304 }
305
306 public abstract Task ReadMapEndAsync(CancellationToken cancellationToken);
307
Jens Geyer5a17b132019-05-26 15:53:37 +0200308 public virtual async ValueTask<TList> ReadListBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100309 {
310 return await ReadListBeginAsync(CancellationToken.None);
311 }
312
Jens Geyer5a17b132019-05-26 15:53:37 +0200313 public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100314
315 public virtual async Task ReadListEndAsync()
316 {
317 await ReadListEndAsync(CancellationToken.None);
318 }
319
320 public abstract Task ReadListEndAsync(CancellationToken cancellationToken);
321
Jens Geyer5a17b132019-05-26 15:53:37 +0200322 public virtual async ValueTask<TSet> ReadSetBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100323 {
324 return await ReadSetBeginAsync(CancellationToken.None);
325 }
326
Jens Geyer5a17b132019-05-26 15:53:37 +0200327 public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100328
329 public virtual async Task ReadSetEndAsync()
330 {
331 await ReadSetEndAsync(CancellationToken.None);
332 }
333
334 public abstract Task ReadSetEndAsync(CancellationToken cancellationToken);
335
Jens Geyer5a17b132019-05-26 15:53:37 +0200336 public virtual async ValueTask<bool> ReadBoolAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100337 {
338 return await ReadBoolAsync(CancellationToken.None);
339 }
340
Jens Geyer5a17b132019-05-26 15:53:37 +0200341 public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100342
Jens Geyer5a17b132019-05-26 15:53:37 +0200343 public virtual async ValueTask<sbyte> ReadByteAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100344 {
345 return await ReadByteAsync(CancellationToken.None);
346 }
347
Jens Geyer5a17b132019-05-26 15:53:37 +0200348 public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100349
Jens Geyer5a17b132019-05-26 15:53:37 +0200350 public virtual async ValueTask<short> ReadI16Async()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100351 {
352 return await ReadI16Async(CancellationToken.None);
353 }
354
Jens Geyer5a17b132019-05-26 15:53:37 +0200355 public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100356
Jens Geyer5a17b132019-05-26 15:53:37 +0200357 public virtual async ValueTask<int> ReadI32Async()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100358 {
359 return await ReadI32Async(CancellationToken.None);
360 }
361
Jens Geyer5a17b132019-05-26 15:53:37 +0200362 public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100363
Jens Geyer5a17b132019-05-26 15:53:37 +0200364 public virtual async ValueTask<long> ReadI64Async()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100365 {
366 return await ReadI64Async(CancellationToken.None);
367 }
368
Jens Geyer5a17b132019-05-26 15:53:37 +0200369 public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100370
Jens Geyer5a17b132019-05-26 15:53:37 +0200371 public virtual async ValueTask<double> ReadDoubleAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100372 {
373 return await ReadDoubleAsync(CancellationToken.None);
374 }
375
Jens Geyer5a17b132019-05-26 15:53:37 +0200376 public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100377
Jens Geyer5a17b132019-05-26 15:53:37 +0200378 public virtual async ValueTask<string> ReadStringAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100379 {
380 return await ReadStringAsync(CancellationToken.None);
381 }
382
Jens Geyer5a17b132019-05-26 15:53:37 +0200383 public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100384 {
385 var buf = await ReadBinaryAsync(cancellationToken);
386 return Encoding.UTF8.GetString(buf, 0, buf.Length);
387 }
388
Jens Geyer5a17b132019-05-26 15:53:37 +0200389 public virtual async ValueTask<byte[]> ReadBinaryAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100390 {
391 return await ReadBinaryAsync(CancellationToken.None);
392 }
393
Jens Geyer5a17b132019-05-26 15:53:37 +0200394 public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100395 }
Jens Geyer5a17b132019-05-26 15:53:37 +0200396}