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