blob: f2bec600059c29f1f4986ed566f0aa140d3a8523 [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
Jens Geyerdce22992020-05-16 23:02:27 +020035 protected static readonly TStruct AnonymousStruct = new TStruct(string.Empty);
36 protected static readonly TField StopField = new TField() { Type = TType.Stop };
37
38
Jens Geyeraa0c8b32019-01-28 23:27:45 +010039 protected TProtocol(TTransport trans)
40 {
41 Trans = trans;
Jens Geyereacd1d42019-11-20 19:03:14 +010042 RecursionLimit = trans.Configuration.RecursionLimit;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010043 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 Geyer561bc9a2022-01-26 22:38:04 +010053 GC.SuppressFinalize(this);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010054 }
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 Geyer50806452019-11-23 01:55:58 +010085
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 Geyerdce22992020-05-16 23:02:27 +0200106 public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100107
Jens Geyerdce22992020-05-16 23:02:27 +0200108 public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100109
Jens Geyerdce22992020-05-16 23:02:27 +0200110 public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100111
Jens Geyerdce22992020-05-16 23:02:27 +0200112 public abstract Task WriteStructEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100113
Jens Geyerdce22992020-05-16 23:02:27 +0200114 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 Geyeraa0c8b32019-01-28 23:27:45 +0100119
Jens Geyerdce22992020-05-16 23:02:27 +0200120 public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100121
Jens Geyerdce22992020-05-16 23:02:27 +0200122 public abstract Task WriteMapEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100123
Jens Geyerdce22992020-05-16 23:02:27 +0200124 public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100125
Jens Geyerdce22992020-05-16 23:02:27 +0200126 public abstract Task WriteListEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100127
Jens Geyerdce22992020-05-16 23:02:27 +0200128 public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100129
Jens Geyerdce22992020-05-16 23:02:27 +0200130 public abstract Task WriteSetEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100131
Jens Geyerdce22992020-05-16 23:02:27 +0200132 public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100133
Jens Geyerdce22992020-05-16 23:02:27 +0200134 public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100135
Jens Geyerdce22992020-05-16 23:02:27 +0200136 public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100137
Jens Geyerdce22992020-05-16 23:02:27 +0200138 public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100139
Jens Geyerdce22992020-05-16 23:02:27 +0200140 public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100141
Jens Geyerdce22992020-05-16 23:02:27 +0200142 public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100143
Jens Geyerdce22992020-05-16 23:02:27 +0200144 public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken = default)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100145 {
146 var bytes = Encoding.UTF8.GetBytes(s);
147 await WriteBinaryAsync(bytes, cancellationToken);
148 }
149
Jens Geyerdce22992020-05-16 23:02:27 +0200150 public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken = default);
Jens Geyer62445c12022-06-29 00:00:00 +0200151 public abstract Task WriteUuidAsync(Guid uuid, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100152
Jens Geyerdce22992020-05-16 23:02:27 +0200153 public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100154
Jens Geyerdce22992020-05-16 23:02:27 +0200155 public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100156
Jens Geyerdce22992020-05-16 23:02:27 +0200157 public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100158
Jens Geyerdce22992020-05-16 23:02:27 +0200159 public abstract Task ReadStructEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100160
Jens Geyerdce22992020-05-16 23:02:27 +0200161 public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100162
Jens Geyerdce22992020-05-16 23:02:27 +0200163 public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100164
Jens Geyerdce22992020-05-16 23:02:27 +0200165 public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100166
Jens Geyerdce22992020-05-16 23:02:27 +0200167 public abstract Task ReadMapEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100168
Jens Geyerdce22992020-05-16 23:02:27 +0200169 public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100170
Jens Geyerdce22992020-05-16 23:02:27 +0200171 public abstract Task ReadListEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100172
Jens Geyerdce22992020-05-16 23:02:27 +0200173 public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100174
Jens Geyerdce22992020-05-16 23:02:27 +0200175 public abstract Task ReadSetEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100176
Jens Geyerdce22992020-05-16 23:02:27 +0200177 public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100178
Jens Geyerdce22992020-05-16 23:02:27 +0200179 public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100180
Jens Geyerdce22992020-05-16 23:02:27 +0200181 public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100182
Jens Geyerdce22992020-05-16 23:02:27 +0200183 public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100184
Jens Geyerdce22992020-05-16 23:02:27 +0200185 public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100186
Jens Geyerdce22992020-05-16 23:02:27 +0200187 public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100188
Jens Geyerdce22992020-05-16 23:02:27 +0200189 public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken = default)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100190 {
191 var buf = await ReadBinaryAsync(cancellationToken);
192 return Encoding.UTF8.GetString(buf, 0, buf.Length);
193 }
194
Jens Geyerdce22992020-05-16 23:02:27 +0200195 public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken = default);
Jens Geyer62445c12022-06-29 00:00:00 +0200196
197 public abstract ValueTask<Guid> ReadUuidAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100198 }
Jens Geyer5a17b132019-05-26 15:53:37 +0200199}