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