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; |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 19 | using System.Buffers; |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 20 | using System.Buffers.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 21 | using System.Collections.Generic; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 22 | using System.Diagnostics; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 23 | using System.Text; |
| 24 | using System.Threading; |
| 25 | using System.Threading.Tasks; |
| 26 | using Thrift.Protocol.Entities; |
| 27 | using Thrift.Transport; |
| 28 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 29 | #pragma warning disable IDE0079 // unnecessary suppression |
| 30 | #pragma warning disable IDE0066 // use switch expression |
| 31 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 32 | namespace Thrift.Protocol |
| 33 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 34 | |
| 35 | // ReSharper disable once InconsistentNaming |
| 36 | public class TCompactProtocol : TProtocol |
| 37 | { |
| 38 | private const byte ProtocolId = 0x82; |
| 39 | private const byte Version = 1; |
| 40 | private const byte VersionMask = 0x1f; // 0001 1111 |
| 41 | private const byte TypeMask = 0xE0; // 1110 0000 |
| 42 | private const byte TypeBits = 0x07; // 0000 0111 |
| 43 | private const int TypeShiftAmount = 5; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 44 | |
| 45 | private const byte NoTypeOverride = 0xFF; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 46 | |
| 47 | // ReSharper disable once InconsistentNaming |
| 48 | private static readonly byte[] TTypeToCompactType = new byte[16]; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 49 | private static readonly TType[] CompactTypeToTType = new TType[13]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 50 | |
| 51 | /// <summary> |
| 52 | /// Used to keep track of the last field for the current and previous structs, so we can do the delta stuff. |
| 53 | /// </summary> |
| 54 | private readonly Stack<short> _lastField = new Stack<short>(15); |
| 55 | |
| 56 | /// <summary> |
| 57 | /// If we encounter a boolean field begin, save the TField here so it can have the value incorporated. |
| 58 | /// </summary> |
| 59 | private TField? _booleanField; |
| 60 | |
| 61 | /// <summary> |
| 62 | /// If we Read a field header, and it's a boolean field, save the boolean value here so that ReadBool can use it. |
| 63 | /// </summary> |
| 64 | private bool? _boolValue; |
| 65 | |
| 66 | private short _lastFieldId; |
| 67 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 68 | // minimize memory allocations by means of an preallocated bytes buffer |
| 69 | // The value of 128 is arbitrarily chosen, the required minimum size must be sizeof(long) |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 70 | private readonly byte[] PreAllocatedBuffer = new byte[128]; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 71 | |
| 72 | private struct VarInt |
| 73 | { |
| 74 | public byte[] bytes; |
| 75 | public int count; |
| 76 | } |
| 77 | |
| 78 | // minimize memory allocations by means of an preallocated VarInt buffer |
| 79 | private VarInt PreAllocatedVarInt = new VarInt() |
| 80 | { |
| 81 | bytes = new byte[10], // see Int64ToVarInt() |
| 82 | count = 0 |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 88 | public TCompactProtocol(TTransport trans) |
| 89 | : base(trans) |
| 90 | { |
| 91 | TTypeToCompactType[(int) TType.Stop] = Types.Stop; |
| 92 | TTypeToCompactType[(int) TType.Bool] = Types.BooleanTrue; |
| 93 | TTypeToCompactType[(int) TType.Byte] = Types.Byte; |
| 94 | TTypeToCompactType[(int) TType.I16] = Types.I16; |
| 95 | TTypeToCompactType[(int) TType.I32] = Types.I32; |
| 96 | TTypeToCompactType[(int) TType.I64] = Types.I64; |
| 97 | TTypeToCompactType[(int) TType.Double] = Types.Double; |
| 98 | TTypeToCompactType[(int) TType.String] = Types.Binary; |
| 99 | TTypeToCompactType[(int) TType.List] = Types.List; |
| 100 | TTypeToCompactType[(int) TType.Set] = Types.Set; |
| 101 | TTypeToCompactType[(int) TType.Map] = Types.Map; |
| 102 | TTypeToCompactType[(int) TType.Struct] = Types.Struct; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 103 | |
| 104 | CompactTypeToTType[Types.Stop] = TType.Stop; |
| 105 | CompactTypeToTType[Types.BooleanTrue] = TType.Bool; |
| 106 | CompactTypeToTType[Types.BooleanFalse] = TType.Bool; |
| 107 | CompactTypeToTType[Types.Byte] = TType.Byte; |
| 108 | CompactTypeToTType[Types.I16] = TType.I16; |
| 109 | CompactTypeToTType[Types.I32] = TType.I32; |
| 110 | CompactTypeToTType[Types.I64] = TType.I64; |
| 111 | CompactTypeToTType[Types.Double] = TType.Double; |
| 112 | CompactTypeToTType[Types.Binary] = TType.String; |
| 113 | CompactTypeToTType[Types.List] = TType.List; |
| 114 | CompactTypeToTType[Types.Set] = TType.Set; |
| 115 | CompactTypeToTType[Types.Map] = TType.Map; |
| 116 | CompactTypeToTType[Types.Struct] = TType.Struct; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public void Reset() |
| 120 | { |
| 121 | _lastField.Clear(); |
| 122 | _lastFieldId = 0; |
| 123 | } |
| 124 | |
| 125 | public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) |
| 126 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 127 | PreAllocatedBuffer[0] = ProtocolId; |
| 128 | PreAllocatedBuffer[1] = (byte)((Version & VersionMask) | (((uint)message.Type << TypeShiftAmount) & TypeMask)); |
| 129 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 2, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 130 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 131 | Int32ToVarInt((uint) message.SeqID, ref PreAllocatedVarInt); |
| 132 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 133 | |
| 134 | await WriteStringAsync(message.Name, cancellationToken); |
| 135 | } |
| 136 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 137 | public override Task WriteMessageEndAsync(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 | /// <summary> |
| 144 | /// Write a struct begin. This doesn't actually put anything on the wire. We |
| 145 | /// use it as an opportunity to put special placeholder markers on the field |
| 146 | /// stack so we can get the field id deltas correct. |
| 147 | /// </summary> |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 148 | public override Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 149 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 150 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 151 | |
| 152 | _lastField.Push(_lastFieldId); |
| 153 | _lastFieldId = 0; |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 154 | |
| 155 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 158 | public override Task WriteStructEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 159 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 160 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 161 | |
| 162 | _lastFieldId = _lastField.Pop(); |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 163 | |
| 164 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 167 | private async Task WriteFieldBeginInternalAsync(TField field, byte fieldType, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 168 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 169 | // if there's a exType override passed in, use that. Otherwise ask GetCompactType(). |
| 170 | if (fieldType == NoTypeOverride) |
| 171 | fieldType = GetCompactType(field.Type); |
| 172 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 173 | |
| 174 | // check if we can use delta encoding for the field id |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 175 | if (field.ID > _lastFieldId) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 176 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 177 | var delta = field.ID - _lastFieldId; |
| 178 | if (delta <= 15) |
| 179 | { |
| 180 | // Write them together |
| 181 | PreAllocatedBuffer[0] = (byte)((delta << 4) | fieldType); |
| 182 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
| 183 | _lastFieldId = field.ID; |
| 184 | return; |
| 185 | } |
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 | // Write them separate |
| 189 | PreAllocatedBuffer[0] = fieldType; |
| 190 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
| 191 | await WriteI16Async(field.ID, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 192 | _lastFieldId = field.ID; |
| 193 | } |
| 194 | |
| 195 | public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken) |
| 196 | { |
| 197 | if (field.Type == TType.Bool) |
| 198 | { |
| 199 | _booleanField = field; |
| 200 | } |
| 201 | else |
| 202 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 203 | await WriteFieldBeginInternalAsync(field, NoTypeOverride, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 207 | public override Task WriteFieldEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 208 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 209 | cancellationToken.ThrowIfCancellationRequested(); |
| 210 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | public override async Task WriteFieldStopAsync(CancellationToken cancellationToken) |
| 214 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 215 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 216 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 217 | PreAllocatedBuffer[0] = Types.Stop; |
| 218 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | protected async Task WriteCollectionBeginAsync(TType elemType, int size, CancellationToken cancellationToken) |
| 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 | /* |
| 226 | Abstract method for writing the start of lists and sets. List and sets on |
| 227 | the wire differ only by the exType indicator. |
| 228 | */ |
| 229 | |
| 230 | if (size <= 14) |
| 231 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 232 | PreAllocatedBuffer[0] = (byte)((size << 4) | GetCompactType(elemType)); |
| 233 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 234 | } |
| 235 | else |
| 236 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 237 | PreAllocatedBuffer[0] = (byte)(0xf0 | GetCompactType(elemType)); |
| 238 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 239 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 240 | Int32ToVarInt((uint) size, ref PreAllocatedVarInt); |
| 241 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken) |
| 246 | { |
| 247 | await WriteCollectionBeginAsync(list.ElementType, list.Count, cancellationToken); |
| 248 | } |
| 249 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 250 | public override Task WriteListEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 251 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 252 | cancellationToken.ThrowIfCancellationRequested(); |
| 253 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken) |
| 257 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 258 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 259 | |
| 260 | await WriteCollectionBeginAsync(set.ElementType, set.Count, cancellationToken); |
| 261 | } |
| 262 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 263 | public override Task WriteSetEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 264 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 265 | cancellationToken.ThrowIfCancellationRequested(); |
| 266 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken) |
| 270 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 271 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | Write a boolean value. Potentially, this could be a boolean field, in |
| 275 | which case the field header info isn't written yet. If so, decide what the |
| 276 | right exType header is for the value and then Write the field header. |
| 277 | Otherwise, Write a single byte. |
| 278 | */ |
| 279 | |
| 280 | if (_booleanField != null) |
| 281 | { |
| 282 | // we haven't written the field header yet |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 283 | var type = b ? Types.BooleanTrue : Types.BooleanFalse; |
| 284 | await WriteFieldBeginInternalAsync(_booleanField.Value, type, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 285 | _booleanField = null; |
| 286 | } |
| 287 | else |
| 288 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 289 | // we're not part of a field, so just write the value. |
| 290 | PreAllocatedBuffer[0] = b ? Types.BooleanTrue : Types.BooleanFalse; |
| 291 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken) |
| 296 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 297 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 298 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 299 | PreAllocatedBuffer[0] = (byte)b; |
| 300 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | public override async Task WriteI16Async(short i16, CancellationToken cancellationToken) |
| 304 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 305 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 306 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 307 | Int32ToVarInt(IntToZigzag(i16), ref PreAllocatedVarInt); |
| 308 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 311 | private static void Int32ToVarInt(uint n, ref VarInt varint) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 312 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 313 | // Write an i32 as a varint. Results in 1 - 5 bytes on the wire. |
| 314 | varint.count = 0; |
| 315 | Debug.Assert(varint.bytes.Length >= 5); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 316 | |
| 317 | while (true) |
| 318 | { |
| 319 | if ((n & ~0x7F) == 0) |
| 320 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 321 | varint.bytes[varint.count++] = (byte)n; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 325 | varint.bytes[varint.count++] = (byte)((n & 0x7F) | 0x80); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 326 | n >>= 7; |
| 327 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | public override async Task WriteI32Async(int i32, CancellationToken cancellationToken) |
| 331 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 332 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 333 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 334 | Int32ToVarInt(IntToZigzag(i32), ref PreAllocatedVarInt); |
| 335 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 338 | static private void Int64ToVarInt(ulong n, ref VarInt varint) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 339 | { |
| 340 | // Write an i64 as a varint. Results in 1-10 bytes on the wire. |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 341 | varint.count = 0; |
| 342 | Debug.Assert(varint.bytes.Length >= 10); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 343 | |
| 344 | while (true) |
| 345 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 346 | if ((n & ~(ulong)0x7FL) == 0) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 347 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 348 | varint.bytes[varint.count++] = (byte)n; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 349 | break; |
| 350 | } |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 351 | varint.bytes[varint.count++] = (byte)((n & 0x7F) | 0x80); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 352 | n >>= 7; |
| 353 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | public override async Task WriteI64Async(long i64, CancellationToken cancellationToken) |
| 357 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 358 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 359 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 360 | Int64ToVarInt(LongToZigzag(i64), ref PreAllocatedVarInt); |
| 361 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken) |
| 365 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 366 | cancellationToken.ThrowIfCancellationRequested(); |
| 367 | |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 368 | BinaryPrimitives.WriteInt64LittleEndian(PreAllocatedBuffer, BitConverter.DoubleToInt64Bits(d)); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 369 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 8, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | public override async Task WriteStringAsync(string str, CancellationToken cancellationToken) |
| 373 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 374 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 375 | |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 376 | var buf = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetByteCount(str)); |
| 377 | try |
| 378 | { |
| 379 | var numberOfBytes = Encoding.UTF8.GetBytes(str, 0, str.Length, buf, 0); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 380 | |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 381 | Int32ToVarInt((uint)numberOfBytes, ref PreAllocatedVarInt); |
| 382 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
| 383 | await Trans.WriteAsync(buf, 0, numberOfBytes, cancellationToken); |
| 384 | } |
| 385 | finally |
| 386 | { |
| 387 | ArrayPool<byte>.Shared.Return(buf); |
| 388 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken) |
| 392 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 393 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 394 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 395 | Int32ToVarInt((uint) bytes.Length, ref PreAllocatedVarInt); |
| 396 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 397 | await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken); |
| 398 | } |
| 399 | |
| 400 | public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken) |
| 401 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 402 | cancellationToken.ThrowIfCancellationRequested(); |
| 403 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 404 | if (map.Count == 0) |
| 405 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 406 | PreAllocatedBuffer[0] = 0; |
| 407 | await Trans.WriteAsync( PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 408 | } |
| 409 | else |
| 410 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 411 | Int32ToVarInt((uint) map.Count, ref PreAllocatedVarInt); |
| 412 | await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); |
| 413 | |
| 414 | PreAllocatedBuffer[0] = (byte)((GetCompactType(map.KeyType) << 4) | GetCompactType(map.ValueType)); |
| 415 | await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 419 | public override Task WriteMapEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 420 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 421 | cancellationToken.ThrowIfCancellationRequested(); |
| 422 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 425 | public override async ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 426 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 427 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 428 | |
| 429 | var protocolId = (byte) await ReadByteAsync(cancellationToken); |
| 430 | if (protocolId != ProtocolId) |
| 431 | { |
| 432 | throw new TProtocolException($"Expected protocol id {ProtocolId:X} but got {protocolId:X}"); |
| 433 | } |
| 434 | |
| 435 | var versionAndType = (byte) await ReadByteAsync(cancellationToken); |
| 436 | var version = (byte) (versionAndType & VersionMask); |
| 437 | |
| 438 | if (version != Version) |
| 439 | { |
| 440 | throw new TProtocolException($"Expected version {Version} but got {version}"); |
| 441 | } |
| 442 | |
| 443 | var type = (byte) ((versionAndType >> TypeShiftAmount) & TypeBits); |
| 444 | var seqid = (int) await ReadVarInt32Async(cancellationToken); |
| 445 | var messageName = await ReadStringAsync(cancellationToken); |
| 446 | |
| 447 | return new TMessage(messageName, (TMessageType) type, seqid); |
| 448 | } |
| 449 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 450 | public override Task ReadMessageEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 451 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 452 | cancellationToken.ThrowIfCancellationRequested(); |
| 453 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 456 | public override ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 457 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 458 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 459 | |
| 460 | _lastField.Push(_lastFieldId); |
| 461 | _lastFieldId = 0; |
| 462 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 463 | return new ValueTask<TStruct>(AnonymousStruct); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 466 | public override Task ReadStructEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 467 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 468 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | Doesn't actually consume any wire data, just removes the last field for |
| 472 | this struct from the field stack. |
| 473 | */ |
| 474 | |
| 475 | // consume the last field we Read off the wire. |
| 476 | _lastFieldId = _lastField.Pop(); |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 477 | |
| 478 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 481 | public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 482 | { |
| 483 | // Read a field header off the wire. |
| 484 | var type = (byte) await ReadByteAsync(cancellationToken); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 485 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 486 | // if it's a stop, then we can return immediately, as the struct is over. |
| 487 | if (type == Types.Stop) |
| 488 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 489 | return StopField; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 492 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 493 | // mask off the 4 MSB of the exType header. it could contain a field id delta. |
| 494 | var modifier = (short) ((type & 0xf0) >> 4); |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 495 | var compactType = (byte)(type & 0x0f); |
| 496 | |
| 497 | short fieldId; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 498 | if (modifier == 0) |
| 499 | { |
| 500 | fieldId = await ReadI16Async(cancellationToken); |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | fieldId = (short) (_lastFieldId + modifier); |
| 505 | } |
| 506 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 507 | var ttype = GetTType(compactType); |
| 508 | var field = new TField(string.Empty, ttype, fieldId); |
| 509 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 510 | // if this happens to be a boolean field, the value is encoded in the exType |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 511 | if( ttype == TType.Bool) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 512 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 513 | _boolValue = (compactType == Types.BooleanTrue); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // push the new field onto the field stack so we can keep the deltas going. |
| 517 | _lastFieldId = field.ID; |
| 518 | return field; |
| 519 | } |
| 520 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 521 | public override Task ReadFieldEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 522 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 523 | cancellationToken.ThrowIfCancellationRequested(); |
| 524 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 527 | public override async ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 528 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 529 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | Read a map header off the wire. If the size is zero, skip Reading the key |
| 533 | and value exType. This means that 0-length maps will yield TMaps without the |
| 534 | "correct" types. |
| 535 | */ |
| 536 | |
| 537 | var size = (int) await ReadVarInt32Async(cancellationToken); |
| 538 | var keyAndValueType = size == 0 ? (byte) 0 : (byte) await ReadByteAsync(cancellationToken); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 539 | var map = new TMap(GetTType((byte) (keyAndValueType >> 4)), GetTType((byte) (keyAndValueType & 0xf)), size); |
| 540 | CheckReadBytesAvailable(map); |
| 541 | return map; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 544 | public override Task ReadMapEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 545 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 546 | cancellationToken.ThrowIfCancellationRequested(); |
| 547 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 550 | public override async ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 551 | { |
| 552 | /* |
| 553 | Read a set header off the wire. If the set size is 0-14, the size will |
| 554 | be packed into the element exType header. If it's a longer set, the 4 MSB |
| 555 | of the element exType header will be 0xF, and a varint will follow with the |
| 556 | true size. |
| 557 | */ |
| 558 | |
| 559 | return new TSet(await ReadListBeginAsync(cancellationToken)); |
| 560 | } |
| 561 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 562 | public override ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 563 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 564 | /* |
| 565 | Read a boolean off the wire. If this is a boolean field, the value should |
| 566 | already have been Read during ReadFieldBegin, so we'll just consume the |
| 567 | pre-stored value. Otherwise, Read a byte. |
| 568 | */ |
| 569 | |
| 570 | if (_boolValue != null) |
| 571 | { |
| 572 | var result = _boolValue.Value; |
| 573 | _boolValue = null; |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 574 | return new ValueTask<bool>(result); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 577 | return InternalCall(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 578 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 579 | async ValueTask<bool> InternalCall() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 580 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 581 | var data = await ReadByteAsync(cancellationToken); |
| 582 | return (data == Types.BooleanTrue); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 583 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 584 | } |
| 585 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 586 | |
| 587 | public override async ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken) |
| 588 | { |
| 589 | // Read a single byte off the wire. Nothing interesting here. |
| 590 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 1, cancellationToken); |
| 591 | return (sbyte)PreAllocatedBuffer[0]; |
| 592 | } |
| 593 | |
| 594 | public override async ValueTask<short> ReadI16Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 595 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 596 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 597 | |
| 598 | return (short) ZigzagToInt(await ReadVarInt32Async(cancellationToken)); |
| 599 | } |
| 600 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 601 | public override async ValueTask<int> ReadI32Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 602 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 603 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 604 | |
| 605 | return ZigzagToInt(await ReadVarInt32Async(cancellationToken)); |
| 606 | } |
| 607 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 608 | public override async ValueTask<long> ReadI64Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 609 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 610 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 611 | |
| 612 | return ZigzagToLong(await ReadVarInt64Async(cancellationToken)); |
| 613 | } |
| 614 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 615 | public override async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 616 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 617 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 618 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 619 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 8, cancellationToken); |
zembord | 9d958a3 | 2019-11-21 13:11:44 +0300 | [diff] [blame] | 620 | |
| 621 | return BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(PreAllocatedBuffer)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 624 | public override async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 625 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 626 | // read length |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 627 | var length = (int) await ReadVarInt32Async(cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 628 | if (length == 0) |
| 629 | { |
| 630 | return string.Empty; |
| 631 | } |
| 632 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 633 | // read and decode data |
| 634 | if (length < PreAllocatedBuffer.Length) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 635 | { |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 636 | await Trans.ReadAllAsync(PreAllocatedBuffer, 0, length, cancellationToken); |
| 637 | return Encoding.UTF8.GetString(PreAllocatedBuffer, 0, length); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 640 | Transport.CheckReadBytesAvailable(length); |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 641 | |
| 642 | var buf = ArrayPool<byte>.Shared.Rent(length); |
| 643 | try |
| 644 | { |
| 645 | await Trans.ReadAllAsync(buf, 0, length, cancellationToken); |
| 646 | return Encoding.UTF8.GetString(buf, 0, length); |
| 647 | } |
| 648 | finally |
| 649 | { |
| 650 | ArrayPool<byte>.Shared.Return(buf); |
| 651 | } |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | public override async ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken) |
| 655 | { |
| 656 | // read length |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 657 | var length = (int) await ReadVarInt32Async(cancellationToken); |
| 658 | if (length == 0) |
| 659 | { |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 660 | return Array.Empty<byte>(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 663 | // read data |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 664 | Transport.CheckReadBytesAvailable(length); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 665 | var buf = new byte[length]; |
| 666 | await Trans.ReadAllAsync(buf, 0, length, cancellationToken); |
| 667 | return buf; |
| 668 | } |
| 669 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 670 | public override async ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 671 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 672 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | Read a list header off the wire. If the list size is 0-14, the size will |
| 676 | be packed into the element exType header. If it's a longer list, the 4 MSB |
| 677 | of the element exType header will be 0xF, and a varint will follow with the |
| 678 | true size. |
| 679 | */ |
| 680 | |
| 681 | var sizeAndType = (byte) await ReadByteAsync(cancellationToken); |
| 682 | var size = (sizeAndType >> 4) & 0x0f; |
| 683 | if (size == 15) |
| 684 | { |
| 685 | size = (int) await ReadVarInt32Async(cancellationToken); |
| 686 | } |
| 687 | |
| 688 | var type = GetTType(sizeAndType); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 689 | var list = new TList(type, size); |
| 690 | CheckReadBytesAvailable(list); |
| 691 | return list; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 694 | public override Task ReadListEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 695 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 696 | cancellationToken.ThrowIfCancellationRequested(); |
| 697 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 700 | public override Task ReadSetEndAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 701 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 702 | cancellationToken.ThrowIfCancellationRequested(); |
| 703 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | private static byte GetCompactType(TType ttype) |
| 707 | { |
| 708 | // Given a TType value, find the appropriate TCompactProtocol.Types constant. |
| 709 | return TTypeToCompactType[(int) ttype]; |
| 710 | } |
| 711 | |
| 712 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 713 | private async ValueTask<uint> ReadVarInt32Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 714 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 715 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 716 | |
| 717 | /* |
| 718 | Read an i32 from the wire as a varint. The MSB of each byte is set |
| 719 | if there is another byte to follow. This can Read up to 5 bytes. |
| 720 | */ |
| 721 | |
| 722 | uint result = 0; |
| 723 | var shift = 0; |
| 724 | |
| 725 | while (true) |
| 726 | { |
| 727 | var b = (byte) await ReadByteAsync(cancellationToken); |
| 728 | result |= (uint) (b & 0x7f) << shift; |
| 729 | if ((b & 0x80) != 0x80) |
| 730 | { |
| 731 | break; |
| 732 | } |
| 733 | shift += 7; |
| 734 | } |
| 735 | |
| 736 | return result; |
| 737 | } |
| 738 | |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 739 | private async ValueTask<ulong> ReadVarInt64Async(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 740 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 741 | cancellationToken.ThrowIfCancellationRequested(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 742 | |
| 743 | /* |
| 744 | Read an i64 from the wire as a proper varint. The MSB of each byte is set |
| 745 | if there is another byte to follow. This can Read up to 10 bytes. |
| 746 | */ |
| 747 | |
| 748 | var shift = 0; |
| 749 | ulong result = 0; |
| 750 | while (true) |
| 751 | { |
| 752 | var b = (byte) await ReadByteAsync(cancellationToken); |
| 753 | result |= (ulong) (b & 0x7f) << shift; |
| 754 | if ((b & 0x80) != 0x80) |
| 755 | { |
| 756 | break; |
| 757 | } |
| 758 | shift += 7; |
| 759 | } |
| 760 | |
| 761 | return result; |
| 762 | } |
| 763 | |
| 764 | private static int ZigzagToInt(uint n) |
| 765 | { |
| 766 | return (int) (n >> 1) ^ -(int) (n & 1); |
| 767 | } |
| 768 | |
| 769 | private static long ZigzagToLong(ulong n) |
| 770 | { |
| 771 | return (long) (n >> 1) ^ -(long) (n & 1); |
| 772 | } |
| 773 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 774 | private static TType GetTType(byte type) |
| 775 | { |
| 776 | // Given a TCompactProtocol.Types constant, convert it to its corresponding TType value. |
Jens Geyer | 5a17b13 | 2019-05-26 15:53:37 +0200 | [diff] [blame] | 777 | return CompactTypeToTType[type & 0x0f]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | private static ulong LongToZigzag(long n) |
| 781 | { |
| 782 | // Convert l into a zigzag long. This allows negative numbers to be represented compactly as a varint |
| 783 | return (ulong) (n << 1) ^ (ulong) (n >> 63); |
| 784 | } |
| 785 | |
| 786 | private static uint IntToZigzag(int n) |
| 787 | { |
| 788 | // Convert n into a zigzag int. This allows negative numbers to be represented compactly as a varint |
| 789 | return (uint) (n << 1) ^ (uint) (n >> 31); |
| 790 | } |
| 791 | |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 792 | // Return the minimum number of bytes a type will consume on the wire |
| 793 | public override int GetMinSerializedSize(TType type) |
| 794 | { |
| 795 | switch (type) |
| 796 | { |
| 797 | case TType.Stop: return 0; |
| 798 | case TType.Void: return 0; |
| 799 | case TType.Bool: return sizeof(byte); |
| 800 | case TType.Double: return 8; // uses fixedLongToBytes() which always writes 8 bytes |
| 801 | case TType.Byte: return sizeof(byte); |
| 802 | case TType.I16: return sizeof(byte); // zigzag |
| 803 | case TType.I32: return sizeof(byte); // zigzag |
| 804 | case TType.I64: return sizeof(byte); // zigzag |
| 805 | case TType.String: return sizeof(byte); // string length |
| 806 | case TType.Struct: return 0; // empty struct |
| 807 | case TType.Map: return sizeof(byte); // element count |
| 808 | case TType.Set: return sizeof(byte); // element count |
| 809 | case TType.List: return sizeof(byte); // element count |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 810 | default: throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "unrecognized type code"); |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 814 | public class Factory : TProtocolFactory |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 815 | { |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 816 | public override TProtocol GetProtocol(TTransport trans) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 817 | { |
| 818 | return new TCompactProtocol(trans); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | /// <summary> |
| 823 | /// All of the on-wire exType codes. |
| 824 | /// </summary> |
| 825 | private static class Types |
| 826 | { |
| 827 | public const byte Stop = 0x00; |
| 828 | public const byte BooleanTrue = 0x01; |
| 829 | public const byte BooleanFalse = 0x02; |
| 830 | public const byte Byte = 0x03; |
| 831 | public const byte I16 = 0x04; |
| 832 | public const byte I32 = 0x05; |
| 833 | public const byte I64 = 0x06; |
| 834 | public const byte Double = 0x07; |
| 835 | public const byte Binary = 0x08; |
| 836 | public const byte List = 0x09; |
| 837 | public const byte Set = 0x0A; |
| 838 | public const byte Map = 0x0B; |
| 839 | public const byte Struct = 0x0C; |
| 840 | } |
| 841 | } |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 842 | } |