blob: dca3f9efc85249a87c08d7fa151c375b689b8166 [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
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 Geyer5a17b132019-05-26 15:53:37 +0200231 public virtual async ValueTask<TMessage> ReadMessageBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100232 {
233 return await ReadMessageBeginAsync(CancellationToken.None);
234 }
235
Jens Geyer5a17b132019-05-26 15:53:37 +0200236 public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100237
238 public virtual async Task ReadMessageEndAsync()
239 {
240 await ReadMessageEndAsync(CancellationToken.None);
241 }
242
243 public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken);
244
Jens Geyer5a17b132019-05-26 15:53:37 +0200245 public virtual async ValueTask<TStruct> ReadStructBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100246 {
247 return await ReadStructBeginAsync(CancellationToken.None);
248 }
249
Jens Geyer5a17b132019-05-26 15:53:37 +0200250 public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100251
252 public virtual async Task ReadStructEndAsync()
253 {
254 await ReadStructEndAsync(CancellationToken.None);
255 }
256
257 public abstract Task ReadStructEndAsync(CancellationToken cancellationToken);
258
Jens Geyer5a17b132019-05-26 15:53:37 +0200259 public virtual async ValueTask<TField> ReadFieldBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100260 {
261 return await ReadFieldBeginAsync(CancellationToken.None);
262 }
263
Jens Geyer5a17b132019-05-26 15:53:37 +0200264 public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100265
266 public virtual async Task ReadFieldEndAsync()
267 {
268 await ReadFieldEndAsync(CancellationToken.None);
269 }
270
271 public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken);
272
Jens Geyer5a17b132019-05-26 15:53:37 +0200273 public virtual async ValueTask<TMap> ReadMapBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100274 {
275 return await ReadMapBeginAsync(CancellationToken.None);
276 }
277
Jens Geyer5a17b132019-05-26 15:53:37 +0200278 public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100279
280 public virtual async Task ReadMapEndAsync()
281 {
282 await ReadMapEndAsync(CancellationToken.None);
283 }
284
285 public abstract Task ReadMapEndAsync(CancellationToken cancellationToken);
286
Jens Geyer5a17b132019-05-26 15:53:37 +0200287 public virtual async ValueTask<TList> ReadListBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100288 {
289 return await ReadListBeginAsync(CancellationToken.None);
290 }
291
Jens Geyer5a17b132019-05-26 15:53:37 +0200292 public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100293
294 public virtual async Task ReadListEndAsync()
295 {
296 await ReadListEndAsync(CancellationToken.None);
297 }
298
299 public abstract Task ReadListEndAsync(CancellationToken cancellationToken);
300
Jens Geyer5a17b132019-05-26 15:53:37 +0200301 public virtual async ValueTask<TSet> ReadSetBeginAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100302 {
303 return await ReadSetBeginAsync(CancellationToken.None);
304 }
305
Jens Geyer5a17b132019-05-26 15:53:37 +0200306 public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100307
308 public virtual async Task ReadSetEndAsync()
309 {
310 await ReadSetEndAsync(CancellationToken.None);
311 }
312
313 public abstract Task ReadSetEndAsync(CancellationToken cancellationToken);
314
Jens Geyer5a17b132019-05-26 15:53:37 +0200315 public virtual async ValueTask<bool> ReadBoolAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100316 {
317 return await ReadBoolAsync(CancellationToken.None);
318 }
319
Jens Geyer5a17b132019-05-26 15:53:37 +0200320 public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100321
Jens Geyer5a17b132019-05-26 15:53:37 +0200322 public virtual async ValueTask<sbyte> ReadByteAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100323 {
324 return await ReadByteAsync(CancellationToken.None);
325 }
326
Jens Geyer5a17b132019-05-26 15:53:37 +0200327 public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100328
Jens Geyer5a17b132019-05-26 15:53:37 +0200329 public virtual async ValueTask<short> ReadI16Async()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100330 {
331 return await ReadI16Async(CancellationToken.None);
332 }
333
Jens Geyer5a17b132019-05-26 15:53:37 +0200334 public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100335
Jens Geyer5a17b132019-05-26 15:53:37 +0200336 public virtual async ValueTask<int> ReadI32Async()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100337 {
338 return await ReadI32Async(CancellationToken.None);
339 }
340
Jens Geyer5a17b132019-05-26 15:53:37 +0200341 public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100342
Jens Geyer5a17b132019-05-26 15:53:37 +0200343 public virtual async ValueTask<long> ReadI64Async()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100344 {
345 return await ReadI64Async(CancellationToken.None);
346 }
347
Jens Geyer5a17b132019-05-26 15:53:37 +0200348 public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100349
Jens Geyer5a17b132019-05-26 15:53:37 +0200350 public virtual async ValueTask<double> ReadDoubleAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100351 {
352 return await ReadDoubleAsync(CancellationToken.None);
353 }
354
Jens Geyer5a17b132019-05-26 15:53:37 +0200355 public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100356
Jens Geyer5a17b132019-05-26 15:53:37 +0200357 public virtual async ValueTask<string> ReadStringAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100358 {
359 return await ReadStringAsync(CancellationToken.None);
360 }
361
Jens Geyer5a17b132019-05-26 15:53:37 +0200362 public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100363 {
364 var buf = await ReadBinaryAsync(cancellationToken);
365 return Encoding.UTF8.GetString(buf, 0, buf.Length);
366 }
367
Jens Geyer5a17b132019-05-26 15:53:37 +0200368 public virtual async ValueTask<byte[]> ReadBinaryAsync()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100369 {
370 return await ReadBinaryAsync(CancellationToken.None);
371 }
372
Jens Geyer5a17b132019-05-26 15:53:37 +0200373 public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100374 }
Jens Geyer5a17b132019-05-26 15:53:37 +0200375}