Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [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; |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 19 | using System.Buffers.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 20 | using System.Text; |
| 21 | using System.Threading; |
| 22 | using System.Threading.Tasks; |
| 23 | using Thrift.Protocol.Entities; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 24 | using Thrift.Protocol.Utilities; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 25 | using Thrift.Transport; |
| 26 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 27 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 28 | namespace Thrift.Protocol |
| 29 | { |
| 30 | // ReSharper disable once InconsistentNaming |
| 31 | public class TBinaryProtocol : TProtocol |
| 32 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 33 | protected const uint VersionMask = 0xffff0000; |
| 34 | protected const uint Version1 = 0x80010000; |
| 35 | |
| 36 | protected bool StrictRead; |
| 37 | protected bool StrictWrite; |
| 38 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 39 | // minimize memory allocations by means of an preallocated bytes buffer |
| 40 | // The value of 128 is arbitrarily chosen, the required minimum size must be sizeof(long) |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 41 | private readonly byte[] PreAllocatedBuffer = new byte[128]; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 42 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 43 | public TBinaryProtocol(TTransport trans) |
| 44 | : this(trans, false, true) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | public TBinaryProtocol(TTransport trans, bool strictRead, bool strictWrite) |
| 49 | : base(trans) |
| 50 | { |
| 51 | StrictRead = strictRead; |
| 52 | StrictWrite = strictWrite; |
| 53 | } |
| 54 | |
| 55 | public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) |
| 56 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 57 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 58 | |
| 59 | if (StrictWrite) |
| 60 | { |
| 61 | var version = Version1 | (uint) message.Type; |
| 62 | await WriteI32Async((int) version, cancellationToken); |
| 63 | await WriteStringAsync(message.Name, cancellationToken); |
| 64 | await WriteI32Async(message.SeqID, cancellationToken); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | await WriteStringAsync(message.Name, cancellationToken); |
| 69 | await WriteByteAsync((sbyte) message.Type, cancellationToken); |
| 70 | await WriteI32Async(message.SeqID, cancellationToken); |
| 71 | } |
| 72 | } |
| 73 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 74 | public override Task WriteMessageEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 75 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 76 | cancellationToken.ThrowIfCancellationRequested(); |
| 77 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 80 | public override Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 81 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 82 | cancellationToken.ThrowIfCancellationRequested(); |
| 83 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 86 | public override Task WriteStructEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 87 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 88 | cancellationToken.ThrowIfCancellationRequested(); |
| 89 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken) |
| 93 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 94 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 95 | await WriteByteAsync((sbyte) field.Type, cancellationToken); |
| 96 | await WriteI16Async(field.ID, cancellationToken); |
| 97 | } |
| 98 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 99 | public override Task WriteFieldEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 100 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 101 | cancellationToken.ThrowIfCancellationRequested(); |
| 102 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public override async Task WriteFieldStopAsync(CancellationToken cancellationToken) |
| 106 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 107 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 108 | |
| 109 | await WriteByteAsync((sbyte) TType.Stop, cancellationToken); |
| 110 | } |
| 111 | |
| 112 | public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken) |
| 113 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 114 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 115 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 116 | PreAllocatedBuffer[0] = (byte)map.KeyType; |
| 117 | PreAllocatedBuffer[1] = (byte)map.ValueType; |
| 118 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 2, cancellationToken); |
| 119 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 120 | await WriteI32Async(map.Count, cancellationToken); |
| 121 | } |
| 122 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 123 | public override Task WriteMapEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 124 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 125 | cancellationToken.ThrowIfCancellationRequested(); |
| 126 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken) |
| 130 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 131 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 132 | await WriteByteAsync((sbyte) list.ElementType, cancellationToken); |
| 133 | await WriteI32Async(list.Count, cancellationToken); |
| 134 | } |
| 135 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 136 | public override Task WriteListEndAsync(CancellationToken cancellationToken) |
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 | cancellationToken.ThrowIfCancellationRequested(); |
| 139 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken) |
| 143 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 144 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 145 | await WriteByteAsync((sbyte) set.ElementType, cancellationToken); |
| 146 | await WriteI32Async(set.Count, cancellationToken); |
| 147 | } |
| 148 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 149 | public override Task WriteSetEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 150 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 151 | cancellationToken.ThrowIfCancellationRequested(); |
| 152 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken) |
| 156 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 157 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 158 | await WriteByteAsync(b ? (sbyte) 1 : (sbyte) 0, cancellationToken); |
| 159 | } |
| 160 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 161 | public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken) |
| 162 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 163 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 164 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 165 | PreAllocatedBuffer[0] = (byte)b; |
| 166 | |
| 167 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 168 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 169 | public override async Task WriteI16Async(short i16, CancellationToken cancellationToken) |
| 170 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 171 | cancellationToken.ThrowIfCancellationRequested(); |
| 172 | |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 173 | BinaryPrimitives.WriteInt16BigEndian(PreAllocatedBuffer, i16); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 174 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 175 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 2, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | public override async Task WriteI32Async(int i32, CancellationToken cancellationToken) |
| 179 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 180 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 181 | |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 182 | BinaryPrimitives.WriteInt32BigEndian(PreAllocatedBuffer, i32); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 183 | |
| 184 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 4, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 187 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 188 | public override async Task WriteI64Async(long i64, CancellationToken cancellationToken) |
| 189 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 190 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 191 | |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 192 | BinaryPrimitives.WriteInt64BigEndian(PreAllocatedBuffer, i64); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 193 | |
| 194 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 8, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken) |
| 198 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 199 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 200 | |
| 201 | await WriteI64Async(BitConverter.DoubleToInt64Bits(d), cancellationToken); |
| 202 | } |
| 203 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 204 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 205 | public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken) |
| 206 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 207 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 208 | |
| 209 | await WriteI32Async(bytes.Length, cancellationToken); |
| 210 | await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken); |
| 211 | } |
| 212 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 213 | public override async Task WriteUuidAsync(Guid uuid, CancellationToken cancellationToken) |
| 214 | { |
| 215 | cancellationToken.ThrowIfCancellationRequested(); |
| 216 | |
| 217 | var bytes = uuid.SwapByteOrder().ToByteArray(); |
| 218 | await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken); |
| 219 | } |
| 220 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 221 | public override async ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 222 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 223 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 224 | |
| 225 | var message = new TMessage(); |
| 226 | var size = await ReadI32Async(cancellationToken); |
| 227 | if (size < 0) |
| 228 | { |
| 229 | var version = (uint) size & VersionMask; |
| 230 | if (version != Version1) |
| 231 | { |
| 232 | throw new TProtocolException(TProtocolException.BAD_VERSION, |
| 233 | $"Bad version in ReadMessageBegin: {version}"); |
| 234 | } |
| 235 | message.Type = (TMessageType) (size & 0x000000ff); |
| 236 | message.Name = await ReadStringAsync(cancellationToken); |
| 237 | message.SeqID = await ReadI32Async(cancellationToken); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | if (StrictRead) |
| 242 | { |
| 243 | throw new TProtocolException(TProtocolException.BAD_VERSION, |
| 244 | "Missing version in ReadMessageBegin, old client?"); |
| 245 | } |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 246 | message.Name = (size > 0) ? await ReadStringBodyAsync(size, cancellationToken) : string.Empty; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 247 | message.Type = (TMessageType) await ReadByteAsync(cancellationToken); |
| 248 | message.SeqID = await ReadI32Async(cancellationToken); |
| 249 | } |
| 250 | return message; |
| 251 | } |
| 252 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 253 | public override Task ReadMessageEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 254 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 255 | cancellationToken.ThrowIfCancellationRequested(); |
Philip Lee | 2d2790f | 2022-09-15 12:43:03 +0100 | [diff] [blame] | 256 | Transport.ResetConsumedMessageSize(); |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 257 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 260 | public override ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 261 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 262 | cancellationToken.ThrowIfCancellationRequested(); |
| 263 | return new ValueTask<TStruct>(AnonymousStruct); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 266 | public override Task ReadStructEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 267 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 268 | cancellationToken.ThrowIfCancellationRequested(); |
| 269 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 272 | public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 273 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 274 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 275 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 276 | var type = (TType)await ReadByteAsync(cancellationToken); |
| 277 | if (type == TType.Stop) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 278 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 279 | return StopField; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 282 | return new TField { |
| 283 | Type = type, |
| 284 | ID = await ReadI16Async(cancellationToken) |
| 285 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 288 | public override Task ReadFieldEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 289 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 290 | cancellationToken.ThrowIfCancellationRequested(); |
| 291 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 294 | public override async ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 295 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 296 | cancellationToken.ThrowIfCancellationRequested(); |
| 297 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 298 | var map = new TMap |
| 299 | { |
| 300 | KeyType = (TType) await ReadByteAsync(cancellationToken), |
| 301 | ValueType = (TType) await ReadByteAsync(cancellationToken), |
| 302 | Count = await ReadI32Async(cancellationToken) |
| 303 | }; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 304 | CheckReadBytesAvailable(map); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 305 | return map; |
| 306 | } |
| 307 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 308 | public override Task ReadMapEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 309 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 310 | cancellationToken.ThrowIfCancellationRequested(); |
| 311 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 314 | public override async ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 315 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 316 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 317 | |
| 318 | var list = new TList |
| 319 | { |
| 320 | ElementType = (TType) await ReadByteAsync(cancellationToken), |
| 321 | Count = await ReadI32Async(cancellationToken) |
| 322 | }; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 323 | CheckReadBytesAvailable(list); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 324 | return list; |
| 325 | } |
| 326 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 327 | public override Task ReadListEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 328 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 329 | cancellationToken.ThrowIfCancellationRequested(); |
| 330 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 333 | public override async ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 334 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 335 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 336 | |
| 337 | var set = new TSet |
| 338 | { |
| 339 | ElementType = (TType) await ReadByteAsync(cancellationToken), |
| 340 | Count = await ReadI32Async(cancellationToken) |
| 341 | }; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 342 | CheckReadBytesAvailable(set); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 343 | return set; |
| 344 | } |
| 345 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 346 | public override Task ReadSetEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 347 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 348 | cancellationToken.ThrowIfCancellationRequested(); |
| 349 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 352 | public override async ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 353 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 354 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 355 | |
| 356 | return await ReadByteAsync(cancellationToken) == 1; |
| 357 | } |
| 358 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 359 | public override async ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 360 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 361 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 362 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 363 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
| 364 | return (sbyte)PreAllocatedBuffer[0]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 367 | public override async ValueTask<short> ReadI16Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 368 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 369 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 370 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 371 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 2, cancellationToken); |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 372 | var result = BinaryPrimitives.ReadInt16BigEndian(PreAllocatedBuffer); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 373 | return result; |
| 374 | } |
| 375 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 376 | public override async ValueTask<int> ReadI32Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 377 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 378 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 379 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 380 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 4, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 381 | |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 382 | var result = BinaryPrimitives.ReadInt32BigEndian(PreAllocatedBuffer); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 383 | |
| 384 | return result; |
| 385 | } |
| 386 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 387 | public override async ValueTask<long> ReadI64Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 388 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 389 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 390 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 391 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 8, cancellationToken); |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 392 | return BinaryPrimitives.ReadInt64BigEndian(PreAllocatedBuffer); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 395 | public override async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 396 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 397 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 398 | |
| 399 | var d = await ReadI64Async(cancellationToken); |
| 400 | return BitConverter.Int64BitsToDouble(d); |
| 401 | } |
| 402 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 403 | public override async ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 404 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 405 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 406 | |
| 407 | var size = await ReadI32Async(cancellationToken); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 408 | Transport.CheckReadBytesAvailable(size); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 409 | var buf = new byte[size]; |
| 410 | await Trans.ReadAllAsync(buf, 0, size, cancellationToken); |
| 411 | return buf; |
| 412 | } |
| 413 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 414 | public override async ValueTask<Guid> ReadUuidAsync(CancellationToken cancellationToken) |
| 415 | { |
| 416 | cancellationToken.ThrowIfCancellationRequested(); |
| 417 | |
| 418 | Transport.CheckReadBytesAvailable(16); // = sizeof(uuid) |
| 419 | var buf = new byte[16]; |
| 420 | await Trans.ReadAllAsync(buf, 0, 16, cancellationToken); |
| 421 | return new Guid(buf).SwapByteOrder(); |
| 422 | } |
| 423 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 424 | public override async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken) |
| 425 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 426 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 427 | |
| 428 | var size = await ReadI32Async(cancellationToken); |
| 429 | return size > 0 ? await ReadStringBodyAsync(size, cancellationToken) : string.Empty; |
| 430 | } |
| 431 | |
| 432 | private async ValueTask<string> ReadStringBodyAsync(int size, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 433 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 434 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 435 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 436 | if (size <= PreAllocatedBuffer.Length) |
| 437 | { |
| 438 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, size, cancellationToken); |
| 439 | return Encoding.UTF8.GetString(PreAllocatedBuffer, 0, size); |
| 440 | } |
| 441 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 442 | Transport.CheckReadBytesAvailable(size); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 443 | var buf = new byte[size]; |
| 444 | await Trans.ReadAllAsync(buf, 0, size, cancellationToken); |
| 445 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 446 | } |
| 447 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 448 | // Return the minimum number of bytes a type will consume on the wire |
| 449 | public override int GetMinSerializedSize(TType type) |
| 450 | { |
| 451 | switch (type) |
| 452 | { |
| 453 | case TType.Stop: return 0; |
| 454 | case TType.Void: return 0; |
| 455 | case TType.Bool: return sizeof(byte); |
| 456 | case TType.Byte: return sizeof(byte); |
| 457 | case TType.Double: return sizeof(double); |
| 458 | case TType.I16: return sizeof(short); |
| 459 | case TType.I32: return sizeof(int); |
| 460 | case TType.I64: return sizeof(long); |
| 461 | case TType.String: return sizeof(int); // string length |
| 462 | case TType.Struct: return 0; // empty struct |
| 463 | case TType.Map: return sizeof(int); // element count |
| 464 | case TType.Set: return sizeof(int); // element count |
| 465 | case TType.List: return sizeof(int); // element count |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 466 | case TType.Uuid: return 16; // uuid bytes |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 467 | default: throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "unrecognized type code"); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 471 | public class Factory : TProtocolFactory |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 472 | { |
| 473 | protected bool StrictRead; |
| 474 | protected bool StrictWrite; |
| 475 | |
| 476 | public Factory() |
| 477 | : this(false, true) |
| 478 | { |
| 479 | } |
| 480 | |
| 481 | public Factory(bool strictRead, bool strictWrite) |
| 482 | { |
| 483 | StrictRead = strictRead; |
| 484 | StrictWrite = strictWrite; |
| 485 | } |
| 486 | |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 487 | public override TProtocol GetProtocol(TTransport trans) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 488 | { |
| 489 | return new TBinaryProtocol(trans, StrictRead, StrictWrite); |
| 490 | } |
| 491 | } |
| 492 | } |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 493 | } |