blob: 5b1bec16b536cef98a59ca220e85025ae2dcd6b7 [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);
53 }
54
55 public void IncrementRecursionDepth()
56 {
57 if (RecursionDepth < RecursionLimit)
58 {
59 ++RecursionDepth;
60 }
61 else
62 {
63 throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded");
64 }
65 }
66
67 public void DecrementRecursionDepth()
68 {
69 --RecursionDepth;
70 }
71
72 protected virtual void Dispose(bool disposing)
73 {
74 if (!_isDisposed)
75 {
76 if (disposing)
77 {
78 (Trans as IDisposable)?.Dispose();
79 }
80 }
81 _isDisposed = true;
82 }
83
Jens Geyer50806452019-11-23 01:55:58 +010084
85 protected void CheckReadBytesAvailable(TSet set)
86 {
87 Transport.CheckReadBytesAvailable(set.Count * GetMinSerializedSize(set.ElementType));
88 }
89
90 protected void CheckReadBytesAvailable(TList list)
91 {
92 Transport.CheckReadBytesAvailable(list.Count * GetMinSerializedSize(list.ElementType));
93 }
94
95 protected void CheckReadBytesAvailable(TMap map)
96 {
97 var elmSize = GetMinSerializedSize(map.KeyType) + GetMinSerializedSize(map.ValueType);
98 Transport.CheckReadBytesAvailable(map.Count * elmSize);
99 }
100
101 // Returns the minimum amount of bytes needed to store the smallest possible instance of TType.
102 public abstract int GetMinSerializedSize(TType type);
103
104
Jens Geyerdce22992020-05-16 23:02:27 +0200105 public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100106
Jens Geyerdce22992020-05-16 23:02:27 +0200107 public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100108
Jens Geyerdce22992020-05-16 23:02:27 +0200109 public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100110
Jens Geyerdce22992020-05-16 23:02:27 +0200111 public abstract Task WriteStructEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100112
Jens Geyerdce22992020-05-16 23:02:27 +0200113 public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken = default);
114
115 public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken = default);
116
117 public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100118
Jens Geyerdce22992020-05-16 23:02:27 +0200119 public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100120
Jens Geyerdce22992020-05-16 23:02:27 +0200121 public abstract Task WriteMapEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100122
Jens Geyerdce22992020-05-16 23:02:27 +0200123 public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100124
Jens Geyerdce22992020-05-16 23:02:27 +0200125 public abstract Task WriteListEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100126
Jens Geyerdce22992020-05-16 23:02:27 +0200127 public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100128
Jens Geyerdce22992020-05-16 23:02:27 +0200129 public abstract Task WriteSetEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100130
Jens Geyerdce22992020-05-16 23:02:27 +0200131 public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100132
Jens Geyerdce22992020-05-16 23:02:27 +0200133 public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100134
Jens Geyerdce22992020-05-16 23:02:27 +0200135 public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100136
Jens Geyerdce22992020-05-16 23:02:27 +0200137 public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100138
Jens Geyerdce22992020-05-16 23:02:27 +0200139 public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100140
Jens Geyerdce22992020-05-16 23:02:27 +0200141 public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100142
Jens Geyerdce22992020-05-16 23:02:27 +0200143 public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken = default)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100144 {
145 var bytes = Encoding.UTF8.GetBytes(s);
146 await WriteBinaryAsync(bytes, cancellationToken);
147 }
148
Jens Geyerdce22992020-05-16 23:02:27 +0200149 public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100150
Jens Geyerdce22992020-05-16 23:02:27 +0200151 public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100152
Jens Geyerdce22992020-05-16 23:02:27 +0200153 public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100154
Jens Geyerdce22992020-05-16 23:02:27 +0200155 public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100156
Jens Geyerdce22992020-05-16 23:02:27 +0200157 public abstract Task ReadStructEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100158
Jens Geyerdce22992020-05-16 23:02:27 +0200159 public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100160
Jens Geyerdce22992020-05-16 23:02:27 +0200161 public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100162
Jens Geyerdce22992020-05-16 23:02:27 +0200163 public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100164
Jens Geyerdce22992020-05-16 23:02:27 +0200165 public abstract Task ReadMapEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100166
Jens Geyerdce22992020-05-16 23:02:27 +0200167 public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100168
Jens Geyerdce22992020-05-16 23:02:27 +0200169 public abstract Task ReadListEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100170
Jens Geyerdce22992020-05-16 23:02:27 +0200171 public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100172
Jens Geyerdce22992020-05-16 23:02:27 +0200173 public abstract Task ReadSetEndAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100174
Jens Geyerdce22992020-05-16 23:02:27 +0200175 public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100176
Jens Geyerdce22992020-05-16 23:02:27 +0200177 public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100178
Jens Geyerdce22992020-05-16 23:02:27 +0200179 public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100180
Jens Geyerdce22992020-05-16 23:02:27 +0200181 public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100182
Jens Geyerdce22992020-05-16 23:02:27 +0200183 public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100184
Jens Geyerdce22992020-05-16 23:02:27 +0200185 public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100186
Jens Geyerdce22992020-05-16 23:02:27 +0200187 public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken = default)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100188 {
189 var buf = await ReadBinaryAsync(cancellationToken);
190 return Encoding.UTF8.GetString(buf, 0, buf.Length);
191 }
192
Jens Geyerdce22992020-05-16 23:02:27 +0200193 public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken = default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100194 }
Jens Geyer5a17b132019-05-26 15:53:37 +0200195}