diff options
author | Jens Geyer <jensg@apache.org> | 2020-05-16 23:02:27 +0200 |
---|---|---|
committer | Jens Geyer <jensg@apache.org> | 2020-05-17 22:34:10 +0200 |
commit | dce229911cd61a441961df2219623fb166b5d139 (patch) | |
tree | c96204f51d0147f4abccd14fe1691280d35698ce /lib/netstd | |
parent | 261de348fe0e83ca2f4c7398f2f03ff28d3eb81e (diff) | |
download | thrift-dce229911cd61a441961df2219623fb166b5d139.tar.gz |
THRIFT-5210 further refactoring and performance optimizations
Client: netstd
Patch: Jens Geyer
This closes #2146
Diffstat (limited to 'lib/netstd')
20 files changed, 293 insertions, 780 deletions
diff --git a/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs b/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs index cb6b07f94..16dcc766e 100644 --- a/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs +++ b/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs @@ -29,7 +29,7 @@ namespace Thrift.Benchmarks public class CompactProtocolBenchmarks { private MemoryStream _Stream; - private TCompactProtocol _Protocol; + private TProtocol _Protocol; [Params(10000)] public int NumberOfOperationsPerIteration { get; set; } diff --git a/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs b/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs index b8df515de..62ab31780 100644 --- a/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs +++ b/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs @@ -57,13 +57,13 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteMessageBeginAsync(expected); - await protocol.WriteMessageEndAsync(); + await protocol.WriteMessageBeginAsync(expected, default); + await protocol.WriteMessageEndAsync(default); stream.Seek(0, SeekOrigin.Begin); - var actualMessage = await protocol.ReadMessageBeginAsync(); - await protocol.ReadMessageEndAsync(); + var actualMessage = await protocol.ReadMessageBeginAsync(default); + await protocol.ReadMessageEndAsync(default); var result = _compareLogic.Compare(expected, actualMessage); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -91,13 +91,13 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteStructBeginAsync(expected); - await protocol.WriteStructEndAsync(); + await protocol.WriteStructBeginAsync(expected, default); + await protocol.WriteStructEndAsync(default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadStructBeginAsync(); - await protocol.ReadStructEndAsync(); + var actual = await protocol.ReadStructBeginAsync(default); + await protocol.ReadStructEndAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -126,13 +126,13 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteFieldBeginAsync(expected); - await protocol.WriteFieldEndAsync(); + await protocol.WriteFieldBeginAsync(expected, default); + await protocol.WriteFieldEndAsync(default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadFieldBeginAsync(); - await protocol.ReadFieldEndAsync(); + var actual = await protocol.ReadFieldBeginAsync(default); + await protocol.ReadFieldEndAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -159,13 +159,13 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteMapBeginAsync(expected); - await protocol.WriteMapEndAsync(); + await protocol.WriteMapBeginAsync(expected, default); + await protocol.WriteMapEndAsync(default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadMapBeginAsync(); - await protocol.ReadMapEndAsync(); + var actual = await protocol.ReadMapBeginAsync(default); + await protocol.ReadMapEndAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -193,13 +193,13 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteListBeginAsync(expected); - await protocol.WriteListEndAsync(); + await protocol.WriteListBeginAsync(expected, default); + await protocol.WriteListEndAsync(default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadListBeginAsync(); - await protocol.ReadListEndAsync(); + var actual = await protocol.ReadListBeginAsync(default); + await protocol.ReadListEndAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -226,13 +226,13 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteSetBeginAsync(expected); - await protocol.WriteSetEndAsync(); + await protocol.WriteSetBeginAsync(expected, default); + await protocol.WriteSetEndAsync(default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadSetBeginAsync(); - await protocol.ReadSetEndAsync(); + var actual = await protocol.ReadSetBeginAsync(default); + await protocol.ReadSetEndAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -259,11 +259,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteBoolAsync(expected); + await protocol.WriteBoolAsync(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadBoolAsync(); + var actual = await protocol.ReadBoolAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -290,11 +290,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteByteAsync(expected); + await protocol.WriteByteAsync(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadByteAsync(); + var actual = await protocol.ReadByteAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -321,11 +321,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteI16Async(expected); + await protocol.WriteI16Async(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadI16Async(); + var actual = await protocol.ReadI16Async(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -352,11 +352,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteI32Async(expected); + await protocol.WriteI32Async(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadI32Async(); + var actual = await protocol.ReadI32Async(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -383,11 +383,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteI64Async(expected); + await protocol.WriteI64Async(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadI64Async(); + var actual = await protocol.ReadI64Async(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -414,11 +414,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteDoubleAsync(expected); + await protocol.WriteDoubleAsync(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadDoubleAsync(); + var actual = await protocol.ReadDoubleAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -445,11 +445,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteStringAsync(expected); + await protocol.WriteStringAsync(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadStringAsync(); + var actual = await protocol.ReadStringAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); @@ -476,11 +476,11 @@ namespace Thrift.IntegrationTests.Protocols { var protocol = tuple.Item2; - await protocol.WriteBinaryAsync(expected); + await protocol.WriteBinaryAsync(expected, default); stream?.Seek(0, SeekOrigin.Begin); - var actual = await protocol.ReadBinaryAsync(); + var actual = await protocol.ReadBinaryAsync(default); var result = _compareLogic.Compare(expected, actual); Assert.IsTrue(result.AreEqual, result.DifferencesString); diff --git a/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs b/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs index e5aeaa6c7..b19221054 100644 --- a/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs +++ b/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs @@ -40,10 +40,7 @@ namespace Thrift.Processor public async Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<bool>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); try { @@ -129,14 +126,10 @@ namespace Thrift.Processor _msgBegin = messageBegin; } - public override async ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) + public override ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TMessage>(cancellationToken); - } - - return _msgBegin; + cancellationToken.ThrowIfCancellationRequested(); + return new ValueTask<TMessage>(_msgBegin); } } } diff --git a/lib/netstd/Thrift/Protocol/Entities/TStruct.cs b/lib/netstd/Thrift/Protocol/Entities/TStruct.cs index e04167e47..d87608799 100644 --- a/lib/netstd/Thrift/Protocol/Entities/TStruct.cs +++ b/lib/netstd/Thrift/Protocol/Entities/TStruct.cs @@ -18,13 +18,13 @@ namespace Thrift.Protocol.Entities { // ReSharper disable once InconsistentNaming - public struct TStruct + public readonly struct TStruct { public TStruct(string name) { Name = name; } - public string Name { get; set; } + public string Name { get; } } -}
\ No newline at end of file +} diff --git a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs index a00c5c162..28b7d2978 100644 --- a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs +++ b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs @@ -38,9 +38,6 @@ namespace Thrift.Protocol // The value of 128 is arbitrarily chosen, the required minimum size must be sizeof(long) private byte[] PreAllocatedBuffer = new byte[128]; - private static readonly TStruct AnonymousStruct = new TStruct(string.Empty); - private static readonly TField StopField = new TField() { Type = TType.Stop }; - public TBinaryProtocol(TTransport trans) : this(trans, false, true) { @@ -55,10 +52,7 @@ namespace Thrift.Protocol public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); if (StrictWrite) { @@ -75,65 +69,47 @@ namespace Thrift.Protocol } } - public override async Task WriteMessageEndAsync(CancellationToken cancellationToken) + public override Task WriteMessageEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } - public override async Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken) + public override Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } - public override async Task WriteStructEndAsync(CancellationToken cancellationToken) + public override Task WriteStructEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } - + cancellationToken.ThrowIfCancellationRequested(); await WriteByteAsync((sbyte) field.Type, cancellationToken); await WriteI16Async(field.ID, cancellationToken); } - public override async Task WriteFieldEndAsync(CancellationToken cancellationToken) + public override Task WriteFieldEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteFieldStopAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); await WriteByteAsync((sbyte) TType.Stop, cancellationToken); } public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); PreAllocatedBuffer[0] = (byte)map.KeyType; PreAllocatedBuffer[1] = (byte)map.ValueType; @@ -142,69 +118,47 @@ namespace Thrift.Protocol await WriteI32Async(map.Count, cancellationToken); } - public override async Task WriteMapEndAsync(CancellationToken cancellationToken) - + public override Task WriteMapEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } - + cancellationToken.ThrowIfCancellationRequested(); await WriteByteAsync((sbyte) list.ElementType, cancellationToken); await WriteI32Async(list.Count, cancellationToken); } - public override async Task WriteListEndAsync(CancellationToken cancellationToken) + public override Task WriteListEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } - + cancellationToken.ThrowIfCancellationRequested(); await WriteByteAsync((sbyte) set.ElementType, cancellationToken); await WriteI32Async(set.Count, cancellationToken); } - public override async Task WriteSetEndAsync(CancellationToken cancellationToken) + public override Task WriteSetEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } - + cancellationToken.ThrowIfCancellationRequested(); await WriteByteAsync(b ? (sbyte) 1 : (sbyte) 0, cancellationToken); } public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); PreAllocatedBuffer[0] = (byte)b; @@ -212,10 +166,8 @@ namespace Thrift.Protocol } public override async Task WriteI16Async(short i16, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); + BinaryPrimitives.WriteInt16BigEndian(PreAllocatedBuffer, i16); await Trans.WriteAsync(PreAllocatedBuffer, 0, 2, cancellationToken); @@ -223,10 +175,7 @@ namespace Thrift.Protocol public override async Task WriteI32Async(int i32, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); BinaryPrimitives.WriteInt32BigEndian(PreAllocatedBuffer, i32); @@ -236,10 +185,7 @@ namespace Thrift.Protocol public override async Task WriteI64Async(long i64, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); BinaryPrimitives.WriteInt64BigEndian(PreAllocatedBuffer, i64); @@ -248,10 +194,7 @@ namespace Thrift.Protocol public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); await WriteI64Async(BitConverter.DoubleToInt64Bits(d), cancellationToken); } @@ -259,10 +202,7 @@ namespace Thrift.Protocol public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); await WriteI32Async(bytes.Length, cancellationToken); await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken); @@ -270,10 +210,7 @@ namespace Thrift.Protocol public override async ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TMessage>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var message = new TMessage(); var size = await ReadI32Async(cancellationToken); @@ -303,39 +240,27 @@ namespace Thrift.Protocol return message; } - public override async Task ReadMessageEndAsync(CancellationToken cancellationToken) + public override Task ReadMessageEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } - public override async ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) + public override ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } - - return AnonymousStruct; + cancellationToken.ThrowIfCancellationRequested(); + return new ValueTask<TStruct>(AnonymousStruct); } - public override async Task ReadStructEndAsync(CancellationToken cancellationToken) + public override Task ReadStructEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TField>(cancellationToken); - } - + cancellationToken.ThrowIfCancellationRequested(); var type = (TType)await ReadByteAsync(cancellationToken); if (type == TType.Stop) @@ -349,21 +274,16 @@ namespace Thrift.Protocol }; } - public override async Task ReadFieldEndAsync(CancellationToken cancellationToken) + public override Task ReadFieldEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TMap>(cancellationToken); - } - + cancellationToken.ThrowIfCancellationRequested(); + var map = new TMap { KeyType = (TType) await ReadByteAsync(cancellationToken), @@ -374,20 +294,15 @@ namespace Thrift.Protocol return map; } - public override async Task ReadMapEndAsync(CancellationToken cancellationToken) + public override Task ReadMapEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TList>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var list = new TList { @@ -398,20 +313,15 @@ namespace Thrift.Protocol return list; } - public override async Task ReadListEndAsync(CancellationToken cancellationToken) + public override Task ReadListEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TSet>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var set = new TSet { @@ -422,30 +332,22 @@ namespace Thrift.Protocol return set; } - public override async Task ReadSetEndAsync(CancellationToken cancellationToken) + public override Task ReadSetEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<bool>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); return await ReadByteAsync(cancellationToken) == 1; } public override async ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<sbyte>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 1, cancellationToken); return (sbyte)PreAllocatedBuffer[0]; @@ -453,10 +355,7 @@ namespace Thrift.Protocol public override async ValueTask<short> ReadI16Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<short>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 2, cancellationToken); var result = BinaryPrimitives.ReadInt16BigEndian(PreAllocatedBuffer); @@ -465,10 +364,7 @@ namespace Thrift.Protocol public override async ValueTask<int> ReadI32Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<int>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 4, cancellationToken); @@ -479,10 +375,7 @@ namespace Thrift.Protocol public override async ValueTask<long> ReadI64Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<long>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 8, cancellationToken); return BinaryPrimitives.ReadInt64BigEndian(PreAllocatedBuffer); @@ -490,10 +383,7 @@ namespace Thrift.Protocol public override async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<double>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var d = await ReadI64Async(cancellationToken); return BitConverter.Int64BitsToDouble(d); @@ -501,10 +391,7 @@ namespace Thrift.Protocol public override async ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<byte[]>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var size = await ReadI32Async(cancellationToken); Transport.CheckReadBytesAvailable(size); @@ -515,10 +402,7 @@ namespace Thrift.Protocol public override async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<string>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var size = await ReadI32Async(cancellationToken); return size > 0 ? await ReadStringBodyAsync(size, cancellationToken) : string.Empty; @@ -526,10 +410,7 @@ namespace Thrift.Protocol private async ValueTask<string> ReadStringBodyAsync(int size, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled<string>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); if (size <= PreAllocatedBuffer.Length) { diff --git a/lib/netstd/Thrift/Protocol/TCompactProtocol.cs b/lib/netstd/Thrift/Protocol/TCompactProtocol.cs index bb531f42e..066b32722 100644 --- a/lib/netstd/Thrift/Protocol/TCompactProtocol.cs +++ b/lib/netstd/Thrift/Protocol/TCompactProtocol.cs @@ -39,8 +39,6 @@ namespace Thrift.Protocol private const byte TypeMask = 0xE0; // 1110 0000 private const byte TypeBits = 0x07; // 0000 0111 private const int TypeShiftAmount = 5; - private static readonly TStruct AnonymousStruct = new TStruct(string.Empty); - private static readonly TField StopField = new TField(string.Empty, TType.Stop, 0); private const byte NoTypeOverride = 0xFF; @@ -134,12 +132,10 @@ namespace Thrift.Protocol await WriteStringAsync(message.Name, cancellationToken); } - public override async Task WriteMessageEndAsync(CancellationToken cancellationToken) + public override Task WriteMessageEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } /// <summary> @@ -147,25 +143,23 @@ namespace Thrift.Protocol /// use it as an opportunity to put special placeholder markers on the field /// stack so we can get the field id deltas correct. /// </summary> - public override async Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken) + public override Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); _lastField.Push(_lastFieldId); _lastFieldId = 0; + + return Task.CompletedTask; } - public override async Task WriteStructEndAsync(CancellationToken cancellationToken) + public override Task WriteStructEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); _lastFieldId = _lastField.Pop(); + + return Task.CompletedTask; } private async Task WriteFieldBeginInternalAsync(TField field, byte fieldType, CancellationToken cancellationToken) @@ -208,20 +202,15 @@ namespace Thrift.Protocol } } - public override async Task WriteFieldEndAsync(CancellationToken cancellationToken) + public override Task WriteFieldEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteFieldStopAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); PreAllocatedBuffer[0] = Types.Stop; await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); @@ -229,10 +218,7 @@ namespace Thrift.Protocol protected async Task WriteCollectionBeginAsync(TType elemType, int size, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); /* Abstract method for writing the start of lists and sets. List and sets on @@ -259,38 +245,28 @@ namespace Thrift.Protocol await WriteCollectionBeginAsync(list.ElementType, list.Count, cancellationToken); } - public override async Task WriteListEndAsync(CancellationToken cancellationToken) + public override Task WriteListEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); await WriteCollectionBeginAsync(set.ElementType, set.Count, cancellationToken); } - public override async Task WriteSetEndAsync(CancellationToken cancellationToken) + public override Task WriteSetEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); /* Write a boolean value. Potentially, this could be a boolean field, in @@ -316,10 +292,7 @@ namespace Thrift.Protocol public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); PreAllocatedBuffer[0] = (byte)b; await Trans.WriteAsync(PreAllocatedBuffer, 0, 1, cancellationToken); @@ -327,10 +300,7 @@ namespace Thrift.Protocol public override async Task WriteI16Async(short i16, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); Int32ToVarInt(IntToZigzag(i16), ref PreAllocatedVarInt); await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); @@ -357,10 +327,7 @@ namespace Thrift.Protocol public override async Task WriteI32Async(int i32, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); Int32ToVarInt(IntToZigzag(i32), ref PreAllocatedVarInt); await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); @@ -386,10 +353,7 @@ namespace Thrift.Protocol public override async Task WriteI64Async(long i64, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); Int64ToVarInt(LongToZigzag(i64), ref PreAllocatedVarInt); await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); @@ -397,20 +361,15 @@ namespace Thrift.Protocol public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); + BinaryPrimitives.WriteInt64LittleEndian(PreAllocatedBuffer, BitConverter.DoubleToInt64Bits(d)); await Trans.WriteAsync(PreAllocatedBuffer, 0, 8, cancellationToken); } public override async Task WriteStringAsync(string str, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); var buf = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetByteCount(str)); try @@ -429,10 +388,7 @@ namespace Thrift.Protocol public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } + cancellationToken.ThrowIfCancellationRequested(); Int32ToVarInt((uint) bytes.Length, ref PreAllocatedVarInt); await Trans.WriteAsync(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count, cancellationToken); @@ -441,11 +397,8 @@ namespace Thrift.Protocol public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return; - } - + cancellationToken.ThrowIfCancellationRequested(); + if (map.Count == 0) { PreAllocatedBuffer[0] = 0; @@ -461,20 +414,15 @@ namespace Thrift.Protocol } } - public override async Task WriteMapEndAsync(CancellationToken cancellationToken) + public override Task WriteMapEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TMessage>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); var protocolId = (byte) await ReadByteAsync(cancellationToken); if (protocolId != ProtocolId) @@ -497,35 +445,25 @@ namespace Thrift.Protocol return new TMessage(messageName, (TMessageType) type, seqid); } - public override async Task ReadMessageEndAsync(CancellationToken cancellationToken) + public override Task ReadMessageEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } - public override async ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) + public override ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TStruct>(cancellationToken); - } - - // some magic is here ) + cancellationToken.ThrowIfCancellationRequested(); _lastField.Push(_lastFieldId); _lastFieldId = 0; - return AnonymousStruct; + return new ValueTask<TStruct>(AnonymousStruct); } - public override async Task ReadStructEndAsync(CancellationToken cancellationToken) + public override Task ReadStructEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); /* Doesn't actually consume any wire data, just removes the last field for @@ -534,6 +472,8 @@ namespace Thrift.Protocol // consume the last field we Read off the wire. _lastFieldId = _lastField.Pop(); + + return Task.CompletedTask; } public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken) @@ -576,20 +516,15 @@ namespace Thrift.Protocol return field; } - public override async Task ReadFieldEndAsync(CancellationToken cancellationToken) + public override Task ReadFieldEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled<TMap>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); /* Read a map header off the wire. If the size is zero, skip Reading the key @@ -604,12 +539,10 @@ namespace Thrift.Protocol return map; } - public override async Task ReadMapEndAsync(CancellationToken cancellationToken) + public override Task ReadMapEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken) @@ -658,40 +591,28 @@ namespace Thrift.Protocol public override async ValueTask<short> ReadI16Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<short>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); return (short) ZigzagToInt(await ReadVarInt32Async(cancellationToken)); } public override async ValueTask<int> ReadI32Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<int>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); return ZigzagToInt(await ReadVarInt32Async(cancellationToken)); } public override async ValueTask<long> ReadI64Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<long>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); return ZigzagToLong(await ReadVarInt64Async(cancellationToken)); } public override async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<double>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 8, cancellationToken); @@ -746,10 +667,7 @@ namespace Thrift.Protocol public override async ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled<TList>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); /* Read a list header off the wire. If the list size is 0-14, the size will @@ -771,20 +689,16 @@ namespace Thrift.Protocol return list; } - public override async Task ReadListEndAsync(CancellationToken cancellationToken) + public override Task ReadListEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } - public override async Task ReadSetEndAsync(CancellationToken cancellationToken) + public override Task ReadSetEndAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } private static byte GetCompactType(TType ttype) @@ -796,10 +710,7 @@ namespace Thrift.Protocol private async ValueTask<uint> ReadVarInt32Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<uint>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); /* Read an i32 from the wire as a varint. The MSB of each byte is set @@ -825,10 +736,7 @@ namespace Thrift.Protocol private async ValueTask<ulong> ReadVarInt64Async(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<uint>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); /* Read an i64 from the wire as a proper varint. The MSB of each byte is set diff --git a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs index 3b2ed06cb..2f1ccdb8d 100644 --- a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs +++ b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs @@ -127,7 +127,7 @@ namespace Thrift.Protocol } else { - await Trans.WriteAsync(bytes.ToArray(), i, 1, cancellationToken); + await Trans.WriteAsync(bytes, i, 1, cancellationToken); } } else @@ -315,12 +315,10 @@ namespace Thrift.Protocol await WriteJsonObjectEndAsync(cancellationToken); } - public override async Task WriteFieldStopAsync(CancellationToken cancellationToken) + public override Task WriteFieldStopAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken) @@ -679,7 +677,8 @@ namespace Thrift.Protocol public override async ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken) { await ReadJsonObjectStartAsync(cancellationToken); - return new TStruct(); + + return AnonymousStruct; } public override async Task ReadStructEndAsync(CancellationToken cancellationToken) @@ -689,18 +688,19 @@ namespace Thrift.Protocol public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken) { - var field = new TField(); var ch = await Reader.PeekAsync(cancellationToken); if (ch == TJSONProtocolConstants.RightBrace[0]) { - field.Type = TType.Stop; + return StopField; } - else + + var field = new TField() { - field.ID = (short) await ReadJsonIntegerAsync(cancellationToken); - await ReadJsonObjectStartAsync(cancellationToken); - field.Type = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); - } + ID = (short)await ReadJsonIntegerAsync(cancellationToken) + }; + + await ReadJsonObjectStartAsync(cancellationToken); + field.Type = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); return field; } @@ -845,20 +845,16 @@ namespace Thrift.Protocol Proto = proto; } - public virtual async Task WriteConditionalDelimiterAsync(CancellationToken cancellationToken) + public virtual Task WriteConditionalDelimiterAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } - public virtual async Task ReadConditionalDelimiterAsync(CancellationToken cancellationToken) + public virtual Task ReadConditionalDelimiterAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public virtual bool EscapeNumbers() @@ -978,10 +974,7 @@ namespace Thrift.Protocol /// </summary> public async ValueTask<byte> ReadAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<byte>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); if (_hasData) { @@ -1001,10 +994,7 @@ namespace Thrift.Protocol /// </summary> public async ValueTask<byte> PeekAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<byte>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); if (!_hasData) { diff --git a/lib/netstd/Thrift/Protocol/TProtocol.cs b/lib/netstd/Thrift/Protocol/TProtocol.cs index 5275c9cd1..5b1bec16b 100644 --- a/lib/netstd/Thrift/Protocol/TProtocol.cs +++ b/lib/netstd/Thrift/Protocol/TProtocol.cs @@ -32,6 +32,10 @@ namespace Thrift.Protocol protected TTransport Trans; + protected static readonly TStruct AnonymousStruct = new TStruct(string.Empty); + protected static readonly TField StopField = new TField() { Type = TType.Stop }; + + protected TProtocol(TTransport trans) { Trans = trans; @@ -98,299 +102,94 @@ namespace Thrift.Protocol public abstract int GetMinSerializedSize(TType type); - public virtual async Task WriteMessageBeginAsync(TMessage message) - { - await WriteMessageBeginAsync(message, CancellationToken.None); - } - - public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken); - - public virtual async Task WriteMessageEndAsync() - { - await WriteMessageEndAsync(CancellationToken.None); - } - - public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken); - - public virtual async Task WriteStructBeginAsync(TStruct @struct) - { - await WriteStructBeginAsync(@struct, CancellationToken.None); - } - - public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken); - - public virtual async Task WriteStructEndAsync() - { - await WriteStructEndAsync(CancellationToken.None); - } - - public abstract Task WriteStructEndAsync(CancellationToken cancellationToken); - - public virtual async Task WriteFieldBeginAsync(TField field) - { - await WriteFieldBeginAsync(field, CancellationToken.None); - } - - public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken); - - public virtual async Task WriteFieldEndAsync() - { - await WriteFieldEndAsync(CancellationToken.None); - } - - public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken); - - public virtual async Task WriteFieldStopAsync() - { - await WriteFieldStopAsync(CancellationToken.None); - } - - public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken); - - public virtual async Task WriteMapBeginAsync(TMap map) - { - await WriteMapBeginAsync(map, CancellationToken.None); - } - - public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken); - - public virtual async Task WriteMapEndAsync() - { - await WriteMapEndAsync(CancellationToken.None); - } - - public abstract Task WriteMapEndAsync(CancellationToken cancellationToken); - - public virtual async Task WriteListBeginAsync(TList list) - { - await WriteListBeginAsync(list, CancellationToken.None); - } - - public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken); - - public virtual async Task WriteListEndAsync() - { - await WriteListEndAsync(CancellationToken.None); - } - - public abstract Task WriteListEndAsync(CancellationToken cancellationToken); - - public virtual async Task WriteSetBeginAsync(TSet set) - { - await WriteSetBeginAsync(set, CancellationToken.None); - } + public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken = default); - public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken); + public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken = default); - public virtual async Task WriteSetEndAsync() - { - await WriteSetEndAsync(CancellationToken.None); - } + public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken = default); - public abstract Task WriteSetEndAsync(CancellationToken cancellationToken); + public abstract Task WriteStructEndAsync(CancellationToken cancellationToken = default); - public virtual async Task WriteBoolAsync(bool b) - { - await WriteBoolAsync(b, CancellationToken.None); - } + public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken = default); + + public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken = default); + + public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken = default); - public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken); + public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken = default); - public virtual async Task WriteByteAsync(sbyte b) - { - await WriteByteAsync(b, CancellationToken.None); - } + public abstract Task WriteMapEndAsync(CancellationToken cancellationToken = default); - public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken); + public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken = default); - public virtual async Task WriteI16Async(short i16) - { - await WriteI16Async(i16, CancellationToken.None); - } + public abstract Task WriteListEndAsync(CancellationToken cancellationToken = default); - public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken); + public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken = default); - public virtual async Task WriteI32Async(int i32) - { - await WriteI32Async(i32, CancellationToken.None); - } + public abstract Task WriteSetEndAsync(CancellationToken cancellationToken = default); - public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken); + public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken = default); - public virtual async Task WriteI64Async(long i64) - { - await WriteI64Async(i64, CancellationToken.None); - } + public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken = default); - public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken); + public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken = default); - public virtual async Task WriteDoubleAsync(double d) - { - await WriteDoubleAsync(d, CancellationToken.None); - } + public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken = default); - public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken); + public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken = default); - public virtual async Task WriteStringAsync(string s) - { - await WriteStringAsync(s, CancellationToken.None); - } + public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken = default); - public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken) + public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken = default) { var bytes = Encoding.UTF8.GetBytes(s); await WriteBinaryAsync(bytes, cancellationToken); } - public virtual async Task WriteBinaryAsync(byte[] bytes) - { - await WriteBinaryAsync(bytes, CancellationToken.None); - } - - public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken); - - public virtual async ValueTask<TMessage> ReadMessageBeginAsync() - { - return await ReadMessageBeginAsync(CancellationToken.None); - } - - public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken); - - public virtual async Task ReadMessageEndAsync() - { - await ReadMessageEndAsync(CancellationToken.None); - } + public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken = default); - public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken); - - public virtual async ValueTask<TStruct> ReadStructBeginAsync() - { - return await ReadStructBeginAsync(CancellationToken.None); - } + public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken = default); - public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken); + public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken = default); - public virtual async Task ReadStructEndAsync() - { - await ReadStructEndAsync(CancellationToken.None); - } + public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken = default); - public abstract Task ReadStructEndAsync(CancellationToken cancellationToken); + public abstract Task ReadStructEndAsync(CancellationToken cancellationToken = default); - public virtual async ValueTask<TField> ReadFieldBeginAsync() - { - return await ReadFieldBeginAsync(CancellationToken.None); - } + public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken = default); - public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken); + public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken = default); - public virtual async Task ReadFieldEndAsync() - { - await ReadFieldEndAsync(CancellationToken.None); - } + public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken = default); - public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken); + public abstract Task ReadMapEndAsync(CancellationToken cancellationToken = default); - public virtual async ValueTask<TMap> ReadMapBeginAsync() - { - return await ReadMapBeginAsync(CancellationToken.None); - } + public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken = default); - public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken); + public abstract Task ReadListEndAsync(CancellationToken cancellationToken = default); - public virtual async Task ReadMapEndAsync() - { - await ReadMapEndAsync(CancellationToken.None); - } + public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken = default); - public abstract Task ReadMapEndAsync(CancellationToken cancellationToken); + public abstract Task ReadSetEndAsync(CancellationToken cancellationToken = default); - public virtual async ValueTask<TList> ReadListBeginAsync() - { - return await ReadListBeginAsync(CancellationToken.None); - } + public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken = default); - public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken); + public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken = default); - public virtual async Task ReadListEndAsync() - { - await ReadListEndAsync(CancellationToken.None); - } + public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken = default); - public abstract Task ReadListEndAsync(CancellationToken cancellationToken); + public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken = default); - public virtual async ValueTask<TSet> ReadSetBeginAsync() - { - return await ReadSetBeginAsync(CancellationToken.None); - } + public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken = default); - public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken); + public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default); - public virtual async Task ReadSetEndAsync() - { - await ReadSetEndAsync(CancellationToken.None); - } - - public abstract Task ReadSetEndAsync(CancellationToken cancellationToken); - - public virtual async ValueTask<bool> ReadBoolAsync() - { - return await ReadBoolAsync(CancellationToken.None); - } - - public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken); - - public virtual async ValueTask<sbyte> ReadByteAsync() - { - return await ReadByteAsync(CancellationToken.None); - } - - public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken); - - public virtual async ValueTask<short> ReadI16Async() - { - return await ReadI16Async(CancellationToken.None); - } - - public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken); - - public virtual async ValueTask<int> ReadI32Async() - { - return await ReadI32Async(CancellationToken.None); - } - - public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken); - - public virtual async ValueTask<long> ReadI64Async() - { - return await ReadI64Async(CancellationToken.None); - } - - public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken); - - public virtual async ValueTask<double> ReadDoubleAsync() - { - return await ReadDoubleAsync(CancellationToken.None); - } - - public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken); - - public virtual async ValueTask<string> ReadStringAsync() - { - return await ReadStringAsync(CancellationToken.None); - } - - public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken) + public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken = default) { var buf = await ReadBinaryAsync(cancellationToken); return Encoding.UTF8.GetString(buf, 0, buf.Length); } - public virtual async ValueTask<byte[]> ReadBinaryAsync() - { - return await ReadBinaryAsync(CancellationToken.None); - } - - public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken); + public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken = default); } } diff --git a/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs b/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs index 18f92d816..832e46e6c 100644 --- a/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs +++ b/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs @@ -26,10 +26,7 @@ namespace Thrift.Protocol.Utilities { public static async Task SkipAsync(TProtocol protocol, TType type, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); protocol.IncrementRecursionDepth(); try diff --git a/lib/netstd/Thrift/Server/TServer.cs b/lib/netstd/Thrift/Server/TServer.cs index f40f2b7e7..3fd0bc531 100644 --- a/lib/netstd/Thrift/Server/TServer.cs +++ b/lib/netstd/Thrift/Server/TServer.cs @@ -76,12 +76,10 @@ namespace Thrift.Server // do nothing } - public virtual async Task ServeAsync(CancellationToken cancellationToken) + public virtual Task ServeAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } } } diff --git a/lib/netstd/Thrift/TApplicationException.cs b/lib/netstd/Thrift/TApplicationException.cs index 67ac2f8c7..514db9a08 100644 --- a/lib/netstd/Thrift/TApplicationException.cs +++ b/lib/netstd/Thrift/TApplicationException.cs @@ -1,4 +1,4 @@ -// Licensed to the Apache Software Foundation(ASF) under one +// Licensed to the Apache Software Foundation(ASF) under one // or more contributor license agreements.See the NOTICE file // distributed with this work for additional information // regarding copyright ownership.The ASF licenses this file @@ -112,10 +112,7 @@ namespace Thrift public async Task WriteAsync(TProtocol outputProtocol, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); const string messageTypeFieldName = "message"; const string exTypeFieldName = "exType"; @@ -147,4 +144,4 @@ namespace Thrift await outputProtocol.WriteStructEndAsync(cancellationToken); } } -}
\ No newline at end of file +} diff --git a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs index 0790cc882..46071cd4a 100644 --- a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs @@ -68,12 +68,10 @@ namespace Thrift.Transport.Client public MediaTypeHeaderValue ContentType { get; set; } - public override async Task OpenAsync(CancellationToken cancellationToken) + public override Task OpenAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override void Close() @@ -99,8 +97,7 @@ namespace Thrift.Transport.Client public override async ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - return await Task.FromCanceled<int>(cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); if (_inputStream == null) throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent"); @@ -130,10 +127,7 @@ namespace Thrift.Transport.Client public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); await _outputStream.WriteAsync(buffer, offset, length, cancellationToken); } diff --git a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs index 290e50cc5..797de4e57 100644 --- a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs @@ -81,12 +81,10 @@ namespace Thrift.Transport.Client public override bool IsOpen => true; - public override async Task OpenAsync(CancellationToken cancellationToken) + public override Task OpenAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override void Close() @@ -143,13 +141,11 @@ namespace Thrift.Transport.Client return Task.CompletedTask; } - public override async Task FlushAsync(CancellationToken cancellationToken) + public override Task FlushAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); ResetConsumedMessageSize(); + return Task.CompletedTask; } public byte[] GetBuffer() diff --git a/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs b/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs index 8dab6a063..815983ea8 100644 --- a/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/TNamedPipeTransport.cs @@ -101,13 +101,12 @@ namespace Thrift.Transport.Client } } - public override async Task FlushAsync(CancellationToken cancellationToken) + public override Task FlushAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + ResetConsumedMessageSize(); + return Task.CompletedTask; } diff --git a/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs b/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs index 612d3daa4..5e3245b8c 100644 --- a/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/TSocketTransport.cs @@ -106,10 +106,7 @@ namespace Thrift.Transport.Client public override async Task OpenAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); if (IsOpen) { diff --git a/lib/netstd/Thrift/Transport/Client/TStreamTransport.cs b/lib/netstd/Thrift/Transport/Client/TStreamTransport.cs index ccadad025..b397460d7 100644 --- a/lib/netstd/Thrift/Transport/Client/TStreamTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/TStreamTransport.cs @@ -51,12 +51,10 @@ namespace Thrift.Transport.Client public override bool IsOpen => true; - public override async Task OpenAsync(CancellationToken cancellationToken) + public override Task OpenAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override void Close() diff --git a/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs b/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs index 4b82cbd22..aea0f865a 100644 --- a/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs +++ b/lib/netstd/Thrift/Transport/Server/TNamedPipeServerTransport.cs @@ -256,12 +256,10 @@ namespace Thrift.Transport.Server public override bool IsOpen => PipeStream != null && PipeStream.IsConnected; - public override async Task OpenAsync(CancellationToken cancellationToken) + public override Task OpenAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; } public override void Close() @@ -306,14 +304,12 @@ namespace Thrift.Transport.Server } } - public override async Task FlushAsync(CancellationToken cancellationToken) + public override Task FlushAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - await Task.FromCanceled(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); ResetConsumedMessageSize(); + return Task.CompletedTask; } protected override void Dispose(bool disposing) diff --git a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs index 6656b641a..281c7ffe5 100644 --- a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs +++ b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs @@ -77,10 +77,7 @@ namespace Thrift.Transport.Server protected override async ValueTask<TTransport> AcceptImplementationAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TTransport>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); if (_server == null) { diff --git a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs index 9f7456252..79d2b1137 100644 --- a/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs +++ b/lib/netstd/Thrift/Transport/Server/TTlsServerSocketTransport.cs @@ -104,10 +104,7 @@ namespace Thrift.Transport.Server protected override async ValueTask<TTransport> AcceptImplementationAsync(CancellationToken cancellationToken) { - if (cancellationToken.IsCancellationRequested) - { - return await Task.FromCanceled<TTransport>(cancellationToken); - } + cancellationToken.ThrowIfCancellationRequested(); if (_server == null) { diff --git a/lib/netstd/Thrift/Transport/TTransport.cs b/lib/netstd/Thrift/Transport/TTransport.cs index dedd51dc5..3f4245aa2 100644 --- a/lib/netstd/Thrift/Transport/TTransport.cs +++ b/lib/netstd/Thrift/Transport/TTransport.cs @@ -73,12 +73,8 @@ namespace Thrift.Transport return true; } - public virtual async Task OpenAsync() - { - await OpenAsync(CancellationToken.None); - } - public abstract Task OpenAsync(CancellationToken cancellationToken); + public abstract Task OpenAsync(CancellationToken cancellationToken = default); public abstract void Close(); @@ -107,25 +103,14 @@ namespace Thrift.Transport #endif } - public virtual async ValueTask<int> ReadAsync(byte[] buffer, int offset, int length) - { - return await ReadAsync(buffer, offset, length, CancellationToken.None); - } public abstract ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken); - public virtual async ValueTask<int> ReadAllAsync(byte[] buffer, int offset, int length) - { - return await ReadAllAsync(buffer, offset, length, CancellationToken.None); - } - public virtual async ValueTask<int> ReadAllAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) { - ValidateBufferArgs(buffer, offset, length); - - if (cancellationToken.IsCancellationRequested) - return await Task.FromCanceled<int>(cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); + ValidateBufferArgs(buffer, offset, length); if (length <= 0) return 0; @@ -164,11 +149,6 @@ namespace Thrift.Transport } } - public virtual async Task WriteAsync(byte[] buffer) - { - await WriteAsync(buffer, CancellationToken.None); - } - public virtual async Task WriteAsync(byte[] buffer, CancellationToken cancellationToken) { await WriteAsync(buffer, 0, buffer.Length, CancellationToken.None); @@ -181,10 +161,6 @@ namespace Thrift.Transport public abstract Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken); - public virtual async Task FlushAsync() - { - await FlushAsync(CancellationToken.None); - } public abstract Task FlushAsync(CancellationToken cancellationToken); |