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(); |
| 256 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 259 | public override ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 260 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 261 | cancellationToken.ThrowIfCancellationRequested(); |
| 262 | return new ValueTask<TStruct>(AnonymousStruct); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 265 | public override Task ReadStructEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 266 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 267 | cancellationToken.ThrowIfCancellationRequested(); |
| 268 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 271 | public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 272 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 273 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 274 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 275 | var type = (TType)await ReadByteAsync(cancellationToken); |
| 276 | if (type == TType.Stop) |
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 | return StopField; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 281 | return new TField { |
| 282 | Type = type, |
| 283 | ID = await ReadI16Async(cancellationToken) |
| 284 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 287 | public override Task ReadFieldEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 288 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 289 | cancellationToken.ThrowIfCancellationRequested(); |
| 290 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 293 | public override async ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 294 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 295 | cancellationToken.ThrowIfCancellationRequested(); |
| 296 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 297 | var map = new TMap |
| 298 | { |
| 299 | KeyType = (TType) await ReadByteAsync(cancellationToken), |
| 300 | ValueType = (TType) await ReadByteAsync(cancellationToken), |
| 301 | Count = await ReadI32Async(cancellationToken) |
| 302 | }; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 303 | CheckReadBytesAvailable(map); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 304 | return map; |
| 305 | } |
| 306 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 307 | public override Task ReadMapEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 308 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 309 | cancellationToken.ThrowIfCancellationRequested(); |
| 310 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 313 | public override async ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 314 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 315 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 316 | |
| 317 | var list = new TList |
| 318 | { |
| 319 | ElementType = (TType) await ReadByteAsync(cancellationToken), |
| 320 | Count = await ReadI32Async(cancellationToken) |
| 321 | }; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 322 | CheckReadBytesAvailable(list); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 323 | return list; |
| 324 | } |
| 325 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 326 | public override Task ReadListEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 327 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 328 | cancellationToken.ThrowIfCancellationRequested(); |
| 329 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 332 | public override async ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 333 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 334 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 335 | |
| 336 | var set = new TSet |
| 337 | { |
| 338 | ElementType = (TType) await ReadByteAsync(cancellationToken), |
| 339 | Count = await ReadI32Async(cancellationToken) |
| 340 | }; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 341 | CheckReadBytesAvailable(set); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 342 | return set; |
| 343 | } |
| 344 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 345 | public override Task ReadSetEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 346 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 347 | cancellationToken.ThrowIfCancellationRequested(); |
| 348 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 351 | public override async ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 352 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 353 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 354 | |
| 355 | return await ReadByteAsync(cancellationToken) == 1; |
| 356 | } |
| 357 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 358 | public override async ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 359 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 360 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 361 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 362 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
| 363 | return (sbyte)PreAllocatedBuffer[0]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 366 | public override async ValueTask<short> ReadI16Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 367 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 368 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 369 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 370 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 2, cancellationToken); |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 371 | var result = BinaryPrimitives.ReadInt16BigEndian(PreAllocatedBuffer); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 372 | return result; |
| 373 | } |
| 374 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 375 | public override async ValueTask<int> ReadI32Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 376 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 377 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 378 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 379 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 4, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 380 | |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 381 | var result = BinaryPrimitives.ReadInt32BigEndian(PreAllocatedBuffer); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 382 | |
| 383 | return result; |
| 384 | } |
| 385 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 386 | public override async ValueTask<long> ReadI64Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 387 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 388 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 389 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 390 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 8, cancellationToken); |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 391 | return BinaryPrimitives.ReadInt64BigEndian(PreAllocatedBuffer); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 394 | public override async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 395 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 396 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 397 | |
| 398 | var d = await ReadI64Async(cancellationToken); |
| 399 | return BitConverter.Int64BitsToDouble(d); |
| 400 | } |
| 401 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 402 | public override async ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 403 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 404 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 405 | |
| 406 | var size = await ReadI32Async(cancellationToken); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 407 | Transport.CheckReadBytesAvailable(size); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 408 | var buf = new byte[size]; |
| 409 | await Trans.ReadAllAsync(buf, 0, size, cancellationToken); |
| 410 | return buf; |
| 411 | } |
| 412 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame^] | 413 | public override async ValueTask<Guid> ReadUuidAsync(CancellationToken cancellationToken) |
| 414 | { |
| 415 | cancellationToken.ThrowIfCancellationRequested(); |
| 416 | |
| 417 | Transport.CheckReadBytesAvailable(16); // = sizeof(uuid) |
| 418 | var buf = new byte[16]; |
| 419 | await Trans.ReadAllAsync(buf, 0, 16, cancellationToken); |
| 420 | return new Guid(buf).SwapByteOrder(); |
| 421 | } |
| 422 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 423 | public override async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken) |
| 424 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 425 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 426 | |
| 427 | var size = await ReadI32Async(cancellationToken); |
| 428 | return size > 0 ? await ReadStringBodyAsync(size, cancellationToken) : string.Empty; |
| 429 | } |
| 430 | |
| 431 | private async ValueTask<string> ReadStringBodyAsync(int size, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 432 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 433 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 434 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 435 | if (size <= PreAllocatedBuffer.Length) |
| 436 | { |
| 437 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, size, cancellationToken); |
| 438 | return Encoding.UTF8.GetString(PreAllocatedBuffer, 0, size); |
| 439 | } |
| 440 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 441 | Transport.CheckReadBytesAvailable(size); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 442 | var buf = new byte[size]; |
| 443 | await Trans.ReadAllAsync(buf, 0, size, cancellationToken); |
| 444 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 445 | } |
| 446 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 447 | // Return the minimum number of bytes a type will consume on the wire |
| 448 | public override int GetMinSerializedSize(TType type) |
| 449 | { |
| 450 | switch (type) |
| 451 | { |
| 452 | case TType.Stop: return 0; |
| 453 | case TType.Void: return 0; |
| 454 | case TType.Bool: return sizeof(byte); |
| 455 | case TType.Byte: return sizeof(byte); |
| 456 | case TType.Double: return sizeof(double); |
| 457 | case TType.I16: return sizeof(short); |
| 458 | case TType.I32: return sizeof(int); |
| 459 | case TType.I64: return sizeof(long); |
| 460 | case TType.String: return sizeof(int); // string length |
| 461 | case TType.Struct: return 0; // empty struct |
| 462 | case TType.Map: return sizeof(int); // element count |
| 463 | case TType.Set: return sizeof(int); // element count |
| 464 | case TType.List: return sizeof(int); // element count |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame^] | 465 | case TType.Uuid: return 16; // uuid bytes |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 466 | default: throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "unrecognized type code"); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 470 | public class Factory : TProtocolFactory |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 471 | { |
| 472 | protected bool StrictRead; |
| 473 | protected bool StrictWrite; |
| 474 | |
| 475 | public Factory() |
| 476 | : this(false, true) |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | public Factory(bool strictRead, bool strictWrite) |
| 481 | { |
| 482 | StrictRead = strictRead; |
| 483 | StrictWrite = strictWrite; |
| 484 | } |
| 485 | |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 486 | public override TProtocol GetProtocol(TTransport trans) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 487 | { |
| 488 | return new TBinaryProtocol(trans, StrictRead, StrictWrite); |
| 489 | } |
| 490 | } |
| 491 | } |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 492 | } |