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