JaegerExporter: Performance Improvements (#494)
* Initial work. * First complete version of Jaeger with perf improvements. * Added JaegerExporter benchmarks. * More performance revisions. * ValueTask perf improvements. * Missed a couple tasks. * Removed null check against struct. * Tweaked how structs are used. * Figured out why the structs were eating more memory. Fixed up unit tests. * Reverted change to ToJaegerSpan extension signature. * Fixed JaegerUdpBatcher not calling CloseAsync in dispose, fixed SpanExporters not being disposed. * Reference System.Threading.Tasks.Extensions for net46 & netstandard2.0 builds of Thirft to add in ValueTask. * Removed the fork of Thrift in favor of the official package. * Increased number of spans per iteration in jaeger benchmarks. Switched ApacheThrift to PrivateAssets=all in jaeger exporter project. * New algorithm for memory management. * Fixed double-allocation of tags. * Immutable structs. Added cancellationToken to IJaegerClient interface. * Added lists that can be backed be pooled buffers. * Everything in implementation folder is now internal. * Return ref from PooledList indexer. Co-authored-by: Sergey Kanzhelev <S.Kanzhelev@live.com>
This commit is contained in:
parent
677c5ae558
commit
d5b4248261
|
|
@ -81,10 +81,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.Jaeg
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.Jaeger.Tests", "test\OpenTelemetry.Exporter.Jaeger.Tests\OpenTelemetry.Exporter.Jaeger.Tests.csproj", "{21E69213-72D5-453F-BD00-75EF36AC4965}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Thrift", "lib\Thrift\Thrift.csproj", "{ED179037-DDB3-4780-A24F-F56278623EBB}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{9D2D6933-C28C-4541-9A25-FDAE0DA5A5D4}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Shims.OpenTracing", "src\OpenTelemetry.Shims.OpenTracing\OpenTelemetry.Shims.OpenTracing.csproj", "{AAC408FE-40EF-4479-97D9-697F2C1A0B28}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Shims.OpenTracing.Tests", "test\OpenTelemetry.Shims.OpenTracing.Tests\OpenTelemetry.Shims.OpenTracing.Tests.csproj", "{49A7853F-5B6F-4B65-A781-7D29A1C92164}"
|
||||
|
|
@ -205,10 +201,6 @@ Global
|
|||
{21E69213-72D5-453F-BD00-75EF36AC4965}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{21E69213-72D5-453F-BD00-75EF36AC4965}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{21E69213-72D5-453F-BD00-75EF36AC4965}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{ED179037-DDB3-4780-A24F-F56278623EBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{ED179037-DDB3-4780-A24F-F56278623EBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{ED179037-DDB3-4780-A24F-F56278623EBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{ED179037-DDB3-4780-A24F-F56278623EBB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AAC408FE-40EF-4479-97D9-697F2C1A0B28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AAC408FE-40EF-4479-97D9-697F2C1A0B28}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AAC408FE-40EF-4479-97D9-697F2C1A0B28}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
|
@ -275,7 +267,6 @@ Global
|
|||
{1EEF77DF-7552-4265-B64C-FA13BC40C256} = {D9C14CDA-5182-4DEA-8606-98FF1A388BD3}
|
||||
{607B3861-4D69-43EF-84CE-19F18CD5339A} = {D9C14CDA-5182-4DEA-8606-98FF1A388BD3}
|
||||
{1EB74FCE-55C5-476A-8BB0-C46B77FE1070} = {D9C14CDA-5182-4DEA-8606-98FF1A388BD3}
|
||||
{ED179037-DDB3-4780-A24F-F56278623EBB} = {9D2D6933-C28C-4541-9A25-FDAE0DA5A5D4}
|
||||
{CEB7F146-81DC-41DB-8015-140EC6A64E6C} = {0169B149-FB8B-46F4-9EF7-8A0E69F8FAAF}
|
||||
{47318988-CA8B-4C81-B55D-2FA11D295A49} = {E359BB2B-9AEC-497D-B321-7DF2450C3B8E}
|
||||
{25C06046-C7D0-46B4-AAAC-90C50C43DE7A} = {E359BB2B-9AEC-497D-B321-7DF2450C3B8E}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@
|
|||
<OutputType>Exe</OutputType>
|
||||
<TargetFrameworks>netcoreapp3.1;net462</TargetFrameworks>
|
||||
<IsPackable>false</IsPackable>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)/../build/debug.snk</AssemblyOriginatorKeyFile>
|
||||
<DefineConstants>$(DefineConstants);SIGNED</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
|
@ -19,8 +22,16 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\src\OpenTelemetry.Exporter.Jaeger\OpenTelemetry.Exporter.Jaeger.csproj" />
|
||||
<ProjectReference Include="..\src\OpenTelemetry.Exporter.Zipkin\OpenTelemetry.Exporter.Zipkin.csproj" />
|
||||
<ProjectReference Include="..\src\OpenTelemetry\OpenTelemetry.csproj" />
|
||||
<PackageReference Include="ApacheThrift" Version="0.13.0.1" ExcludeAssets="Compile" GeneratePathProperty="true" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="ApacheThrift">
|
||||
<HintPath>$(PkgApacheThrift)\lib\netstandard2.0\Thrift.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,173 @@
|
|||
// <copyright file="JaegerExporterBenchmarks.cs" company="OpenTelemetry Authors">
|
||||
// Copyright 2018, OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using OpenTelemetry.Exporter.Jaeger;
|
||||
using OpenTelemetry.Exporter.Jaeger.Implementation;
|
||||
using OpenTelemetry.Trace;
|
||||
using OpenTelemetry.Trace.Export;
|
||||
using Thrift.Transport;
|
||||
|
||||
namespace Benchmarks.Exporter
|
||||
{
|
||||
[MemoryDiagnoser]
|
||||
#if !NET462
|
||||
[ThreadingDiagnoser]
|
||||
#endif
|
||||
public class JaegerExporterBenchmarks
|
||||
{
|
||||
[Params(1, 10, 100)]
|
||||
public int NumberOfBatches { get; set; }
|
||||
|
||||
[Params(10000)]
|
||||
public int NumberOfSpans { get; set; }
|
||||
|
||||
private SpanData testSpan;
|
||||
|
||||
[GlobalSetup]
|
||||
public void GlobalSetup()
|
||||
{
|
||||
this.testSpan = this.CreateTestSpan();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public async Task JaegerExporter_Batching()
|
||||
{
|
||||
using (var jaegerUdpBatcher = new JaegerUdpBatcher(
|
||||
new JaegerExporterOptions(),
|
||||
new BlackHoleTransport()))
|
||||
{
|
||||
jaegerUdpBatcher.Process = new OpenTelemetry.Exporter.Jaeger.Process("TestService");
|
||||
|
||||
for (int i = 0; i < this.NumberOfBatches; i++)
|
||||
{
|
||||
for (int c = 0; c < this.NumberOfSpans; c++)
|
||||
{
|
||||
await jaegerUdpBatcher.AppendAsync(this.testSpan, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await jaegerUdpBatcher.FlushAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class BlackHoleTransport : TTransport
|
||||
{
|
||||
public override bool IsOpen => true;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public override ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private SpanData CreateTestSpan()
|
||||
{
|
||||
var startTimestamp = new DateTimeOffset(2019, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
||||
var endTimestamp = startTimestamp.AddSeconds(60);
|
||||
var eventTimestamp = new DateTimeOffset(2019, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
||||
|
||||
var traceId = ActivityTraceId.CreateFromString("e8ea7e9ac72de94e91fabc613f9686b2".AsSpan());
|
||||
var spanId = ActivitySpanId.CreateFromString("6a69db47429ea340".AsSpan());
|
||||
var parentSpanId = ActivitySpanId.CreateFromBytes(new byte[] { 12, 23, 34, 45, 56, 67, 78, 89 });
|
||||
var attributes = new Dictionary<string, object>
|
||||
{
|
||||
{ "stringKey", "value"},
|
||||
{ "longKey", 1L},
|
||||
{ "longKey2", 1 },
|
||||
{ "doubleKey", 1D},
|
||||
{ "doubleKey2", 1F},
|
||||
{ "boolKey", true},
|
||||
};
|
||||
var events = new List<Event>
|
||||
{
|
||||
new Event(
|
||||
"Event1",
|
||||
eventTimestamp,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{ "key", "value" },
|
||||
}
|
||||
),
|
||||
new Event(
|
||||
"Event2",
|
||||
eventTimestamp,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{ "key", "value" },
|
||||
}
|
||||
),
|
||||
};
|
||||
|
||||
var linkedSpanId = ActivitySpanId.CreateFromString("888915b6286b9c41".AsSpan());
|
||||
|
||||
var link = new Link(new SpanContext(
|
||||
traceId,
|
||||
linkedSpanId,
|
||||
ActivityTraceFlags.Recorded));
|
||||
|
||||
return new SpanData(
|
||||
"Name",
|
||||
new SpanContext(traceId, spanId, ActivityTraceFlags.Recorded),
|
||||
parentSpanId,
|
||||
SpanKind.Client,
|
||||
startTimestamp,
|
||||
attributes,
|
||||
events,
|
||||
new[] { link, },
|
||||
null,
|
||||
Status.Ok,
|
||||
endTimestamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<Project>
|
||||
<Import Project="..\Directory.Build.props" Condition="Exists('..\Directory.Build.props')" />
|
||||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenTelemetry.sln'))\build\Common.prod.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<SkipAnalysis>True</SkipAnalysis>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
<Project>
|
||||
<Import Project="..\Directory.Build.targets" Condition="Exists('..\Directory.Build.targets')" />
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-*">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -1,101 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Collections;
|
||||
|
||||
namespace Thrift.Collections
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TCollections
|
||||
{
|
||||
/// <summary>
|
||||
/// This will return true if the two collections are value-wise the same.
|
||||
/// If the collection contains a collection, the collections will be compared using this method.
|
||||
/// </summary>
|
||||
public static bool Equals(IEnumerable first, IEnumerable second)
|
||||
{
|
||||
if (first == null && second == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (first == null || second == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var fiter = first.GetEnumerator();
|
||||
var siter = second.GetEnumerator();
|
||||
|
||||
var fnext = fiter.MoveNext();
|
||||
var snext = siter.MoveNext();
|
||||
|
||||
while (fnext && snext)
|
||||
{
|
||||
var fenum = fiter.Current as IEnumerable;
|
||||
var senum = siter.Current as IEnumerable;
|
||||
|
||||
if (fenum != null && senum != null)
|
||||
{
|
||||
if (!Equals(fenum, senum))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (fenum == null ^ senum == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (!Equals(fiter.Current, siter.Current))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
fnext = fiter.MoveNext();
|
||||
snext = siter.MoveNext();
|
||||
}
|
||||
|
||||
return fnext == snext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This returns a hashcode based on the value of the enumerable.
|
||||
/// </summary>
|
||||
public static int GetHashCode(IEnumerable enumerable)
|
||||
{
|
||||
if (enumerable == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var hashcode = 0;
|
||||
|
||||
foreach (var obj in enumerable)
|
||||
{
|
||||
var enum2 = obj as IEnumerable;
|
||||
var objHash = enum2 == null ? obj.GetHashCode() : GetHashCode(enum2);
|
||||
|
||||
unchecked
|
||||
{
|
||||
hashcode = (hashcode*397) ^ (objHash);
|
||||
}
|
||||
}
|
||||
|
||||
return hashcode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Thrift.Collections
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class THashSet<T> : ICollection<T>
|
||||
{
|
||||
private readonly HashSet<T> _set = new HashSet<T>();
|
||||
|
||||
public int Count => _set.Count;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
_set.Add(item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_set.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(T item)
|
||||
{
|
||||
return _set.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
_set.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return _set.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<T>) _set).GetEnumerator();
|
||||
}
|
||||
|
||||
public bool Remove(T item)
|
||||
{
|
||||
return _set.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
public interface ITAsyncProcessor
|
||||
{
|
||||
Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot);
|
||||
Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using Thrift.Server;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public interface ITProcessorFactory
|
||||
{
|
||||
ITAsyncProcessor GetAsyncProcessor(TClientTransport trans, TBaseServer baseServer = null);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public struct TField
|
||||
{
|
||||
public TField(string name, TType type, short id)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
ID = id;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public TType Type { get; set; }
|
||||
|
||||
// ReSharper disable once InconsistentNaming - do not rename - it used for generation
|
||||
public short ID { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public struct TList
|
||||
{
|
||||
public TList(TType elementType, int count)
|
||||
{
|
||||
ElementType = elementType;
|
||||
Count = count;
|
||||
}
|
||||
|
||||
public TType ElementType { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public struct TMap
|
||||
{
|
||||
public TMap(TType keyType, TType valueType, int count)
|
||||
{
|
||||
KeyType = keyType;
|
||||
ValueType = valueType;
|
||||
Count = count;
|
||||
}
|
||||
|
||||
public TType KeyType { get; set; }
|
||||
|
||||
public TType ValueType { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public struct TMessage
|
||||
{
|
||||
public TMessage(string name, TMessageType type, int seqid)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
SeqID = seqid;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public TMessageType Type { get; set; }
|
||||
|
||||
// ReSharper disable once InconsistentNaming - do not rename - it used for generation
|
||||
public int SeqID { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public enum TMessageType
|
||||
{
|
||||
Call = 1,
|
||||
Reply = 2,
|
||||
Exception = 3,
|
||||
Oneway = 4
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public struct TSet
|
||||
{
|
||||
public TSet(TType elementType, int count)
|
||||
{
|
||||
ElementType = elementType;
|
||||
Count = count;
|
||||
}
|
||||
|
||||
public TSet(TList list)
|
||||
: this(list.ElementType, list.Count)
|
||||
{
|
||||
}
|
||||
|
||||
public TType ElementType { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public struct TStruct
|
||||
{
|
||||
public TStruct(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Entities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public enum TType : byte
|
||||
{
|
||||
Stop = 0,
|
||||
Void = 1,
|
||||
Bool = 2,
|
||||
Byte = 3,
|
||||
Double = 4,
|
||||
I16 = 6,
|
||||
I32 = 8,
|
||||
I64 = 10,
|
||||
String = 11,
|
||||
Struct = 12,
|
||||
Map = 13,
|
||||
Set = 14,
|
||||
List = 15
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public interface ITProtocolFactory
|
||||
{
|
||||
TProtocol GetProtocol(TClientTransport trans);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public interface TAbstractBase
|
||||
{
|
||||
Task WriteAsync(TProtocol tProtocol, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public interface TBase : TAbstractBase
|
||||
{
|
||||
Task ReadAsync(TProtocol tProtocol, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,613 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TBinaryProtocol : TProtocol
|
||||
{
|
||||
//TODO: Unit tests
|
||||
//TODO: Localization
|
||||
//TODO: pragma
|
||||
|
||||
protected const uint VersionMask = 0xffff0000;
|
||||
protected const uint Version1 = 0x80010000;
|
||||
|
||||
protected bool StrictRead;
|
||||
protected bool StrictWrite;
|
||||
|
||||
public TBinaryProtocol(TClientTransport trans)
|
||||
: this(trans, false, true)
|
||||
{
|
||||
}
|
||||
|
||||
public TBinaryProtocol(TClientTransport trans, bool strictRead, bool strictWrite)
|
||||
: base(trans)
|
||||
{
|
||||
StrictRead = strictRead;
|
||||
StrictWrite = strictWrite;
|
||||
}
|
||||
|
||||
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (StrictWrite)
|
||||
{
|
||||
var version = Version1 | (uint) message.Type;
|
||||
await WriteI32Async((int) version, cancellationToken).ConfigureAwait(false);
|
||||
await WriteStringAsync(message.Name, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI32Async(message.SeqID, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await WriteStringAsync(message.Name, cancellationToken).ConfigureAwait(false);
|
||||
await WriteByteAsync((sbyte) message.Type, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI32Async(message.SeqID, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteByteAsync((sbyte) field.Type, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI16Async(field.ID, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteFieldStopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteByteAsync((sbyte) TType.Stop, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteByteAsync((sbyte) map.KeyType, cancellationToken).ConfigureAwait(false);
|
||||
await WriteByteAsync((sbyte) map.ValueType, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI32Async(map.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteByteAsync((sbyte) list.ElementType, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI32Async(list.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteByteAsync((sbyte) set.ElementType, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI32Async(set.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteByteAsync(b ? (sbyte) 1 : (sbyte) 0, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal static byte[] CreateWriteByte(sbyte b)
|
||||
{
|
||||
var bout = new byte[1];
|
||||
|
||||
bout[0] = (byte) b;
|
||||
|
||||
return bout;
|
||||
}
|
||||
|
||||
public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bout = CreateWriteByte(b);
|
||||
await Trans.WriteAsync(bout, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal static byte[] CreateWriteI16(short s)
|
||||
{
|
||||
var i16Out = new byte[2];
|
||||
|
||||
i16Out[0] = (byte) (0xff & (s >> 8));
|
||||
i16Out[1] = (byte) (0xff & s);
|
||||
|
||||
return i16Out;
|
||||
}
|
||||
|
||||
public override async Task WriteI16Async(short i16, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var i16Out = CreateWriteI16(i16);
|
||||
await Trans.WriteAsync(i16Out, 0, 2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal static byte[] CreateWriteI32(int i32)
|
||||
{
|
||||
var i32Out = new byte[4];
|
||||
|
||||
i32Out[0] = (byte) (0xff & (i32 >> 24));
|
||||
i32Out[1] = (byte) (0xff & (i32 >> 16));
|
||||
i32Out[2] = (byte) (0xff & (i32 >> 8));
|
||||
i32Out[3] = (byte) (0xff & i32);
|
||||
|
||||
return i32Out;
|
||||
}
|
||||
|
||||
public override async Task WriteI32Async(int i32, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var i32Out = CreateWriteI32(i32);
|
||||
await Trans.WriteAsync(i32Out, 0, 4, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal static byte[] CreateWriteI64(long i64)
|
||||
{
|
||||
var i64Out = new byte[8];
|
||||
|
||||
i64Out[0] = (byte) (0xff & (i64 >> 56));
|
||||
i64Out[1] = (byte) (0xff & (i64 >> 48));
|
||||
i64Out[2] = (byte) (0xff & (i64 >> 40));
|
||||
i64Out[3] = (byte) (0xff & (i64 >> 32));
|
||||
i64Out[4] = (byte) (0xff & (i64 >> 24));
|
||||
i64Out[5] = (byte) (0xff & (i64 >> 16));
|
||||
i64Out[6] = (byte) (0xff & (i64 >> 8));
|
||||
i64Out[7] = (byte) (0xff & i64);
|
||||
|
||||
return i64Out;
|
||||
}
|
||||
|
||||
public override async Task WriteI64Async(long i64, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var i64Out = CreateWriteI64(i64);
|
||||
await Trans.WriteAsync(i64Out, 0, 8, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteI64Async(BitConverter.DoubleToInt64Bits(d), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteI32Async(bytes.Length, cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TMessage>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var message = new TMessage();
|
||||
var size = await ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
if (size < 0)
|
||||
{
|
||||
var version = (uint) size & VersionMask;
|
||||
if (version != Version1)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.BAD_VERSION,
|
||||
$"Bad version in ReadMessageBegin: {version}");
|
||||
}
|
||||
message.Type = (TMessageType) (size & 0x000000ff);
|
||||
message.Name = await ReadStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
message.SeqID = await ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (StrictRead)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.BAD_VERSION,
|
||||
"Missing version in ReadMessageBegin, old client?");
|
||||
}
|
||||
message.Name = await ReadStringBodyAsync(size, cancellationToken).ConfigureAwait(false);
|
||||
message.Type = (TMessageType) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
message.SeqID = await ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
public override async Task ReadMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
//TODO: no read from internal transport?
|
||||
return new TStruct();
|
||||
}
|
||||
|
||||
public override async Task ReadStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TField> ReadFieldBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TField>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var field = new TField
|
||||
{
|
||||
Type = (TType) await ReadByteAsync(cancellationToken)
|
||||
};
|
||||
|
||||
if (field.Type != TType.Stop)
|
||||
{
|
||||
field.ID = await ReadI16Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
public override async Task ReadFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TMap> ReadMapBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TMap>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var map = new TMap
|
||||
{
|
||||
KeyType = (TType) await ReadByteAsync(cancellationToken),
|
||||
ValueType = (TType) await ReadByteAsync(cancellationToken),
|
||||
Count = await ReadI32Async(cancellationToken)
|
||||
};
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public override async Task ReadMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TList> ReadListBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TList>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var list = new TList
|
||||
{
|
||||
ElementType = (TType) await ReadByteAsync(cancellationToken),
|
||||
Count = await ReadI32Async(cancellationToken)
|
||||
};
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public override async Task ReadListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TSet> ReadSetBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TSet>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var set = new TSet
|
||||
{
|
||||
ElementType = (TType) await ReadByteAsync(cancellationToken),
|
||||
Count = await ReadI32Async(cancellationToken)
|
||||
};
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
public override async Task ReadSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<bool> ReadBoolAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<bool>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return (await ReadByteAsync(cancellationToken).ConfigureAwait(false)) == 1;
|
||||
}
|
||||
|
||||
public override async Task<sbyte> ReadByteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<sbyte>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var bin = new byte[1];
|
||||
await Trans.ReadAllAsync(bin, 0, 1, cancellationToken).ConfigureAwait(false); //TODO: why readall ?
|
||||
return (sbyte) bin[0];
|
||||
}
|
||||
|
||||
public override async Task<short> ReadI16Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<short>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var i16In = new byte[2];
|
||||
await Trans.ReadAllAsync(i16In, 0, 2, cancellationToken).ConfigureAwait(false);
|
||||
var result = (short) (((i16In[0] & 0xff) << 8) | i16In[1] & 0xff);
|
||||
return result;
|
||||
}
|
||||
|
||||
public override async Task<int> ReadI32Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<int>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var i32In = new byte[4];
|
||||
await Trans.ReadAllAsync(i32In, 0, 4, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var result =
|
||||
((i32In[0] & 0xff) << 24) |
|
||||
((i32In[1] & 0xff) << 16) |
|
||||
((i32In[2] & 0xff) << 8) |
|
||||
i32In[3] & 0xff;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#pragma warning disable 675
|
||||
|
||||
protected internal long CreateReadI64(byte[] buf)
|
||||
{
|
||||
var result =
|
||||
((long) (buf[0] & 0xff) << 56) |
|
||||
((long) (buf[1] & 0xff) << 48) |
|
||||
((long) (buf[2] & 0xff) << 40) |
|
||||
((long) (buf[3] & 0xff) << 32) |
|
||||
((long) (buf[4] & 0xff) << 24) |
|
||||
((long) (buf[5] & 0xff) << 16) |
|
||||
((long) (buf[6] & 0xff) << 8) |
|
||||
buf[7] & 0xff;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#pragma warning restore 675
|
||||
|
||||
public override async Task<long> ReadI64Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<long>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var i64In = new byte[8];
|
||||
await Trans.ReadAllAsync(i64In, 0, 8, cancellationToken).ConfigureAwait(false);
|
||||
return CreateReadI64(i64In);
|
||||
}
|
||||
|
||||
public override async Task<double> ReadDoubleAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<double>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var d = await ReadI64Async(cancellationToken).ConfigureAwait(false);
|
||||
return BitConverter.Int64BitsToDouble(d);
|
||||
}
|
||||
|
||||
public override async Task<byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<byte[]>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var size = await ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
var buf = new byte[size];
|
||||
await Trans.ReadAllAsync(buf, 0, size, cancellationToken).ConfigureAwait(false);
|
||||
return buf;
|
||||
}
|
||||
|
||||
private async Task<string> ReadStringBodyAsync(int size, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled<string>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var buf = new byte[size];
|
||||
await Trans.ReadAllAsync(buf, 0, size, cancellationToken).ConfigureAwait(false);
|
||||
return Encoding.UTF8.GetString(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public class Factory : ITProtocolFactory
|
||||
{
|
||||
protected bool StrictRead;
|
||||
protected bool StrictWrite;
|
||||
|
||||
public Factory()
|
||||
: this(false, true)
|
||||
{
|
||||
}
|
||||
|
||||
public Factory(bool strictRead, bool strictWrite)
|
||||
{
|
||||
StrictRead = strictRead;
|
||||
StrictWrite = strictWrite;
|
||||
}
|
||||
|
||||
public TProtocol GetProtocol(TClientTransport trans)
|
||||
{
|
||||
return new TBinaryProtocol(trans, StrictRead, StrictWrite);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,922 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
//TODO: implementation of TProtocol
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TCompactProtocol : TProtocol
|
||||
{
|
||||
private const byte ProtocolId = 0x82;
|
||||
private const byte Version = 1;
|
||||
private const byte VersionMask = 0x1f; // 0001 1111
|
||||
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 Tstop = new TField(string.Empty, TType.Stop, 0);
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
private static readonly byte[] TTypeToCompactType = new byte[16];
|
||||
|
||||
/// <summary>
|
||||
/// Used to keep track of the last field for the current and previous structs, so we can do the delta stuff.
|
||||
/// </summary>
|
||||
private readonly Stack<short> _lastField = new Stack<short>(15);
|
||||
|
||||
/// <summary>
|
||||
/// If we encounter a boolean field begin, save the TField here so it can have the value incorporated.
|
||||
/// </summary>
|
||||
private TField? _booleanField;
|
||||
|
||||
/// <summary>
|
||||
/// If we Read a field header, and it's a boolean field, save the boolean value here so that ReadBool can use it.
|
||||
/// </summary>
|
||||
private bool? _boolValue;
|
||||
|
||||
private short _lastFieldId;
|
||||
|
||||
public TCompactProtocol(TClientTransport trans)
|
||||
: base(trans)
|
||||
{
|
||||
TTypeToCompactType[(int) TType.Stop] = Types.Stop;
|
||||
TTypeToCompactType[(int) TType.Bool] = Types.BooleanTrue;
|
||||
TTypeToCompactType[(int) TType.Byte] = Types.Byte;
|
||||
TTypeToCompactType[(int) TType.I16] = Types.I16;
|
||||
TTypeToCompactType[(int) TType.I32] = Types.I32;
|
||||
TTypeToCompactType[(int) TType.I64] = Types.I64;
|
||||
TTypeToCompactType[(int) TType.Double] = Types.Double;
|
||||
TTypeToCompactType[(int) TType.String] = Types.Binary;
|
||||
TTypeToCompactType[(int) TType.List] = Types.List;
|
||||
TTypeToCompactType[(int) TType.Set] = Types.Set;
|
||||
TTypeToCompactType[(int) TType.Map] = Types.Map;
|
||||
TTypeToCompactType[(int) TType.Struct] = Types.Struct;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_lastField.Clear();
|
||||
_lastFieldId = 0;
|
||||
}
|
||||
|
||||
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Trans.WriteAsync(new[] {ProtocolId}, cancellationToken).ConfigureAwait(false);
|
||||
await
|
||||
Trans.WriteAsync(
|
||||
new[] {(byte) ((Version & VersionMask) | (((uint) message.Type << TypeShiftAmount) & TypeMask))},
|
||||
cancellationToken);
|
||||
|
||||
var bufferTuple = CreateWriteVarInt32((uint) message.SeqID);
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await WriteStringAsync(message.Name, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a struct begin. This doesn't actually put anything on the wire. We
|
||||
/// 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)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_lastField.Push(_lastFieldId);
|
||||
_lastFieldId = 0;
|
||||
}
|
||||
|
||||
public override async Task WriteStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_lastFieldId = _lastField.Pop();
|
||||
}
|
||||
|
||||
private async Task WriteFieldBeginInternalAsync(TField field, byte typeOverride,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// if there's a exType override, use that.
|
||||
var typeToWrite = typeOverride == 0xFF ? GetCompactType(field.Type) : typeOverride;
|
||||
|
||||
// check if we can use delta encoding for the field id
|
||||
if ((field.ID > _lastFieldId) && (field.ID - _lastFieldId <= 15))
|
||||
{
|
||||
var b = (byte) (((field.ID - _lastFieldId) << 4) | typeToWrite);
|
||||
// Write them together
|
||||
await Trans.WriteAsync(new[] {b}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write them separate
|
||||
await Trans.WriteAsync(new[] {typeToWrite}, cancellationToken).ConfigureAwait(false);
|
||||
await WriteI16Async(field.ID, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_lastFieldId = field.ID;
|
||||
}
|
||||
|
||||
public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken)
|
||||
{
|
||||
if (field.Type == TType.Bool)
|
||||
{
|
||||
_booleanField = field;
|
||||
}
|
||||
else
|
||||
{
|
||||
await WriteFieldBeginInternalAsync(field, 0xFF, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteFieldStopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Trans.WriteAsync(new[] {Types.Stop}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected async Task WriteCollectionBeginAsync(TType elemType, int size, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
Abstract method for writing the start of lists and sets. List and sets on
|
||||
the wire differ only by the exType indicator.
|
||||
*/
|
||||
|
||||
if (size <= 14)
|
||||
{
|
||||
await Trans.WriteAsync(new[] {(byte) ((size << 4) | GetCompactType(elemType))}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Trans.WriteAsync(new[] {(byte) (0xf0 | GetCompactType(elemType))}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var bufferTuple = CreateWriteVarInt32((uint) size);
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteCollectionBeginAsync(list.ElementType, list.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteCollectionBeginAsync(set.ElementType, set.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
Write a boolean value. Potentially, this could be a boolean field, in
|
||||
which case the field header info isn't written yet. If so, decide what the
|
||||
right exType header is for the value and then Write the field header.
|
||||
Otherwise, Write a single byte.
|
||||
*/
|
||||
|
||||
if (_booleanField != null)
|
||||
{
|
||||
// we haven't written the field header yet
|
||||
await
|
||||
WriteFieldBeginInternalAsync(_booleanField.Value, b ? Types.BooleanTrue : Types.BooleanFalse,
|
||||
cancellationToken);
|
||||
_booleanField = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we're not part of a field, so just Write the value.
|
||||
await Trans.WriteAsync(new[] {b ? Types.BooleanTrue : Types.BooleanFalse}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Trans.WriteAsync(new[] {(byte) b}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI16Async(short i16, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bufferTuple = CreateWriteVarInt32(IntToZigzag(i16));
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal Tuple<byte[], int> CreateWriteVarInt32(uint n)
|
||||
{
|
||||
// Write an i32 as a varint.Results in 1 - 5 bytes on the wire.
|
||||
var i32Buf = new byte[5];
|
||||
var idx = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if ((n & ~0x7F) == 0)
|
||||
{
|
||||
i32Buf[idx++] = (byte) n;
|
||||
break;
|
||||
}
|
||||
|
||||
i32Buf[idx++] = (byte) ((n & 0x7F) | 0x80);
|
||||
n >>= 7;
|
||||
}
|
||||
|
||||
return new Tuple<byte[], int>(i32Buf, idx);
|
||||
}
|
||||
|
||||
public override async Task WriteI32Async(int i32, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bufferTuple = CreateWriteVarInt32(IntToZigzag(i32));
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal Tuple<byte[], int> CreateWriteVarInt64(ulong n)
|
||||
{
|
||||
// Write an i64 as a varint. Results in 1-10 bytes on the wire.
|
||||
var buf = new byte[10];
|
||||
var idx = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if ((n & ~(ulong) 0x7FL) == 0)
|
||||
{
|
||||
buf[idx++] = (byte) n;
|
||||
break;
|
||||
}
|
||||
buf[idx++] = (byte) ((n & 0x7F) | 0x80);
|
||||
n >>= 7;
|
||||
}
|
||||
|
||||
return new Tuple<byte[], int>(buf, idx);
|
||||
}
|
||||
|
||||
public override async Task WriteI64Async(long i64, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bufferTuple = CreateWriteVarInt64(LongToZigzag(i64));
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var data = new byte[8];
|
||||
FixedLongToBytes(BitConverter.DoubleToInt64Bits(d), data, 0);
|
||||
await Trans.WriteAsync(data, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStringAsync(string str, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(str);
|
||||
|
||||
var bufferTuple = CreateWriteVarInt32((uint) bytes.Length);
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bufferTuple = CreateWriteVarInt32((uint) bytes.Length);
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (map.Count == 0)
|
||||
{
|
||||
await Trans.WriteAsync(new[] {(byte) 0}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
var bufferTuple = CreateWriteVarInt32((uint) map.Count);
|
||||
await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
await
|
||||
Trans.WriteAsync(
|
||||
new[] {(byte) ((GetCompactType(map.KeyType) << 4) | GetCompactType(map.ValueType))},
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TMessage>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var protocolId = (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (protocolId != ProtocolId)
|
||||
{
|
||||
throw new TProtocolException($"Expected protocol id {ProtocolId:X} but got {protocolId:X}");
|
||||
}
|
||||
|
||||
var versionAndType = (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
var version = (byte) (versionAndType & VersionMask);
|
||||
|
||||
if (version != Version)
|
||||
{
|
||||
throw new TProtocolException($"Expected version {Version} but got {version}");
|
||||
}
|
||||
|
||||
var type = (byte) ((versionAndType >> TypeShiftAmount) & TypeBits);
|
||||
var seqid = (int) await ReadVarInt32Async(cancellationToken).ConfigureAwait(false);
|
||||
var messageName = await ReadStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new TMessage(messageName, (TMessageType) type, seqid);
|
||||
}
|
||||
|
||||
public override async Task ReadMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TStruct>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// some magic is here )
|
||||
|
||||
_lastField.Push(_lastFieldId);
|
||||
_lastFieldId = 0;
|
||||
|
||||
return AnonymousStruct;
|
||||
}
|
||||
|
||||
public override async Task ReadStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/*
|
||||
Doesn't actually consume any wire data, just removes the last field for
|
||||
this struct from the field stack.
|
||||
*/
|
||||
|
||||
// consume the last field we Read off the wire.
|
||||
_lastFieldId = _lastField.Pop();
|
||||
}
|
||||
|
||||
public override async Task<TField> ReadFieldBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// Read a field header off the wire.
|
||||
var type = (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
// if it's a stop, then we can return immediately, as the struct is over.
|
||||
if (type == Types.Stop)
|
||||
{
|
||||
return Tstop;
|
||||
}
|
||||
|
||||
short fieldId;
|
||||
// mask off the 4 MSB of the exType header. it could contain a field id delta.
|
||||
var modifier = (short) ((type & 0xf0) >> 4);
|
||||
if (modifier == 0)
|
||||
{
|
||||
fieldId = await ReadI16Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldId = (short) (_lastFieldId + modifier);
|
||||
}
|
||||
|
||||
var field = new TField(string.Empty, GetTType((byte) (type & 0x0f)), fieldId);
|
||||
// if this happens to be a boolean field, the value is encoded in the exType
|
||||
if (IsBoolType(type))
|
||||
{
|
||||
_boolValue = (byte) (type & 0x0f) == Types.BooleanTrue;
|
||||
}
|
||||
|
||||
// push the new field onto the field stack so we can keep the deltas going.
|
||||
_lastFieldId = field.ID;
|
||||
return field;
|
||||
}
|
||||
|
||||
public override async Task ReadFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TMap> ReadMapBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled<TMap>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/*
|
||||
Read a map header off the wire. If the size is zero, skip Reading the key
|
||||
and value exType. This means that 0-length maps will yield TMaps without the
|
||||
"correct" types.
|
||||
*/
|
||||
|
||||
var size = (int) await ReadVarInt32Async(cancellationToken).ConfigureAwait(false);
|
||||
var keyAndValueType = size == 0 ? (byte) 0 : (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new TMap(GetTType((byte) (keyAndValueType >> 4)), GetTType((byte) (keyAndValueType & 0xf)), size);
|
||||
}
|
||||
|
||||
public override async Task ReadMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TSet> ReadSetBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
/*
|
||||
Read a set header off the wire. If the set size is 0-14, the size will
|
||||
be packed into the element exType header. If it's a longer set, the 4 MSB
|
||||
of the element exType header will be 0xF, and a varint will follow with the
|
||||
true size.
|
||||
*/
|
||||
|
||||
return new TSet(await ReadListBeginAsync(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
public override async Task<bool> ReadBoolAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<bool>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/*
|
||||
Read a boolean off the wire. If this is a boolean field, the value should
|
||||
already have been Read during ReadFieldBegin, so we'll just consume the
|
||||
pre-stored value. Otherwise, Read a byte.
|
||||
*/
|
||||
|
||||
if (_boolValue != null)
|
||||
{
|
||||
var result = _boolValue.Value;
|
||||
_boolValue = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
return (await ReadByteAsync(cancellationToken).ConfigureAwait(false)) == Types.BooleanTrue;
|
||||
}
|
||||
|
||||
public override async Task<sbyte> ReadByteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<sbyte>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// Read a single byte off the wire. Nothing interesting here.
|
||||
var buf = new byte[1];
|
||||
await Trans.ReadAllAsync(buf, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
return (sbyte) buf[0];
|
||||
}
|
||||
|
||||
public override async Task<short> ReadI16Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<short>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return (short) ZigzagToInt(await ReadVarInt32Async(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
public override async Task<int> ReadI32Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<int>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return ZigzagToInt(await ReadVarInt32Async(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
public override async Task<long> ReadI64Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<long>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return ZigzagToLong(await ReadVarInt64Async(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
public override async Task<double> ReadDoubleAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<double>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var longBits = new byte[8];
|
||||
await Trans.ReadAllAsync(longBits, 0, 8, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return BitConverter.Int64BitsToDouble(BytesToLong(longBits));
|
||||
}
|
||||
|
||||
public override async Task<string> ReadStringAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled<string>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// Reads a byte[] (via ReadBinary), and then UTF-8 decodes it.
|
||||
var length = (int) await ReadVarInt32Async(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var buf = new byte[length];
|
||||
await Trans.ReadAllAsync(buf, 0, length, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return Encoding.UTF8.GetString(buf);
|
||||
}
|
||||
|
||||
public override async Task<byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<byte[]>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// Read a byte[] from the wire.
|
||||
var length = (int) await ReadVarInt32Async(cancellationToken).ConfigureAwait(false);
|
||||
if (length == 0)
|
||||
{
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
var buf = new byte[length];
|
||||
await Trans.ReadAllAsync(buf, 0, length, cancellationToken).ConfigureAwait(false);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public override async Task<TList> ReadListBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled<TList>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/*
|
||||
Read a list header off the wire. If the list size is 0-14, the size will
|
||||
be packed into the element exType header. If it's a longer list, the 4 MSB
|
||||
of the element exType header will be 0xF, and a varint will follow with the
|
||||
true size.
|
||||
*/
|
||||
|
||||
var sizeAndType = (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
var size = (sizeAndType >> 4) & 0x0f;
|
||||
if (size == 15)
|
||||
{
|
||||
size = (int) await ReadVarInt32Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var type = GetTType(sizeAndType);
|
||||
return new TList(type, size);
|
||||
}
|
||||
|
||||
public override async Task ReadListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task ReadSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte GetCompactType(TType ttype)
|
||||
{
|
||||
// Given a TType value, find the appropriate TCompactProtocol.Types constant.
|
||||
return TTypeToCompactType[(int) ttype];
|
||||
}
|
||||
|
||||
|
||||
private async Task<uint> ReadVarInt32Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<uint>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/*
|
||||
Read an i32 from the wire as a varint. The MSB of each byte is set
|
||||
if there is another byte to follow. This can Read up to 5 bytes.
|
||||
*/
|
||||
|
||||
uint result = 0;
|
||||
var shift = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var b = (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
result |= (uint) (b & 0x7f) << shift;
|
||||
if ((b & 0x80) != 0x80)
|
||||
{
|
||||
break;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<ulong> ReadVarInt64Async(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<uint>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/*
|
||||
Read an i64 from the wire as a proper varint. The MSB of each byte is set
|
||||
if there is another byte to follow. This can Read up to 10 bytes.
|
||||
*/
|
||||
|
||||
var shift = 0;
|
||||
ulong result = 0;
|
||||
while (true)
|
||||
{
|
||||
var b = (byte) await ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
result |= (ulong) (b & 0x7f) << shift;
|
||||
if ((b & 0x80) != 0x80)
|
||||
{
|
||||
break;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int ZigzagToInt(uint n)
|
||||
{
|
||||
return (int) (n >> 1) ^ -(int) (n & 1);
|
||||
}
|
||||
|
||||
private static long ZigzagToLong(ulong n)
|
||||
{
|
||||
return (long) (n >> 1) ^ -(long) (n & 1);
|
||||
}
|
||||
|
||||
private static long BytesToLong(byte[] bytes)
|
||||
{
|
||||
/*
|
||||
Note that it's important that the mask bytes are long literals,
|
||||
otherwise they'll default to ints, and when you shift an int left 56 bits,
|
||||
you just get a messed up int.
|
||||
*/
|
||||
|
||||
return
|
||||
((bytes[7] & 0xffL) << 56) |
|
||||
((bytes[6] & 0xffL) << 48) |
|
||||
((bytes[5] & 0xffL) << 40) |
|
||||
((bytes[4] & 0xffL) << 32) |
|
||||
((bytes[3] & 0xffL) << 24) |
|
||||
((bytes[2] & 0xffL) << 16) |
|
||||
((bytes[1] & 0xffL) << 8) |
|
||||
(bytes[0] & 0xffL);
|
||||
}
|
||||
|
||||
private static bool IsBoolType(byte b)
|
||||
{
|
||||
var lowerNibble = b & 0x0f;
|
||||
return (lowerNibble == Types.BooleanTrue) || (lowerNibble == Types.BooleanFalse);
|
||||
}
|
||||
|
||||
private static TType GetTType(byte type)
|
||||
{
|
||||
// Given a TCompactProtocol.Types constant, convert it to its corresponding TType value.
|
||||
switch ((byte) (type & 0x0f))
|
||||
{
|
||||
case Types.Stop:
|
||||
return TType.Stop;
|
||||
case Types.BooleanFalse:
|
||||
case Types.BooleanTrue:
|
||||
return TType.Bool;
|
||||
case Types.Byte:
|
||||
return TType.Byte;
|
||||
case Types.I16:
|
||||
return TType.I16;
|
||||
case Types.I32:
|
||||
return TType.I32;
|
||||
case Types.I64:
|
||||
return TType.I64;
|
||||
case Types.Double:
|
||||
return TType.Double;
|
||||
case Types.Binary:
|
||||
return TType.String;
|
||||
case Types.List:
|
||||
return TType.List;
|
||||
case Types.Set:
|
||||
return TType.Set;
|
||||
case Types.Map:
|
||||
return TType.Map;
|
||||
case Types.Struct:
|
||||
return TType.Struct;
|
||||
default:
|
||||
throw new TProtocolException($"Don't know what exType: {(byte) (type & 0x0f)}");
|
||||
}
|
||||
}
|
||||
|
||||
private static ulong LongToZigzag(long n)
|
||||
{
|
||||
// Convert l into a zigzag long. This allows negative numbers to be represented compactly as a varint
|
||||
return (ulong) (n << 1) ^ (ulong) (n >> 63);
|
||||
}
|
||||
|
||||
private static uint IntToZigzag(int n)
|
||||
{
|
||||
// Convert n into a zigzag int. This allows negative numbers to be represented compactly as a varint
|
||||
return (uint) (n << 1) ^ (uint) (n >> 31);
|
||||
}
|
||||
|
||||
private static void FixedLongToBytes(long n, byte[] buf, int off)
|
||||
{
|
||||
// Convert a long into little-endian bytes in buf starting at off and going until off+7.
|
||||
buf[off + 0] = (byte) (n & 0xff);
|
||||
buf[off + 1] = (byte) ((n >> 8) & 0xff);
|
||||
buf[off + 2] = (byte) ((n >> 16) & 0xff);
|
||||
buf[off + 3] = (byte) ((n >> 24) & 0xff);
|
||||
buf[off + 4] = (byte) ((n >> 32) & 0xff);
|
||||
buf[off + 5] = (byte) ((n >> 40) & 0xff);
|
||||
buf[off + 6] = (byte) ((n >> 48) & 0xff);
|
||||
buf[off + 7] = (byte) ((n >> 56) & 0xff);
|
||||
}
|
||||
|
||||
public class Factory : ITProtocolFactory
|
||||
{
|
||||
public TProtocol GetProtocol(TClientTransport trans)
|
||||
{
|
||||
return new TCompactProtocol(trans);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// All of the on-wire exType codes.
|
||||
/// </summary>
|
||||
private static class Types
|
||||
{
|
||||
public const byte Stop = 0x00;
|
||||
public const byte BooleanTrue = 0x01;
|
||||
public const byte BooleanFalse = 0x02;
|
||||
public const byte Byte = 0x03;
|
||||
public const byte I16 = 0x04;
|
||||
public const byte I32 = 0x05;
|
||||
public const byte I64 = 0x06;
|
||||
public const byte Double = 0x07;
|
||||
public const byte Binary = 0x08;
|
||||
public const byte List = 0x09;
|
||||
public const byte Set = 0x0A;
|
||||
public const byte Map = 0x0B;
|
||||
public const byte Struct = 0x0C;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,981 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocols.Utilities;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON protocol implementation for thrift.
|
||||
/// This is a full-featured protocol supporting Write and Read.
|
||||
/// Please see the C++ class header for a detailed description of the
|
||||
/// protocol's wire format.
|
||||
/// Adapted from the Java version.
|
||||
/// </summary>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TJsonProtocol : TProtocol
|
||||
{
|
||||
private const long Version = 1;
|
||||
|
||||
// Temporary buffer used by several methods
|
||||
private readonly byte[] _tempBuffer = new byte[4];
|
||||
|
||||
// Current context that we are in
|
||||
protected JSONBaseContext Context;
|
||||
|
||||
// Stack of nested contexts that we may be in
|
||||
protected Stack<JSONBaseContext> ContextStack = new Stack<JSONBaseContext>();
|
||||
|
||||
// Reader that manages a 1-byte buffer
|
||||
protected LookaheadReader Reader;
|
||||
|
||||
// Default encoding
|
||||
protected Encoding Utf8Encoding = Encoding.UTF8;
|
||||
|
||||
/// <summary>
|
||||
/// TJsonProtocol Constructor
|
||||
/// </summary>
|
||||
public TJsonProtocol(TClientTransport trans)
|
||||
: base(trans)
|
||||
{
|
||||
Context = new JSONBaseContext(this);
|
||||
Reader = new LookaheadReader(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Push a new JSON context onto the stack.
|
||||
/// </summary>
|
||||
protected void PushContext(JSONBaseContext c)
|
||||
{
|
||||
ContextStack.Push(Context);
|
||||
Context = c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pop the last JSON context off the stack
|
||||
/// </summary>
|
||||
protected void PopContext()
|
||||
{
|
||||
Context = ContextStack.Pop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a byte that must match b[0]; otherwise an exception is thrown.
|
||||
/// Marked protected to avoid synthetic accessor in JSONListContext.Read
|
||||
/// and JSONPairContext.Read
|
||||
/// </summary>
|
||||
protected async Task ReadJsonSyntaxCharAsync(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
var ch = await Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (ch != bytes[0])
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, $"Unexpected character: {(char) ch}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write the bytes in array buf as a JSON characters, escaping as needed
|
||||
/// </summary>
|
||||
private async Task WriteJsonStringAsync(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var len = bytes.Length;
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
if ((bytes[i] & 0x00FF) >= 0x30)
|
||||
{
|
||||
if (bytes[i] == TJSONProtocolConstants.Backslash[0])
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Backslash, cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Backslash, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Trans.WriteAsync(bytes.ToArray(), i, 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_tempBuffer[0] = TJSONProtocolConstants.JsonCharTable[bytes[i]];
|
||||
if (_tempBuffer[0] == 1)
|
||||
{
|
||||
await Trans.WriteAsync(bytes, i, 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else if (_tempBuffer[0] > 1)
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Backslash, cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(_tempBuffer, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.EscSequences, cancellationToken).ConfigureAwait(false);
|
||||
_tempBuffer[0] = TJSONProtocolHelper.ToHexChar((byte) (bytes[i] >> 4));
|
||||
_tempBuffer[1] = TJSONProtocolHelper.ToHexChar(bytes[i]);
|
||||
await Trans.WriteAsync(_tempBuffer, 0, 2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out number as a JSON value. If the context dictates so, it will be
|
||||
/// wrapped in quotes to output as a JSON string.
|
||||
/// </summary>
|
||||
private async Task WriteJsonIntegerAsync(long num, CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
var str = num.ToString();
|
||||
|
||||
var escapeNum = Context.EscapeNumbers();
|
||||
if (escapeNum)
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var bytes = Utf8Encoding.GetBytes(str);
|
||||
await Trans.WriteAsync(bytes, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (escapeNum)
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out a double as a JSON value. If it is NaN or infinity or if the
|
||||
/// context dictates escaping, Write out as JSON string.
|
||||
/// </summary>
|
||||
private async Task WriteJsonDoubleAsync(double num, CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
var str = num.ToString("G17", CultureInfo.InvariantCulture);
|
||||
var special = false;
|
||||
|
||||
switch (str[0])
|
||||
{
|
||||
case 'N': // NaN
|
||||
case 'I': // Infinity
|
||||
special = true;
|
||||
break;
|
||||
case '-':
|
||||
if (str[1] == 'I')
|
||||
{
|
||||
// -Infinity
|
||||
special = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
var escapeNum = special || Context.EscapeNumbers();
|
||||
|
||||
if (escapeNum)
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Trans.WriteAsync(Utf8Encoding.GetBytes(str), cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (escapeNum)
|
||||
{
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out contents of byte array b as a JSON string with base-64 encoded
|
||||
/// data
|
||||
/// </summary>
|
||||
private async Task WriteJsonBase64Async(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var len = bytes.Length;
|
||||
var off = 0;
|
||||
|
||||
while (len >= 3)
|
||||
{
|
||||
// Encode 3 bytes at a time
|
||||
TBase64Helper.Encode(bytes, off, 3, _tempBuffer, 0);
|
||||
await Trans.WriteAsync(_tempBuffer, 0, 4, cancellationToken).ConfigureAwait(false);
|
||||
off += 3;
|
||||
len -= 3;
|
||||
}
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
// Encode remainder
|
||||
TBase64Helper.Encode(bytes, off, len, _tempBuffer, 0);
|
||||
await Trans.WriteAsync(_tempBuffer, 0, len + 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task WriteJsonObjectStartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.LeftBrace, cancellationToken).ConfigureAwait(false);
|
||||
PushContext(new JSONPairContext(this));
|
||||
}
|
||||
|
||||
private async Task WriteJsonObjectEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
PopContext();
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.RightBrace, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task WriteJsonArrayStartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.LeftBracket, cancellationToken).ConfigureAwait(false);
|
||||
PushContext(new JSONListContext(this));
|
||||
}
|
||||
|
||||
private async Task WriteJsonArrayEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
PopContext();
|
||||
await Trans.WriteAsync(TJSONProtocolConstants.RightBracket, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonIntegerAsync(Version, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var b = Utf8Encoding.GetBytes(message.Name);
|
||||
await WriteJsonStringAsync(b, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await WriteJsonIntegerAsync((long) message.Type, cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonIntegerAsync(message.SeqID, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonObjectStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonObjectEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonIntegerAsync(field.ID, cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonObjectStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(field.Type), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonObjectEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldStopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(map.KeyType), cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(map.ValueType), cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonIntegerAsync(map.Count, cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonObjectStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonObjectEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(list.ElementType), cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonIntegerAsync(list.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(set.ElementType), cancellationToken).ConfigureAwait(false);
|
||||
await WriteJsonIntegerAsync(set.Count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonIntegerAsync(b ? 1 : 0, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonIntegerAsync(b, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI16Async(short i16, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonIntegerAsync(i16, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI32Async(int i32, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonIntegerAsync(i32, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI64Async(long i64, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonIntegerAsync(i64, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonDoubleAsync(d, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStringAsync(string s, CancellationToken cancellationToken)
|
||||
{
|
||||
var b = Utf8Encoding.GetBytes(s);
|
||||
await WriteJsonStringAsync(b, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteJsonBase64Async(bytes, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read in a JSON string, unescaping as appropriate.. Skip Reading from the
|
||||
/// context if skipContext is true.
|
||||
/// </summary>
|
||||
private async Task<byte[]> ReadJsonStringAsync(bool skipContext, CancellationToken cancellationToken)
|
||||
{
|
||||
using (var buffer = new MemoryStream())
|
||||
{
|
||||
var codeunits = new List<char>();
|
||||
|
||||
|
||||
if (!skipContext)
|
||||
{
|
||||
await Context.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
while (true)
|
||||
{
|
||||
var ch = await Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (ch == TJSONProtocolConstants.Quote[0])
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// escaped?
|
||||
if (ch != TJSONProtocolConstants.EscSequences[0])
|
||||
{
|
||||
await buffer.WriteAsync(new[] {ch}, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
// distinguish between \uXXXX and \?
|
||||
ch = await Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (ch != TJSONProtocolConstants.EscSequences[1]) // control chars like \n
|
||||
{
|
||||
var off = Array.IndexOf(TJSONProtocolConstants.EscapeChars, (char) ch);
|
||||
if (off == -1)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected control char");
|
||||
}
|
||||
ch = TJSONProtocolConstants.EscapeCharValues[off];
|
||||
await buffer.WriteAsync(new[] {ch}, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
// it's \uXXXX
|
||||
await Trans.ReadAllAsync(_tempBuffer, 0, 4, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var wch = (short) ((TJSONProtocolHelper.ToHexVal(_tempBuffer[0]) << 12) +
|
||||
(TJSONProtocolHelper.ToHexVal(_tempBuffer[1]) << 8) +
|
||||
(TJSONProtocolHelper.ToHexVal(_tempBuffer[2]) << 4) +
|
||||
TJSONProtocolHelper.ToHexVal(_tempBuffer[3]));
|
||||
|
||||
if (char.IsHighSurrogate((char) wch))
|
||||
{
|
||||
if (codeunits.Count > 0)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected low surrogate char");
|
||||
}
|
||||
codeunits.Add((char) wch);
|
||||
}
|
||||
else if (char.IsLowSurrogate((char) wch))
|
||||
{
|
||||
if (codeunits.Count == 0)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected high surrogate char");
|
||||
}
|
||||
|
||||
codeunits.Add((char) wch);
|
||||
var tmp = Utf8Encoding.GetBytes(codeunits.ToArray());
|
||||
await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken).ConfigureAwait(false);
|
||||
codeunits.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
var tmp = Utf8Encoding.GetBytes(new[] {(char) wch});
|
||||
await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (codeunits.Count > 0)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected low surrogate char");
|
||||
}
|
||||
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read in a sequence of characters that are all valid in JSON numbers. Does
|
||||
/// not do a complete regex check to validate that this is actually a number.
|
||||
/// </summary>
|
||||
private async Task<string> ReadJsonNumericCharsAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var strbld = new StringBuilder();
|
||||
while (true)
|
||||
{
|
||||
//TODO: workaround for primitive types with TJsonProtocol, think - how to rewrite into more easy form without exceptions
|
||||
try
|
||||
{
|
||||
var ch = await Reader.PeekAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (!TJSONProtocolHelper.IsJsonNumeric(ch))
|
||||
{
|
||||
break;
|
||||
}
|
||||
var c = (char)await Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
strbld.Append(c);
|
||||
}
|
||||
catch (TTransportException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return strbld.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read in a JSON number. If the context dictates, Read in enclosing quotes.
|
||||
/// </summary>
|
||||
private async Task<long> ReadJsonIntegerAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (Context.EscapeNumbers())
|
||||
{
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var str = await ReadJsonNumericCharsAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (Context.EscapeNumbers())
|
||||
{
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return long.Parse(str);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Bad data encounted in numeric data");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read in a JSON double value. Throw if the value is not wrapped in quotes
|
||||
/// when expected or if wrapped in quotes when not expected.
|
||||
/// </summary>
|
||||
private async Task<double> ReadJsonDoubleAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (await Reader.PeekAsync(cancellationToken) == TJSONProtocolConstants.Quote[0])
|
||||
{
|
||||
var arr = await ReadJsonStringAsync(true, cancellationToken).ConfigureAwait(false);
|
||||
var dub = double.Parse(Utf8Encoding.GetString(arr, 0, arr.Length), CultureInfo.InvariantCulture);
|
||||
|
||||
if (!Context.EscapeNumbers() && !double.IsNaN(dub) && !double.IsInfinity(dub))
|
||||
{
|
||||
// Throw exception -- we should not be in a string in this case
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Numeric data unexpectedly quoted");
|
||||
}
|
||||
|
||||
return dub;
|
||||
}
|
||||
|
||||
if (Context.EscapeNumbers())
|
||||
{
|
||||
// This will throw - we should have had a quote if escapeNum == true
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Quote, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return double.Parse(await ReadJsonNumericCharsAsync(cancellationToken).ConfigureAwait(false), CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Bad data encounted in numeric data");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read in a JSON string containing base-64 encoded data and decode it.
|
||||
/// </summary>
|
||||
private async Task<byte[]> ReadJsonBase64Async(CancellationToken cancellationToken)
|
||||
{
|
||||
var b = await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false);
|
||||
var len = b.Length;
|
||||
var off = 0;
|
||||
var size = 0;
|
||||
|
||||
// reduce len to ignore fill bytes
|
||||
while ((len > 0) && (b[len - 1] == '='))
|
||||
{
|
||||
--len;
|
||||
}
|
||||
|
||||
// read & decode full byte triplets = 4 source bytes
|
||||
while (len > 4)
|
||||
{
|
||||
// Decode 4 bytes at a time
|
||||
TBase64Helper.Decode(b, off, 4, b, size); // NB: decoded in place
|
||||
off += 4;
|
||||
len -= 4;
|
||||
size += 3;
|
||||
}
|
||||
|
||||
// Don't decode if we hit the end or got a single leftover byte (invalid
|
||||
// base64 but legal for skip of regular string exType)
|
||||
if (len > 1)
|
||||
{
|
||||
// Decode remainder
|
||||
TBase64Helper.Decode(b, off, len, b, size); // NB: decoded in place
|
||||
size += len - 1;
|
||||
}
|
||||
|
||||
// Sadly we must copy the byte[] (any way around this?)
|
||||
var result = new byte[size];
|
||||
Array.Copy(b, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task ReadJsonObjectStartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.LeftBrace, cancellationToken).ConfigureAwait(false);
|
||||
PushContext(new JSONPairContext(this));
|
||||
}
|
||||
|
||||
private async Task ReadJsonObjectEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.RightBrace, cancellationToken).ConfigureAwait(false);
|
||||
PopContext();
|
||||
}
|
||||
|
||||
private async Task ReadJsonArrayStartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Context.ReadAsync(cancellationToken).ConfigureAwait(false);
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.LeftBracket, cancellationToken).ConfigureAwait(false);
|
||||
PushContext(new JSONListContext(this));
|
||||
}
|
||||
|
||||
private async Task ReadJsonArrayEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.RightBracket, cancellationToken).ConfigureAwait(false);
|
||||
PopContext();
|
||||
}
|
||||
|
||||
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var message = new TMessage();
|
||||
await ReadJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (await ReadJsonIntegerAsync(cancellationToken) != Version)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.BAD_VERSION, "Message contained bad version.");
|
||||
}
|
||||
|
||||
var buf = await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false);
|
||||
message.Name = Utf8Encoding.GetString(buf, 0, buf.Length);
|
||||
message.Type = (TMessageType) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
message.SeqID = (int) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
return message;
|
||||
}
|
||||
|
||||
public override async Task ReadMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonObjectStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new TStruct();
|
||||
}
|
||||
|
||||
public override async Task ReadStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonObjectEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TField> ReadFieldBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var field = new TField();
|
||||
var ch = await Reader.PeekAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (ch == TJSONProtocolConstants.RightBrace[0])
|
||||
{
|
||||
field.Type = TType.Stop;
|
||||
}
|
||||
else
|
||||
{
|
||||
field.ID = (short) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
await ReadJsonObjectStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
field.Type = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
public override async Task ReadFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonObjectEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TMap> ReadMapBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var map = new TMap();
|
||||
await ReadJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
map.KeyType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false));
|
||||
map.ValueType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false));
|
||||
map.Count = (int) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
await ReadJsonObjectStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
return map;
|
||||
}
|
||||
|
||||
public override async Task ReadMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonObjectEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
await ReadJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TList> ReadListBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var list = new TList();
|
||||
await ReadJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
list.ElementType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false));
|
||||
list.Count = (int) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
return list;
|
||||
}
|
||||
|
||||
public override async Task ReadListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TSet> ReadSetBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var set = new TSet();
|
||||
await ReadJsonArrayStartAsync(cancellationToken).ConfigureAwait(false);
|
||||
set.ElementType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false));
|
||||
set.Count = (int) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
return set;
|
||||
}
|
||||
|
||||
public override async Task ReadSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ReadJsonArrayEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<bool> ReadBoolAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return (await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false)) != 0;
|
||||
}
|
||||
|
||||
public override async Task<sbyte> ReadByteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return (sbyte) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<short> ReadI16Async(CancellationToken cancellationToken)
|
||||
{
|
||||
return (short) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<int> ReadI32Async(CancellationToken cancellationToken)
|
||||
{
|
||||
return (int) await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<long> ReadI64Async(CancellationToken cancellationToken)
|
||||
{
|
||||
return await ReadJsonIntegerAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<double> ReadDoubleAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await ReadJsonDoubleAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<string> ReadStringAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var buf = await ReadJsonStringAsync(false, cancellationToken).ConfigureAwait(false);
|
||||
return Utf8Encoding.GetString(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public override async Task<byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await ReadJsonBase64Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Factory for JSON protocol objects
|
||||
/// </summary>
|
||||
public class Factory : ITProtocolFactory
|
||||
{
|
||||
public TProtocol GetProtocol(TClientTransport trans)
|
||||
{
|
||||
return new TJsonProtocol(trans);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base class for tracking JSON contexts that may require
|
||||
/// inserting/Reading additional JSON syntax characters
|
||||
/// This base context does nothing.
|
||||
/// </summary>
|
||||
protected class JSONBaseContext
|
||||
{
|
||||
protected TJsonProtocol Proto;
|
||||
|
||||
public JSONBaseContext(TJsonProtocol proto)
|
||||
{
|
||||
Proto = proto;
|
||||
}
|
||||
|
||||
public virtual async Task WriteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task ReadAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool EscapeNumbers()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Context for JSON lists. Will insert/Read commas before each item except
|
||||
/// for the first one
|
||||
/// </summary>
|
||||
protected class JSONListContext : JSONBaseContext
|
||||
{
|
||||
private bool _first = true;
|
||||
|
||||
public JSONListContext(TJsonProtocol protocol)
|
||||
: base(protocol)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_first)
|
||||
{
|
||||
_first = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
await Proto.Trans.WriteAsync(TJSONProtocolConstants.Comma, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task ReadAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_first)
|
||||
{
|
||||
_first = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
await Proto.ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Comma, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Context for JSON records. Will insert/Read colons before the value portion
|
||||
/// of each record pair, and commas before each key except the first. In
|
||||
/// addition, will indicate that numbers in the key position need to be
|
||||
/// escaped in quotes (since JSON keys must be strings).
|
||||
/// </summary>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
protected class JSONPairContext : JSONBaseContext
|
||||
{
|
||||
private bool _colon = true;
|
||||
|
||||
private bool _first = true;
|
||||
|
||||
public JSONPairContext(TJsonProtocol proto)
|
||||
: base(proto)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_first)
|
||||
{
|
||||
_first = false;
|
||||
_colon = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
await Proto.Trans.WriteAsync(_colon ? TJSONProtocolConstants.Colon : TJSONProtocolConstants.Comma, cancellationToken).ConfigureAwait(false);
|
||||
_colon = !_colon;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task ReadAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_first)
|
||||
{
|
||||
_first = false;
|
||||
_colon = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
await Proto.ReadJsonSyntaxCharAsync(_colon ? TJSONProtocolConstants.Colon : TJSONProtocolConstants.Comma, cancellationToken).ConfigureAwait(false);
|
||||
_colon = !_colon;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool EscapeNumbers()
|
||||
{
|
||||
return _colon;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Holds up to one byte from the transport
|
||||
/// </summary>
|
||||
protected class LookaheadReader
|
||||
{
|
||||
private readonly byte[] _data = new byte[1];
|
||||
|
||||
private bool _hasData;
|
||||
protected TJsonProtocol Proto;
|
||||
|
||||
public LookaheadReader(TJsonProtocol proto)
|
||||
{
|
||||
Proto = proto;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return and consume the next byte to be Read, either taking it from the
|
||||
/// data buffer if present or getting it from the transport otherwise.
|
||||
/// </summary>
|
||||
public async Task<byte> ReadAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<byte>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (_hasData)
|
||||
{
|
||||
_hasData = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// find more easy way to avoid exception on reading primitive types
|
||||
await Proto.Trans.ReadAllAsync(_data, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
return _data[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the next byte to be Read without consuming, filling the data
|
||||
/// buffer if it has not been filled alReady.
|
||||
/// </summary>
|
||||
public async Task<byte> PeekAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<byte>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (!_hasData)
|
||||
{
|
||||
// find more easy way to avoid exception on reading primitive types
|
||||
await Proto.Trans.ReadAllAsync(_data, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
_hasData = true;
|
||||
return _data[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
/**
|
||||
* TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift
|
||||
* client to communicate with a multiplexing Thrift server, by prepending the service name
|
||||
* to the function name during function calls.
|
||||
*
|
||||
* NOTE: THIS IS NOT TO BE USED BY SERVERS.
|
||||
* On the server, use TMultiplexedProcessor to handle requests from a multiplexing client.
|
||||
*
|
||||
* This example uses a single socket transport to invoke two services:
|
||||
*
|
||||
* TSocketClientTransport transport = new TSocketClientTransport("localhost", 9090);
|
||||
* transport.open();
|
||||
*
|
||||
* TBinaryProtocol protocol = new TBinaryProtocol(transport);
|
||||
*
|
||||
* TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator");
|
||||
* Calculator.Client service = new Calculator.Client(mp);
|
||||
*
|
||||
* TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport");
|
||||
* WeatherReport.Client service2 = new WeatherReport.Client(mp2);
|
||||
*
|
||||
* System.out.println(service.add(2,2));
|
||||
* System.out.println(service2.getTemperature());
|
||||
*
|
||||
*/
|
||||
|
||||
//TODO: implementation of TProtocol
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TMultiplexedProtocol : TProtocolDecorator
|
||||
{
|
||||
/** Used to delimit the service name from the function name */
|
||||
public const string Separator = ":";
|
||||
|
||||
private readonly string _serviceName;
|
||||
|
||||
/**
|
||||
* Wrap the specified protocol, allowing it to be used to communicate with a
|
||||
* multiplexing server. The <code>serviceName</code> is required as it is
|
||||
* prepended to the message header so that the multiplexing server can broker
|
||||
* the function call to the proper service.
|
||||
*
|
||||
* Args:
|
||||
* protocol Your communication protocol of choice, e.g. TBinaryProtocol
|
||||
* serviceName The service name of the service communicating via this protocol.
|
||||
*/
|
||||
|
||||
public TMultiplexedProtocol(TProtocol protocol, string serviceName)
|
||||
: base(protocol)
|
||||
{
|
||||
_serviceName = serviceName;
|
||||
}
|
||||
|
||||
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
switch (message.Type)
|
||||
{
|
||||
case TMessageType.Call:
|
||||
case TMessageType.Oneway:
|
||||
await base.WriteMessageBeginAsync(new TMessage($"{_serviceName}{Separator}{message.Name}", message.Type, message.SeqID), cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
default:
|
||||
await base.WriteMessageBeginAsync(message, cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,376 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public abstract class TProtocol : IDisposable
|
||||
{
|
||||
public const int DefaultRecursionDepth = 64;
|
||||
private bool _isDisposed;
|
||||
protected int RecursionDepth;
|
||||
|
||||
protected TClientTransport Trans;
|
||||
|
||||
protected TProtocol(TClientTransport trans)
|
||||
{
|
||||
Trans = trans;
|
||||
RecursionLimit = DefaultRecursionDepth;
|
||||
RecursionDepth = 0;
|
||||
}
|
||||
|
||||
public TClientTransport Transport => Trans;
|
||||
|
||||
protected int RecursionLimit { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
public void IncrementRecursionDepth()
|
||||
{
|
||||
if (RecursionDepth < RecursionLimit)
|
||||
{
|
||||
++RecursionDepth;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
public void DecrementRecursionDepth()
|
||||
{
|
||||
--RecursionDepth;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
(Trans as IDisposable)?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
public virtual async Task WriteMessageBeginAsync(TMessage message)
|
||||
{
|
||||
await WriteMessageBeginAsync(message, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteMessageEndAsync()
|
||||
{
|
||||
await WriteMessageEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteStructBeginAsync(TStruct @struct)
|
||||
{
|
||||
await WriteStructBeginAsync(@struct, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteStructEndAsync()
|
||||
{
|
||||
await WriteStructEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteStructEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteFieldBeginAsync(TField field)
|
||||
{
|
||||
await WriteFieldBeginAsync(field, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteFieldEndAsync()
|
||||
{
|
||||
await WriteFieldEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteFieldStopAsync()
|
||||
{
|
||||
await WriteFieldStopAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteMapBeginAsync(TMap map)
|
||||
{
|
||||
await WriteMapBeginAsync(map, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteMapEndAsync()
|
||||
{
|
||||
await WriteMapEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteMapEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteListBeginAsync(TList list)
|
||||
{
|
||||
await WriteListBeginAsync(list, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteListEndAsync()
|
||||
{
|
||||
await WriteListEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteListEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteSetBeginAsync(TSet set)
|
||||
{
|
||||
await WriteSetBeginAsync(set, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteSetEndAsync()
|
||||
{
|
||||
await WriteSetEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteSetEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteBoolAsync(bool b)
|
||||
{
|
||||
await WriteBoolAsync(b, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteByteAsync(sbyte b)
|
||||
{
|
||||
await WriteByteAsync(b, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteI16Async(short i16)
|
||||
{
|
||||
await WriteI16Async(i16, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteI32Async(int i32)
|
||||
{
|
||||
await WriteI32Async(i32, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteI64Async(long i64)
|
||||
{
|
||||
await WriteI64Async(i64, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteDoubleAsync(double d)
|
||||
{
|
||||
await WriteDoubleAsync(d, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task WriteStringAsync(string s)
|
||||
{
|
||||
await WriteStringAsync(s, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(s);
|
||||
await WriteBinaryAsync(bytes, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task WriteBinaryAsync(byte[] bytes)
|
||||
{
|
||||
await WriteBinaryAsync(bytes, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<TMessage> ReadMessageBeginAsync()
|
||||
{
|
||||
return await ReadMessageBeginAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task ReadMessageEndAsync()
|
||||
{
|
||||
await ReadMessageEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<TStruct> ReadStructBeginAsync()
|
||||
{
|
||||
return await ReadStructBeginAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task ReadStructEndAsync()
|
||||
{
|
||||
await ReadStructEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task ReadStructEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<TField> ReadFieldBeginAsync()
|
||||
{
|
||||
return await ReadFieldBeginAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<TField> ReadFieldBeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task ReadFieldEndAsync()
|
||||
{
|
||||
await ReadFieldEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<TMap> ReadMapBeginAsync()
|
||||
{
|
||||
return await ReadMapBeginAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<TMap> ReadMapBeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task ReadMapEndAsync()
|
||||
{
|
||||
await ReadMapEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task ReadMapEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<TList> ReadListBeginAsync()
|
||||
{
|
||||
return await ReadListBeginAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<TList> ReadListBeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task ReadListEndAsync()
|
||||
{
|
||||
await ReadListEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task ReadListEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<TSet> ReadSetBeginAsync()
|
||||
{
|
||||
return await ReadSetBeginAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<TSet> ReadSetBeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task ReadSetEndAsync()
|
||||
{
|
||||
await ReadSetEndAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task ReadSetEndAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<bool> ReadBoolAsync()
|
||||
{
|
||||
return await ReadBoolAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<bool> ReadBoolAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<sbyte> ReadByteAsync()
|
||||
{
|
||||
return await ReadByteAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<sbyte> ReadByteAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<short> ReadI16Async()
|
||||
{
|
||||
return await ReadI16Async(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<short> ReadI16Async(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<int> ReadI32Async()
|
||||
{
|
||||
return await ReadI32Async(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<int> ReadI32Async(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<long> ReadI64Async()
|
||||
{
|
||||
return await ReadI64Async(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<long> ReadI64Async(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<double> ReadDoubleAsync()
|
||||
{
|
||||
return await ReadDoubleAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<double> ReadDoubleAsync(CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<string> ReadStringAsync()
|
||||
{
|
||||
return await ReadStringAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task<string> ReadStringAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var buf = await ReadBinaryAsync(cancellationToken).ConfigureAwait(false);
|
||||
return Encoding.UTF8.GetString(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public virtual async Task<byte[]> ReadBinaryAsync()
|
||||
{
|
||||
return await ReadBinaryAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<byte[]> ReadBinaryAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,247 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
/// <summary>
|
||||
/// TProtocolDecorator forwards all requests to an enclosed TProtocol instance,
|
||||
/// providing a way to author concise concrete decorator subclasses.While it has
|
||||
/// no abstract methods, it is marked abstract as a reminder that by itself,
|
||||
/// it does not modify the behaviour of the enclosed TProtocol.
|
||||
/// </summary>
|
||||
public abstract class TProtocolDecorator : TProtocol
|
||||
{
|
||||
private readonly TProtocol _wrappedProtocol;
|
||||
|
||||
protected TProtocolDecorator(TProtocol protocol)
|
||||
: base(protocol.Transport)
|
||||
{
|
||||
_wrappedProtocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
|
||||
}
|
||||
|
||||
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteMessageBeginAsync(message, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteMessageEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteStructBeginAsync(@struct, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteStructEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteFieldBeginAsync(field, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteFieldEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteFieldStopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteFieldStopAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteMapBeginAsync(map, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteMapEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteListBeginAsync(list, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteListEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteSetBeginAsync(set, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteSetEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteBoolAsync(b, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteByteAsync(b, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI16Async(short i16, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteI16Async(i16, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI32Async(int i32, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteI32Async(i32, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteI64Async(long i64, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteI64Async(i64, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteDoubleAsync(d, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteStringAsync(string s, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteStringAsync(s, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.WriteBinaryAsync(bytes, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadMessageBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task ReadMessageEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.ReadMessageEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadStructBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task ReadStructEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.ReadStructEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TField> ReadFieldBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadFieldBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task ReadFieldEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.ReadFieldEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TMap> ReadMapBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadMapBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task ReadMapEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.ReadMapEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TList> ReadListBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadListBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task ReadListEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.ReadListEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<TSet> ReadSetBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadSetBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task ReadSetEndAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _wrappedProtocol.ReadSetEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<bool> ReadBoolAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadBoolAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<sbyte> ReadByteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<short> ReadI16Async(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadI16Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<int> ReadI32Async(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<long> ReadI64Async(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadI64Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<double> ReadDoubleAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadDoubleAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<string> ReadStringAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task<byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _wrappedProtocol.ReadBinaryAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
namespace Thrift.Protocols
|
||||
{
|
||||
public class TProtocolException : TException
|
||||
{
|
||||
// do not rename public constants - they used in generated files
|
||||
public const int UNKNOWN = 0;
|
||||
public const int INVALID_DATA = 1;
|
||||
public const int NEGATIVE_SIZE = 2;
|
||||
public const int SIZE_LIMIT = 3;
|
||||
public const int BAD_VERSION = 4;
|
||||
public const int NOT_IMPLEMENTED = 5;
|
||||
public const int DEPTH_LIMIT = 6;
|
||||
|
||||
protected int Type = UNKNOWN;
|
||||
|
||||
public TProtocolException()
|
||||
{
|
||||
}
|
||||
|
||||
public TProtocolException(int type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public TProtocolException(int type, string message)
|
||||
: base(message)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public TProtocolException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public int GetExceptionType()
|
||||
{
|
||||
return Type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,101 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Thrift.Protocols.Utilities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
internal static class TBase64Helper
|
||||
{
|
||||
//TODO: Constants
|
||||
//TODO: Check for args
|
||||
//TODO: Unitests
|
||||
|
||||
internal const string EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
private static readonly int[] DecodeTable =
|
||||
{
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
|
||||
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
|
||||
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
||||
};
|
||||
|
||||
internal static void Encode(byte[] src, int srcOff, int len, byte[] dst, int dstOff)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(src));
|
||||
}
|
||||
|
||||
dst[dstOff] = (byte) EncodeTable[(src[srcOff] >> 2) & 0x3F];
|
||||
|
||||
if (len == 3)
|
||||
{
|
||||
dst[dstOff + 1] = (byte) EncodeTable[((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
|
||||
dst[dstOff + 2] = (byte) EncodeTable[((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)];
|
||||
dst[dstOff + 3] = (byte) EncodeTable[src[srcOff + 2] & 0x3F];
|
||||
}
|
||||
else if (len == 2)
|
||||
{
|
||||
dst[dstOff + 1] = (byte) EncodeTable[((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
|
||||
dst[dstOff + 2] = (byte) EncodeTable[(src[srcOff + 1] << 2) & 0x3C];
|
||||
}
|
||||
else
|
||||
{
|
||||
// len == 1
|
||||
dst[dstOff + 1] = (byte) EncodeTable[(src[srcOff] << 4) & 0x30];
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Decode(byte[] src, int srcOff, int len, byte[] dst, int dstOff)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(src));
|
||||
}
|
||||
|
||||
dst[dstOff] = (byte) ((DecodeTable[src[srcOff] & 0x0FF] << 2) | (DecodeTable[src[srcOff + 1] & 0x0FF] >> 4));
|
||||
|
||||
if (len > 2)
|
||||
{
|
||||
dst[dstOff + 1] =
|
||||
(byte)
|
||||
(((DecodeTable[src[srcOff + 1] & 0x0FF] << 4) & 0xF0) | (DecodeTable[src[srcOff + 2] & 0x0FF] >> 2));
|
||||
if (len > 3)
|
||||
{
|
||||
dst[dstOff + 2] =
|
||||
(byte)
|
||||
(((DecodeTable[src[srcOff + 2] & 0x0FF] << 6) & 0xC0) | DecodeTable[src[srcOff + 3] & 0x0FF]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,101 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Thrift.Protocols.Utilities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
internal static class TBase64Utils
|
||||
{
|
||||
//TODO: Constants
|
||||
//TODO: Check for args
|
||||
//TODO: Unitests
|
||||
|
||||
internal const string EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
private static readonly int[] DecodeTable =
|
||||
{
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
|
||||
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
|
||||
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
||||
};
|
||||
|
||||
internal static void Encode(byte[] src, int srcOff, int len, byte[] dst, int dstOff)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(src));
|
||||
}
|
||||
|
||||
dst[dstOff] = (byte) EncodeTable[(src[srcOff] >> 2) & 0x3F];
|
||||
|
||||
if (len == 3)
|
||||
{
|
||||
dst[dstOff + 1] = (byte) EncodeTable[((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
|
||||
dst[dstOff + 2] = (byte) EncodeTable[((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)];
|
||||
dst[dstOff + 3] = (byte) EncodeTable[src[srcOff + 2] & 0x3F];
|
||||
}
|
||||
else if (len == 2)
|
||||
{
|
||||
dst[dstOff + 1] = (byte) EncodeTable[((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
|
||||
dst[dstOff + 2] = (byte) EncodeTable[(src[srcOff + 1] << 2) & 0x3C];
|
||||
}
|
||||
else
|
||||
{
|
||||
// len == 1
|
||||
dst[dstOff + 1] = (byte) EncodeTable[(src[srcOff] << 4) & 0x30];
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Decode(byte[] src, int srcOff, int len, byte[] dst, int dstOff)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(src));
|
||||
}
|
||||
|
||||
dst[dstOff] = (byte) ((DecodeTable[src[srcOff] & 0x0FF] << 2) | (DecodeTable[src[srcOff + 1] & 0x0FF] >> 4));
|
||||
|
||||
if (len > 2)
|
||||
{
|
||||
dst[dstOff + 1] =
|
||||
(byte)
|
||||
(((DecodeTable[src[srcOff + 1] & 0x0FF] << 4) & 0xF0) | (DecodeTable[src[srcOff + 2] & 0x0FF] >> 2));
|
||||
if (len > 3)
|
||||
{
|
||||
dst[dstOff + 2] =
|
||||
(byte)
|
||||
(((DecodeTable[src[srcOff + 2] & 0x0FF] << 6) & 0xC0) | DecodeTable[src[srcOff + 3] & 0x0FF]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Protocols.Utilities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class TJSONProtocolConstants
|
||||
{
|
||||
//TODO Check for performance for reusing ImmutableArray from System.Collections.Immutable (https://blogs.msdn.microsoft.com/dotnet/2013/06/24/please-welcome-immutablearrayt/)
|
||||
// can be possible to get better performance and also better GC
|
||||
|
||||
public static readonly byte[] Comma = {(byte) ','};
|
||||
public static readonly byte[] Colon = {(byte) ':'};
|
||||
public static readonly byte[] LeftBrace = {(byte) '{'};
|
||||
public static readonly byte[] RightBrace = {(byte) '}'};
|
||||
public static readonly byte[] LeftBracket = {(byte) '['};
|
||||
public static readonly byte[] RightBracket = {(byte) ']'};
|
||||
public static readonly byte[] Quote = {(byte) '"'};
|
||||
public static readonly byte[] Backslash = {(byte) '\\'};
|
||||
|
||||
public static readonly byte[] JsonCharTable =
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, (byte) 'b', (byte) 't', (byte) 'n', 0, (byte) 'f', (byte) 'r', 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 1, (byte) '"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
|
||||
};
|
||||
|
||||
public static readonly char[] EscapeChars = "\"\\/bfnrt".ToCharArray();
|
||||
public static readonly byte[] EscapeCharValues = {(byte) '"', (byte) '\\', (byte) '/', (byte) '\b', (byte) '\f', (byte) '\n', (byte) '\r', (byte) '\t'};
|
||||
public static readonly byte[] EscSequences = {(byte) '\\', (byte) 'u', (byte) '0', (byte) '0'};
|
||||
|
||||
public static class TypeNames
|
||||
{
|
||||
public static readonly byte[] NameBool = { (byte)'t', (byte)'f' };
|
||||
public static readonly byte[] NameByte = { (byte)'i', (byte)'8' };
|
||||
public static readonly byte[] NameI16 = { (byte)'i', (byte)'1', (byte)'6' };
|
||||
public static readonly byte[] NameI32 = { (byte)'i', (byte)'3', (byte)'2' };
|
||||
public static readonly byte[] NameI64 = { (byte)'i', (byte)'6', (byte)'4' };
|
||||
public static readonly byte[] NameDouble = { (byte)'d', (byte)'b', (byte)'l' };
|
||||
public static readonly byte[] NameStruct = { (byte)'r', (byte)'e', (byte)'c' };
|
||||
public static readonly byte[] NameString = { (byte)'s', (byte)'t', (byte)'r' };
|
||||
public static readonly byte[] NameMap = { (byte)'m', (byte)'a', (byte)'p' };
|
||||
public static readonly byte[] NameList = { (byte)'l', (byte)'s', (byte)'t' };
|
||||
public static readonly byte[] NameSet = { (byte)'s', (byte)'e', (byte)'t' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,176 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using Thrift.Protocols.Entities;
|
||||
|
||||
namespace Thrift.Protocols.Utilities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class TJSONProtocolHelper
|
||||
{
|
||||
public static byte[] GetTypeNameForTypeId(TType typeId)
|
||||
{
|
||||
switch (typeId)
|
||||
{
|
||||
case TType.Bool:
|
||||
return TJSONProtocolConstants.TypeNames.NameBool;
|
||||
case TType.Byte:
|
||||
return TJSONProtocolConstants.TypeNames.NameByte;
|
||||
case TType.I16:
|
||||
return TJSONProtocolConstants.TypeNames.NameI16;
|
||||
case TType.I32:
|
||||
return TJSONProtocolConstants.TypeNames.NameI32;
|
||||
case TType.I64:
|
||||
return TJSONProtocolConstants.TypeNames.NameI64;
|
||||
case TType.Double:
|
||||
return TJSONProtocolConstants.TypeNames.NameDouble;
|
||||
case TType.String:
|
||||
return TJSONProtocolConstants.TypeNames.NameString;
|
||||
case TType.Struct:
|
||||
return TJSONProtocolConstants.TypeNames.NameStruct;
|
||||
case TType.Map:
|
||||
return TJSONProtocolConstants.TypeNames.NameMap;
|
||||
case TType.Set:
|
||||
return TJSONProtocolConstants.TypeNames.NameSet;
|
||||
case TType.List:
|
||||
return TJSONProtocolConstants.TypeNames.NameList;
|
||||
default:
|
||||
throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType");
|
||||
}
|
||||
}
|
||||
|
||||
public static TType GetTypeIdForTypeName(byte[] name)
|
||||
{
|
||||
var result = TType.Stop;
|
||||
if (name.Length > 1)
|
||||
{
|
||||
switch (name[0])
|
||||
{
|
||||
case (byte) 'd':
|
||||
result = TType.Double;
|
||||
break;
|
||||
case (byte) 'i':
|
||||
switch (name[1])
|
||||
{
|
||||
case (byte) '8':
|
||||
result = TType.Byte;
|
||||
break;
|
||||
case (byte) '1':
|
||||
result = TType.I16;
|
||||
break;
|
||||
case (byte) '3':
|
||||
result = TType.I32;
|
||||
break;
|
||||
case (byte) '6':
|
||||
result = TType.I64;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case (byte) 'l':
|
||||
result = TType.List;
|
||||
break;
|
||||
case (byte) 'm':
|
||||
result = TType.Map;
|
||||
break;
|
||||
case (byte) 'r':
|
||||
result = TType.Struct;
|
||||
break;
|
||||
case (byte) 's':
|
||||
if (name[1] == (byte) 't')
|
||||
{
|
||||
result = TType.String;
|
||||
}
|
||||
else if (name[1] == (byte) 'e')
|
||||
{
|
||||
result = TType.Set;
|
||||
}
|
||||
break;
|
||||
case (byte) 't':
|
||||
result = TType.Bool;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (result == TType.Stop)
|
||||
{
|
||||
throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return true if the given byte could be a valid part of a JSON number.
|
||||
/// </summary>
|
||||
public static bool IsJsonNumeric(byte b)
|
||||
{
|
||||
switch (b)
|
||||
{
|
||||
case (byte)'+':
|
||||
case (byte)'-':
|
||||
case (byte)'.':
|
||||
case (byte)'0':
|
||||
case (byte)'1':
|
||||
case (byte)'2':
|
||||
case (byte)'3':
|
||||
case (byte)'4':
|
||||
case (byte)'5':
|
||||
case (byte)'6':
|
||||
case (byte)'7':
|
||||
case (byte)'8':
|
||||
case (byte)'9':
|
||||
case (byte)'E':
|
||||
case (byte)'e':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its
|
||||
/// corresponding hex value
|
||||
/// </summary>
|
||||
public static byte ToHexVal(byte ch)
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
return (byte)((char)ch - '0');
|
||||
}
|
||||
|
||||
if (ch >= 'a' && ch <= 'f')
|
||||
{
|
||||
ch += 10;
|
||||
return (byte)((char)ch - 'a');
|
||||
}
|
||||
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected hex character");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a byte containing a hex value to its corresponding hex character
|
||||
/// </summary>
|
||||
public static byte ToHexChar(byte val)
|
||||
{
|
||||
val &= 0x0F;
|
||||
if (val < 10)
|
||||
{
|
||||
return (byte)((char)val + '0');
|
||||
}
|
||||
val -= 10;
|
||||
return (byte)((char)val + 'a');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols.Entities;
|
||||
|
||||
namespace Thrift.Protocols.Utilities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class TProtocolUtil
|
||||
{
|
||||
public static async Task SkipAsync(TProtocol protocol, TType type, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protocol.IncrementRecursionDepth();
|
||||
try
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case TType.Bool:
|
||||
await protocol.ReadBoolAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.Byte:
|
||||
await protocol.ReadByteAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.I16:
|
||||
await protocol.ReadI16Async(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.I32:
|
||||
await protocol.ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.I64:
|
||||
await protocol.ReadI64Async(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.Double:
|
||||
await protocol.ReadDoubleAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.String:
|
||||
// Don't try to decode the string, just skip it.
|
||||
await protocol.ReadBinaryAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.Struct:
|
||||
await protocol.ReadStructBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
while (true)
|
||||
{
|
||||
var field = await protocol.ReadFieldBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (field.Type == TType.Stop)
|
||||
{
|
||||
break;
|
||||
}
|
||||
await SkipAsync(protocol, field.Type, cancellationToken).ConfigureAwait(false);
|
||||
await protocol.ReadFieldEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await protocol.ReadStructEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.Map:
|
||||
var map = await protocol.ReadMapBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
for (var i = 0; i < map.Count; i++)
|
||||
{
|
||||
await SkipAsync(protocol, map.KeyType, cancellationToken).ConfigureAwait(false);
|
||||
await SkipAsync(protocol, map.ValueType, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await protocol.ReadMapEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.Set:
|
||||
var set = await protocol.ReadSetBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
for (var i = 0; i < set.Count; i++)
|
||||
{
|
||||
await SkipAsync(protocol, set.ElementType, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await protocol.ReadSetEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
case TType.List:
|
||||
var list = await protocol.ReadListBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
await SkipAsync(protocol, list.ElementType, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await protocol.ReadListEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
default:
|
||||
throw new TProtocolException(TProtocolException.INVALID_DATA, "Unknown data type " + type.ToString("d"));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
protocol.DecrementRecursionDepth();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
# Thrift Protocol .Net Implementation
|
||||
|
||||
This is the .net implementation of the Apache Thrift protocol. This code was forked from the [Jaeger Tracing C# Client Repository](https://github.com/jaegertracing/jaeger-client-csharp).
|
||||
|
||||
Path: src/Thrift
|
||||
|
||||
commitID: 0794ea71cb6e58f7bf0f0ef2c0c8ceceb1d8b6d9
|
||||
|
||||
The following changes were made to this fork:
|
||||
|
||||
* ConfigureAwait(false) added to async calls to prevent deadlocks.
|
||||
* THttpClientTransport uses WebRequestHandler() in .NET Framework 4.6 builds.
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Server
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public abstract class TBaseServer
|
||||
{
|
||||
protected ITProtocolFactory InputProtocolFactory;
|
||||
protected TTransportFactory InputTransportFactory;
|
||||
protected ITProcessorFactory ItProcessorFactory;
|
||||
protected ITProtocolFactory OutputProtocolFactory;
|
||||
protected TTransportFactory OutputTransportFactory;
|
||||
|
||||
protected TServerEventHandler ServerEventHandler;
|
||||
protected TServerTransport ServerTransport;
|
||||
|
||||
protected TBaseServer(ITProcessorFactory itProcessorFactory, TServerTransport serverTransport,
|
||||
TTransportFactory inputTransportFactory, TTransportFactory outputTransportFactory,
|
||||
ITProtocolFactory inputProtocolFactory, ITProtocolFactory outputProtocolFactory)
|
||||
{
|
||||
ItProcessorFactory = itProcessorFactory ?? throw new ArgumentNullException(nameof(itProcessorFactory));
|
||||
ServerTransport = serverTransport;
|
||||
InputTransportFactory = inputTransportFactory ?? throw new ArgumentNullException(nameof(inputTransportFactory));
|
||||
OutputTransportFactory = outputTransportFactory ?? throw new ArgumentNullException(nameof(outputTransportFactory));
|
||||
InputProtocolFactory = inputProtocolFactory ?? throw new ArgumentNullException(nameof(inputProtocolFactory));
|
||||
OutputProtocolFactory = outputProtocolFactory ?? throw new ArgumentNullException(nameof(outputProtocolFactory));
|
||||
}
|
||||
|
||||
public void SetEventHandler(TServerEventHandler seh)
|
||||
{
|
||||
ServerEventHandler = seh;
|
||||
}
|
||||
|
||||
public TServerEventHandler GetEventHandler()
|
||||
{
|
||||
return ServerEventHandler;
|
||||
}
|
||||
|
||||
public abstract void Stop();
|
||||
|
||||
public virtual void Start()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public virtual async Task ServeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift.Server
|
||||
{
|
||||
//TODO: replacement by event?
|
||||
|
||||
/// <summary>
|
||||
/// Interface implemented by server users to handle events from the server
|
||||
/// </summary>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public interface TServerEventHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Called before the server begins */
|
||||
/// </summary>
|
||||
Task PreServeAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Called when a new client has connected and is about to being processing */
|
||||
/// </summary>
|
||||
Task<object> CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Called when a client has finished request-handling to delete server context */
|
||||
/// </summary>
|
||||
Task DeleteContextAsync(object serverContext, TProtocol input, TProtocol output,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Called when a client is about to call the processor */
|
||||
/// </summary>
|
||||
Task ProcessContextAsync(object serverContext, TClientTransport transport, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
<StyleCopSettings Version="4.3" >
|
||||
<GlobalSettings>
|
||||
<BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
|
||||
</GlobalSettings>
|
||||
</StyleCopSettings>
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using Thrift.Server;
|
||||
using Thrift.Transports;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class SingletonTProcessorFactory : ITProcessorFactory
|
||||
{
|
||||
private readonly ITAsyncProcessor _tAsyncProcessor;
|
||||
|
||||
public SingletonTProcessorFactory(ITAsyncProcessor tAsyncProcessor)
|
||||
{
|
||||
_tAsyncProcessor = tAsyncProcessor;
|
||||
}
|
||||
|
||||
public ITAsyncProcessor GetAsyncProcessor(TClientTransport trans, TBaseServer baseServer = null)
|
||||
{
|
||||
return _tAsyncProcessor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,150 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocols.Utilities;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TApplicationException : TException
|
||||
{
|
||||
public enum ExceptionType
|
||||
{
|
||||
Unknown,
|
||||
UnknownMethod,
|
||||
InvalidMessageType,
|
||||
WrongMethodName,
|
||||
BadSequenceId,
|
||||
MissingResult,
|
||||
InternalError,
|
||||
ProtocolError,
|
||||
InvalidTransform,
|
||||
InvalidProtocol,
|
||||
UnsupportedClientType
|
||||
}
|
||||
|
||||
private const int MessageTypeFieldId = 1;
|
||||
private const int ExTypeFieldId = 2;
|
||||
|
||||
protected ExceptionType Type;
|
||||
|
||||
public TApplicationException()
|
||||
{
|
||||
}
|
||||
|
||||
public TApplicationException(ExceptionType type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public TApplicationException(ExceptionType type, string message)
|
||||
: base(message)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public static async Task<TApplicationException> ReadAsync(TProtocol inputProtocol, CancellationToken cancellationToken)
|
||||
{
|
||||
string message = null;
|
||||
var type = ExceptionType.Unknown;
|
||||
|
||||
await inputProtocol.ReadStructBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
while (true)
|
||||
{
|
||||
var field = await inputProtocol.ReadFieldBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (field.Type == TType.Stop)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
switch (field.ID)
|
||||
{
|
||||
case MessageTypeFieldId:
|
||||
if (field.Type == TType.String)
|
||||
{
|
||||
message = await inputProtocol.ReadStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await TProtocolUtil.SkipAsync(inputProtocol, field.Type, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
case ExTypeFieldId:
|
||||
if (field.Type == TType.I32)
|
||||
{
|
||||
type = (ExceptionType) await inputProtocol.ReadI32Async(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await TProtocolUtil.SkipAsync(inputProtocol, field.Type, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
await TProtocolUtil.SkipAsync(inputProtocol, field.Type, cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
}
|
||||
|
||||
await inputProtocol.ReadFieldEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await inputProtocol.ReadStructEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new TApplicationException(type, message);
|
||||
}
|
||||
|
||||
public async Task WriteAsync(TProtocol outputProtocol, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const string messageTypeFieldName = "message";
|
||||
const string exTypeFieldName = "exType";
|
||||
const string structApplicationExceptionName = "TApplicationException";
|
||||
|
||||
var struc = new TStruct(structApplicationExceptionName);
|
||||
var field = new TField();
|
||||
|
||||
await outputProtocol.WriteStructBeginAsync(struc, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!string.IsNullOrEmpty(Message))
|
||||
{
|
||||
field.Name = messageTypeFieldName;
|
||||
field.Type = TType.String;
|
||||
field.ID = MessageTypeFieldId;
|
||||
await outputProtocol.WriteFieldBeginAsync(field, cancellationToken).ConfigureAwait(false);
|
||||
await outputProtocol.WriteStringAsync(Message, cancellationToken).ConfigureAwait(false);
|
||||
await outputProtocol.WriteFieldEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
field.Name = exTypeFieldName;
|
||||
field.Type = TType.I32;
|
||||
field.ID = ExTypeFieldId;
|
||||
|
||||
await outputProtocol.WriteFieldBeginAsync(field, cancellationToken).ConfigureAwait(false);
|
||||
await outputProtocol.WriteI32Async((int) Type, cancellationToken).ConfigureAwait(false);
|
||||
await outputProtocol.WriteFieldEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
await outputProtocol.WriteFieldStopAsync(cancellationToken).ConfigureAwait(false);
|
||||
await outputProtocol.WriteStructEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
/// <summary>
|
||||
/// TBaseClient.
|
||||
/// Base client for generated clients.
|
||||
/// Do not change this class without checking generated code (namings, etc.)
|
||||
/// </summary>
|
||||
public abstract class TBaseClient
|
||||
{
|
||||
private readonly TProtocol _inputProtocol;
|
||||
private readonly TProtocol _outputProtocol;
|
||||
private bool _isDisposed;
|
||||
private int _seqId;
|
||||
public readonly Guid ClientId = Guid.NewGuid();
|
||||
|
||||
protected TBaseClient(TProtocol inputProtocol, TProtocol outputProtocol)
|
||||
{
|
||||
_inputProtocol = inputProtocol ?? throw new ArgumentNullException(nameof(inputProtocol));
|
||||
_outputProtocol = outputProtocol ?? throw new ArgumentNullException(nameof(outputProtocol));
|
||||
}
|
||||
|
||||
public TProtocol InputProtocol => _inputProtocol;
|
||||
|
||||
public TProtocol OutputProtocol => _outputProtocol;
|
||||
|
||||
public int SeqId
|
||||
{
|
||||
get { return ++_seqId; }
|
||||
}
|
||||
|
||||
public virtual async Task OpenTransportAsync()
|
||||
{
|
||||
await OpenTransportAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task OpenTransportAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_inputProtocol.Transport.IsOpen)
|
||||
{
|
||||
await _inputProtocol.Transport.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (!_inputProtocol.Transport.IsOpen)
|
||||
{
|
||||
await _outputProtocol.Transport.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_inputProtocol?.Dispose();
|
||||
_outputProtocol?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TException : Exception
|
||||
{
|
||||
public TException()
|
||||
{
|
||||
}
|
||||
|
||||
public TException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,143 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
|
||||
namespace Thrift
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TMultiplexedProcessor : ITAsyncProcessor
|
||||
{
|
||||
//TODO: Localization
|
||||
|
||||
private readonly Dictionary<string, ITAsyncProcessor> _serviceProcessorMap =
|
||||
new Dictionary<string, ITAsyncProcessor>();
|
||||
|
||||
public async Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot)
|
||||
{
|
||||
return await ProcessAsync(iprot, oprot, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<bool>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var message = await iprot.ReadMessageBeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if ((message.Type != TMessageType.Call) && (message.Type != TMessageType.Oneway))
|
||||
{
|
||||
await FailAsync(oprot, message, TApplicationException.ExceptionType.InvalidMessageType,
|
||||
"Message exType CALL or ONEWAY expected", cancellationToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extract the service name
|
||||
var index = message.Name.IndexOf(TMultiplexedProtocol.Separator, StringComparison.Ordinal);
|
||||
if (index < 0)
|
||||
{
|
||||
await FailAsync(oprot, message, TApplicationException.ExceptionType.InvalidProtocol,
|
||||
$"Service name not found in message name: {message.Name}. Did you forget to use a TMultiplexProtocol in your client?",
|
||||
cancellationToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a new TMessage, something that can be consumed by any TProtocol
|
||||
var serviceName = message.Name.Substring(0, index);
|
||||
ITAsyncProcessor actualProcessor;
|
||||
if (!_serviceProcessorMap.TryGetValue(serviceName, out actualProcessor))
|
||||
{
|
||||
await FailAsync(oprot, message, TApplicationException.ExceptionType.InternalError,
|
||||
$"Service name not found: {serviceName}. Did you forget to call RegisterProcessor()?",
|
||||
cancellationToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a new TMessage, removing the service name
|
||||
var newMessage = new TMessage(
|
||||
message.Name.Substring(serviceName.Length + TMultiplexedProtocol.Separator.Length),
|
||||
message.Type,
|
||||
message.SeqID);
|
||||
|
||||
// Dispatch processing to the stored processor
|
||||
return
|
||||
await
|
||||
actualProcessor.ProcessAsync(new StoredMessageProtocol(iprot, newMessage), oprot,
|
||||
cancellationToken);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return false; // similar to all other processors
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterProcessor(string serviceName, ITAsyncProcessor processor)
|
||||
{
|
||||
if (_serviceProcessorMap.ContainsKey(serviceName))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Processor map already contains processor with name: '{serviceName}'");
|
||||
}
|
||||
|
||||
_serviceProcessorMap.Add(serviceName, processor);
|
||||
}
|
||||
|
||||
private async Task FailAsync(TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype,
|
||||
string etxt, CancellationToken cancellationToken)
|
||||
{
|
||||
var appex = new TApplicationException(extype, etxt);
|
||||
|
||||
var newMessage = new TMessage(message.Name, TMessageType.Exception, message.SeqID);
|
||||
|
||||
await oprot.WriteMessageBeginAsync(newMessage, cancellationToken).ConfigureAwait(false);
|
||||
await appex.WriteAsync(oprot, cancellationToken).ConfigureAwait(false);
|
||||
await oprot.WriteMessageEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
await oprot.Transport.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private class StoredMessageProtocol : TProtocolDecorator
|
||||
{
|
||||
readonly TMessage _msgBegin;
|
||||
|
||||
public StoredMessageProtocol(TProtocol protocol, TMessage messageBegin)
|
||||
: base(protocol)
|
||||
{
|
||||
_msgBegin = messageBegin;
|
||||
}
|
||||
|
||||
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TMessage>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return _msgBegin;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
|
||||
<Authors>OpenTelemetry authors</Authors>
|
||||
<PackageIconUrl>https://opentelemetry.io/img/logos/opentelemetry-icon-color.png</PackageIconUrl>
|
||||
<PackageProjectUrl>https://OpenTelemetry.io</PackageProjectUrl>
|
||||
<PackageTags>Tracing;OpenTelemetry;Management;Monitoring;Jaeger;distributed-tracing</PackageTags>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<NoWarn>$(NoWarn),1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
|
||||
<Reference Include="System.Net.Http.WebRequest" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -1,206 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TBufferedClientTransport : TClientTransport
|
||||
{
|
||||
private readonly int _bufSize;
|
||||
private readonly MemoryStream _inputBuffer = new MemoryStream(0);
|
||||
private readonly MemoryStream _outputBuffer = new MemoryStream(0);
|
||||
private readonly TClientTransport _transport;
|
||||
private bool _isDisposed;
|
||||
|
||||
//TODO: should support only specified input transport?
|
||||
public TBufferedClientTransport(TClientTransport transport, int bufSize = 1024)
|
||||
{
|
||||
if (bufSize <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(bufSize), "Buffer size must be a positive number.");
|
||||
}
|
||||
|
||||
_transport = transport ?? throw new ArgumentNullException(nameof(transport));
|
||||
_bufSize = bufSize;
|
||||
}
|
||||
|
||||
public TClientTransport UnderlyingTransport
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
return _transport;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsOpen => !_isDisposed && _transport.IsOpen;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
await _transport.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
_transport.Close();
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
//TODO: investigate how it should work correctly
|
||||
CheckNotDisposed();
|
||||
|
||||
ValidateBufferArgs(buffer, offset, length);
|
||||
|
||||
if (!IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
if (_inputBuffer.Capacity < _bufSize)
|
||||
{
|
||||
_inputBuffer.Capacity = _bufSize;
|
||||
}
|
||||
|
||||
var got = await _inputBuffer.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
if (got > 0)
|
||||
{
|
||||
return got;
|
||||
}
|
||||
|
||||
_inputBuffer.Seek(0, SeekOrigin.Begin);
|
||||
_inputBuffer.SetLength(_inputBuffer.Capacity);
|
||||
|
||||
ArraySegment<byte> bufSegment;
|
||||
_inputBuffer.TryGetBuffer(out bufSegment);
|
||||
|
||||
// investigate
|
||||
var filled = await _transport.ReadAsync(bufSegment.Array, 0, (int) _inputBuffer.Length, cancellationToken).ConfigureAwait(false);
|
||||
_inputBuffer.SetLength(filled);
|
||||
|
||||
if (filled == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return await ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
ValidateBufferArgs(buffer, offset, length);
|
||||
|
||||
if (!IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
// Relative offset from "off" argument
|
||||
var writtenCount = 0;
|
||||
if (_outputBuffer.Length > 0)
|
||||
{
|
||||
var capa = (int) (_outputBuffer.Capacity - _outputBuffer.Length);
|
||||
var writeSize = capa <= length ? capa : length;
|
||||
await _outputBuffer.WriteAsync(buffer, offset, writeSize, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
writtenCount += writeSize;
|
||||
if (writeSize == capa)
|
||||
{
|
||||
//ArraySegment<byte> bufSegment;
|
||||
//_outputBuffer.TryGetBuffer(out bufSegment);
|
||||
var data = _outputBuffer.ToArray();
|
||||
//await _transport.WriteAsync(bufSegment.Array, cancellationToken).ConfigureAwait(false);
|
||||
await _transport.WriteAsync(data, cancellationToken).ConfigureAwait(false);
|
||||
_outputBuffer.SetLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
while (length - writtenCount >= _bufSize)
|
||||
{
|
||||
await _transport.WriteAsync(buffer, offset + writtenCount, _bufSize, cancellationToken).ConfigureAwait(false);
|
||||
writtenCount += _bufSize;
|
||||
}
|
||||
|
||||
var remain = length - writtenCount;
|
||||
if (remain > 0)
|
||||
{
|
||||
if (_outputBuffer.Capacity < _bufSize)
|
||||
{
|
||||
_outputBuffer.Capacity = _bufSize;
|
||||
}
|
||||
await _outputBuffer.WriteAsync(buffer, offset + writtenCount, remain, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
if (!IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
if (_outputBuffer.Length > 0)
|
||||
{
|
||||
//ArraySegment<byte> bufSegment;
|
||||
var data = _outputBuffer.ToArray(); // TryGetBuffer(out bufSegment);
|
||||
|
||||
await _transport.WriteAsync(data /*bufSegment.Array*/, cancellationToken).ConfigureAwait(false);
|
||||
_outputBuffer.SetLength(0);
|
||||
}
|
||||
|
||||
await _transport.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private void CheckNotDisposed()
|
||||
{
|
||||
if (_isDisposed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(_transport));
|
||||
}
|
||||
}
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_inputBuffer?.Dispose();
|
||||
_outputBuffer?.Dispose();
|
||||
_transport?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,201 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
//TODO: check for correct implementation
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TFramedClientTransport : TClientTransport
|
||||
{
|
||||
private const int HeaderSize = 4;
|
||||
private readonly byte[] _headerBuf = new byte[HeaderSize];
|
||||
private readonly MemoryStream _readBuffer = new MemoryStream(1024);
|
||||
private readonly TClientTransport _transport;
|
||||
private readonly MemoryStream _writeBuffer = new MemoryStream(1024);
|
||||
|
||||
private bool _isDisposed;
|
||||
|
||||
public TFramedClientTransport(TClientTransport transport)
|
||||
{
|
||||
_transport = transport ?? throw new ArgumentNullException(nameof(transport));
|
||||
|
||||
InitWriteBuffer();
|
||||
}
|
||||
|
||||
public override bool IsOpen => !_isDisposed && _transport.IsOpen;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
await _transport.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
_transport.Close();
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
ValidateBufferArgs(buffer, offset, length);
|
||||
|
||||
if (!IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
var got = await _readBuffer.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
if (got > 0)
|
||||
{
|
||||
return got;
|
||||
}
|
||||
|
||||
// Read another frame of data
|
||||
await ReadFrameAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return await _readBuffer.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ReadFrameAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _transport.ReadAllAsync(_headerBuf, 0, HeaderSize, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var size = DecodeFrameSize(_headerBuf);
|
||||
|
||||
_readBuffer.SetLength(size);
|
||||
_readBuffer.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
ArraySegment<byte> bufSegment;
|
||||
_readBuffer.TryGetBuffer(out bufSegment);
|
||||
|
||||
var buff = bufSegment.Array;
|
||||
|
||||
await _transport.ReadAllAsync(buff, 0, size, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
ValidateBufferArgs(buffer, offset, length);
|
||||
|
||||
if (!IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
if (_writeBuffer.Length + length > int.MaxValue)
|
||||
{
|
||||
await FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await _writeBuffer.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
CheckNotDisposed();
|
||||
|
||||
if (!IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
//ArraySegment<byte> bufSegment;
|
||||
//_writeBuffer.TryGetBuffer(out bufSegment);
|
||||
//var buf = bufSegment.Array;
|
||||
var buf = _writeBuffer.ToArray();
|
||||
|
||||
//var len = (int)_writeBuffer.Length;
|
||||
var dataLen = (int) _writeBuffer.Length - HeaderSize;
|
||||
if (dataLen < 0)
|
||||
{
|
||||
throw new InvalidOperationException(); // logic error actually
|
||||
}
|
||||
|
||||
// Inject message header into the reserved buffer space
|
||||
EncodeFrameSize(dataLen, buf);
|
||||
|
||||
// Send the entire message at once
|
||||
await _transport.WriteAsync(buf, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
InitWriteBuffer();
|
||||
|
||||
await _transport.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private void InitWriteBuffer()
|
||||
{
|
||||
// Reserve space for message header to be put right before sending it out
|
||||
_writeBuffer.SetLength(HeaderSize);
|
||||
_writeBuffer.Seek(0, SeekOrigin.End);
|
||||
}
|
||||
|
||||
private static void EncodeFrameSize(int frameSize, byte[] buf)
|
||||
{
|
||||
buf[0] = (byte) (0xff & (frameSize >> 24));
|
||||
buf[1] = (byte) (0xff & (frameSize >> 16));
|
||||
buf[2] = (byte) (0xff & (frameSize >> 8));
|
||||
buf[3] = (byte) (0xff & (frameSize));
|
||||
}
|
||||
|
||||
private static int DecodeFrameSize(byte[] buf)
|
||||
{
|
||||
return
|
||||
((buf[0] & 0xff) << 24) |
|
||||
((buf[1] & 0xff) << 16) |
|
||||
((buf[2] & 0xff) << 8) |
|
||||
(buf[3] & 0xff);
|
||||
}
|
||||
|
||||
|
||||
private void CheckNotDisposed()
|
||||
{
|
||||
if (_isDisposed)
|
||||
{
|
||||
throw new ObjectDisposedException("TFramedClientTransport");
|
||||
}
|
||||
}
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_readBuffer?.Dispose();
|
||||
_writeBuffer?.Dispose();
|
||||
_transport?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,235 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class THttpClientTransport : TClientTransport
|
||||
{
|
||||
private readonly X509Certificate[] _certificates;
|
||||
private readonly Uri _uri;
|
||||
|
||||
// Timeouts in milliseconds
|
||||
private int _connectTimeout = 30000;
|
||||
private HttpClient _httpClient;
|
||||
private Stream _inputStream;
|
||||
|
||||
private bool _isDisposed;
|
||||
private MemoryStream _outputStream = new MemoryStream();
|
||||
private static readonly MediaTypeWithQualityHeaderValue ApacheThriftMediaType = new MediaTypeWithQualityHeaderValue("application/vnd.apache.thrift.binary");
|
||||
|
||||
public THttpClientTransport(Uri u, IDictionary<string, string> customHeaders)
|
||||
: this(u, Enumerable.Empty<X509Certificate>(), customHeaders)
|
||||
{
|
||||
}
|
||||
|
||||
public THttpClientTransport(Uri u, IEnumerable<X509Certificate> certificates,
|
||||
IDictionary<string, string> customHeaders)
|
||||
{
|
||||
_uri = u;
|
||||
_certificates = (certificates ?? Enumerable.Empty<X509Certificate>()).ToArray();
|
||||
CustomHeaders = customHeaders;
|
||||
|
||||
// due to current bug with performance of Dispose in netcore https://github.com/dotnet/corefx/issues/8809
|
||||
// this can be switched to default way (create client->use->dispose per flush) later
|
||||
_httpClient = CreateClient();
|
||||
}
|
||||
|
||||
public IDictionary<string, string> CustomHeaders { get; }
|
||||
|
||||
public int ConnectTimeout
|
||||
{
|
||||
set { _connectTimeout = value; }
|
||||
}
|
||||
|
||||
public override bool IsOpen => true;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_inputStream != null)
|
||||
{
|
||||
_inputStream.Dispose();
|
||||
_inputStream = null;
|
||||
}
|
||||
|
||||
if (_outputStream != null)
|
||||
{
|
||||
_outputStream.Dispose();
|
||||
_outputStream = null;
|
||||
}
|
||||
|
||||
if (_httpClient != null)
|
||||
{
|
||||
_httpClient.Dispose();
|
||||
_httpClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<int>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (_inputStream == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var ret = await _inputStream.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (ret == -1)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.EndOfFile, "No more data available");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
catch (IOException iox)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await _outputStream.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private HttpClient CreateClient()
|
||||
{
|
||||
#if NET46
|
||||
var handler = new WebRequestHandler();
|
||||
#else
|
||||
var handler = new HttpClientHandler();
|
||||
#endif
|
||||
if (_certificates.Length > 0)
|
||||
{
|
||||
handler.ClientCertificates.AddRange(_certificates);
|
||||
}
|
||||
|
||||
var httpClient = new HttpClient(handler);
|
||||
|
||||
if (_connectTimeout > 0)
|
||||
{
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(_connectTimeout);
|
||||
}
|
||||
|
||||
httpClient.DefaultRequestHeaders.Accept.Add(ApacheThriftMediaType);
|
||||
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("THttpClientTransport", "1.0.0"));
|
||||
|
||||
if (CustomHeaders != null)
|
||||
{
|
||||
foreach (var item in CustomHeaders)
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_outputStream.CanSeek)
|
||||
{
|
||||
_outputStream.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
using (var outStream = new StreamContent(_outputStream))
|
||||
{
|
||||
outStream.Headers.ContentType = ApacheThriftMediaType;
|
||||
var msg = await _httpClient.PostAsync(_uri, outStream, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
msg.EnsureSuccessStatusCode();
|
||||
|
||||
if (_inputStream != null)
|
||||
{
|
||||
_inputStream.Dispose();
|
||||
_inputStream = null;
|
||||
}
|
||||
|
||||
_inputStream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
||||
if (_inputStream.CanSeek)
|
||||
{
|
||||
_inputStream.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException iox)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString());
|
||||
}
|
||||
catch (HttpRequestException wx)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown,
|
||||
"Couldn't connect to server: " + wx);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_outputStream = new MemoryStream();
|
||||
}
|
||||
}
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_inputStream?.Dispose();
|
||||
_outputStream?.Dispose();
|
||||
_httpClient?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TMemoryBufferClientTransport : TClientTransport
|
||||
{
|
||||
private readonly MemoryStream _byteStream;
|
||||
private bool _isDisposed;
|
||||
|
||||
public TMemoryBufferClientTransport()
|
||||
{
|
||||
_byteStream = new MemoryStream();
|
||||
}
|
||||
|
||||
public TMemoryBufferClientTransport(byte[] buf)
|
||||
{
|
||||
_byteStream = new MemoryStream(buf);
|
||||
}
|
||||
|
||||
public override bool IsOpen => true;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return await _byteStream.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, CancellationToken cancellationToken)
|
||||
{
|
||||
await _byteStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
await _byteStream.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] GetBuffer()
|
||||
{
|
||||
return _byteStream.ToArray();
|
||||
}
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_byteStream?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.IO.Pipes;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TNamedPipeClientTransport : TClientTransport
|
||||
{
|
||||
private NamedPipeClientStream _client;
|
||||
|
||||
public TNamedPipeClientTransport(string pipe) : this(".", pipe)
|
||||
{
|
||||
}
|
||||
|
||||
public TNamedPipeClientTransport(string server, string pipe)
|
||||
{
|
||||
var serverName = string.IsNullOrWhiteSpace(server) ? server : ".";
|
||||
|
||||
_client = new NamedPipeClientStream(serverName, pipe, PipeDirection.InOut, PipeOptions.None);
|
||||
}
|
||||
|
||||
public override bool IsOpen => _client != null && _client.IsConnected;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen);
|
||||
}
|
||||
|
||||
await _client.ConnectAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_client != null)
|
||||
{
|
||||
_client.Dispose();
|
||||
_client = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (_client == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
return await _client.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_client == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
await _client.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
_client.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,139 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TSocketClientTransport : TStreamClientTransport
|
||||
{
|
||||
private bool _isDisposed;
|
||||
|
||||
public TSocketClientTransport(TcpClient client)
|
||||
{
|
||||
TcpClient = client ?? throw new ArgumentNullException(nameof(client));
|
||||
|
||||
if (IsOpen)
|
||||
{
|
||||
InputStream = client.GetStream();
|
||||
OutputStream = client.GetStream();
|
||||
}
|
||||
}
|
||||
|
||||
public TSocketClientTransport(IPAddress host, int port)
|
||||
: this(host, port, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public TSocketClientTransport(IPAddress host, int port, int timeout)
|
||||
{
|
||||
Host = host;
|
||||
Port = port;
|
||||
|
||||
TcpClient = new TcpClient();
|
||||
TcpClient.ReceiveTimeout = TcpClient.SendTimeout = timeout;
|
||||
TcpClient.Client.NoDelay = true;
|
||||
}
|
||||
|
||||
public TcpClient TcpClient { get; private set; }
|
||||
public IPAddress Host { get; }
|
||||
public int Port { get; }
|
||||
|
||||
public int Timeout
|
||||
{
|
||||
set
|
||||
{
|
||||
if (TcpClient != null)
|
||||
{
|
||||
TcpClient.ReceiveTimeout = TcpClient.SendTimeout = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsOpen
|
||||
{
|
||||
get
|
||||
{
|
||||
if (TcpClient == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return TcpClient.Connected;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
|
||||
}
|
||||
|
||||
if (Port <= 0)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
|
||||
}
|
||||
|
||||
if (TcpClient == null)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid or not initialized tcp client");
|
||||
}
|
||||
|
||||
await TcpClient.ConnectAsync(Host, Port).ConfigureAwait(false);
|
||||
|
||||
InputStream = TcpClient.GetStream();
|
||||
OutputStream = TcpClient.GetStream();
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
|
||||
if (TcpClient != null)
|
||||
{
|
||||
TcpClient.Dispose();
|
||||
TcpClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
TcpClient?.Dispose();
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TStreamClientTransport : TClientTransport
|
||||
{
|
||||
private bool _isDisposed;
|
||||
|
||||
protected TStreamClientTransport()
|
||||
{
|
||||
}
|
||||
|
||||
public TStreamClientTransport(Stream inputStream, Stream outputStream)
|
||||
{
|
||||
InputStream = inputStream;
|
||||
OutputStream = outputStream;
|
||||
}
|
||||
|
||||
protected Stream OutputStream { get; set; }
|
||||
|
||||
protected Stream InputStream { get; set; }
|
||||
|
||||
public override bool IsOpen => true;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (InputStream != null)
|
||||
{
|
||||
InputStream.Dispose();
|
||||
InputStream = null;
|
||||
}
|
||||
|
||||
if (OutputStream != null)
|
||||
{
|
||||
OutputStream.Dispose();
|
||||
OutputStream = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (InputStream == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen,
|
||||
"Cannot read from null inputstream");
|
||||
}
|
||||
|
||||
return await InputStream.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
if (OutputStream == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen,
|
||||
"Cannot read from null inputstream");
|
||||
}
|
||||
|
||||
await OutputStream.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return OutputStream.FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
InputStream?.Dispose();
|
||||
OutputStream?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,237 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Client
|
||||
{
|
||||
//TODO: check for correct work
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TTlsSocketClientTransport : TStreamClientTransport
|
||||
{
|
||||
private readonly X509Certificate2 _certificate;
|
||||
private readonly RemoteCertificateValidationCallback _certValidator;
|
||||
private readonly IPAddress _host;
|
||||
private readonly bool _isServer;
|
||||
private readonly LocalCertificateSelectionCallback _localCertificateSelectionCallback;
|
||||
private readonly int _port;
|
||||
private readonly SslProtocols _sslProtocols;
|
||||
private TcpClient _client;
|
||||
private SslStream _secureStream;
|
||||
private int _timeout;
|
||||
|
||||
public TTlsSocketClientTransport(TcpClient client, X509Certificate2 certificate, bool isServer = false,
|
||||
RemoteCertificateValidationCallback certValidator = null,
|
||||
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
|
||||
SslProtocols sslProtocols = SslProtocols.Tls12)
|
||||
{
|
||||
_client = client;
|
||||
_certificate = certificate;
|
||||
_certValidator = certValidator;
|
||||
_localCertificateSelectionCallback = localCertificateSelectionCallback;
|
||||
_sslProtocols = sslProtocols;
|
||||
_isServer = isServer;
|
||||
|
||||
if (isServer && certificate == null)
|
||||
{
|
||||
throw new ArgumentException("TTlsSocketClientTransport needs certificate to be used for server",
|
||||
nameof(certificate));
|
||||
}
|
||||
|
||||
if (IsOpen)
|
||||
{
|
||||
InputStream = client.GetStream();
|
||||
OutputStream = client.GetStream();
|
||||
}
|
||||
}
|
||||
|
||||
public TTlsSocketClientTransport(IPAddress host, int port, string certificatePath,
|
||||
RemoteCertificateValidationCallback certValidator = null,
|
||||
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
|
||||
SslProtocols sslProtocols = SslProtocols.Tls12)
|
||||
: this(host, port, 0,
|
||||
new X509Certificate2(certificatePath),
|
||||
certValidator,
|
||||
localCertificateSelectionCallback,
|
||||
sslProtocols)
|
||||
{
|
||||
}
|
||||
|
||||
public TTlsSocketClientTransport(IPAddress host, int port,
|
||||
X509Certificate2 certificate = null,
|
||||
RemoteCertificateValidationCallback certValidator = null,
|
||||
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
|
||||
SslProtocols sslProtocols = SslProtocols.Tls12)
|
||||
: this(host, port, 0,
|
||||
certificate,
|
||||
certValidator,
|
||||
localCertificateSelectionCallback,
|
||||
sslProtocols)
|
||||
{
|
||||
}
|
||||
|
||||
public TTlsSocketClientTransport(IPAddress host, int port, int timeout,
|
||||
X509Certificate2 certificate,
|
||||
RemoteCertificateValidationCallback certValidator = null,
|
||||
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
|
||||
SslProtocols sslProtocols = SslProtocols.Tls12)
|
||||
{
|
||||
_host = host;
|
||||
_port = port;
|
||||
_timeout = timeout;
|
||||
_certificate = certificate;
|
||||
_certValidator = certValidator;
|
||||
_localCertificateSelectionCallback = localCertificateSelectionCallback;
|
||||
_sslProtocols = sslProtocols;
|
||||
|
||||
InitSocket();
|
||||
}
|
||||
|
||||
public int Timeout
|
||||
{
|
||||
set { _client.ReceiveTimeout = _client.SendTimeout = _timeout = value; }
|
||||
}
|
||||
|
||||
public TcpClient TcpClient => _client;
|
||||
|
||||
public IPAddress Host => _host;
|
||||
|
||||
public int Port => _port;
|
||||
|
||||
public override bool IsOpen
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_client == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _client.Connected;
|
||||
}
|
||||
}
|
||||
|
||||
private void InitSocket()
|
||||
{
|
||||
_client = new TcpClient();
|
||||
_client.ReceiveTimeout = _client.SendTimeout = _timeout;
|
||||
_client.Client.NoDelay = true;
|
||||
}
|
||||
|
||||
private bool DefaultCertificateValidator(object sender, X509Certificate certificate, X509Chain chain,
|
||||
SslPolicyErrors sslValidationErrors)
|
||||
{
|
||||
return sslValidationErrors == SslPolicyErrors.None;
|
||||
}
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (IsOpen)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
|
||||
}
|
||||
|
||||
if (_host == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
|
||||
}
|
||||
|
||||
if (_port <= 0)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
|
||||
}
|
||||
|
||||
if (_client == null)
|
||||
{
|
||||
InitSocket();
|
||||
}
|
||||
|
||||
if (_client != null)
|
||||
{
|
||||
await _client.ConnectAsync(_host, _port).ConfigureAwait(false);
|
||||
await SetupTlsAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetupTlsAsync()
|
||||
{
|
||||
var validator = _certValidator ?? DefaultCertificateValidator;
|
||||
|
||||
if (_localCertificateSelectionCallback != null)
|
||||
{
|
||||
_secureStream = new SslStream(_client.GetStream(), false, validator, _localCertificateSelectionCallback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_secureStream = new SslStream(_client.GetStream(), false, validator);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (_isServer)
|
||||
{
|
||||
// Server authentication
|
||||
await
|
||||
_secureStream.AuthenticateAsServerAsync(_certificate, _certValidator != null, _sslProtocols,
|
||||
true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Client authentication
|
||||
var certs = _certificate != null
|
||||
? new X509CertificateCollection {_certificate}
|
||||
: new X509CertificateCollection();
|
||||
|
||||
var targetHost = _host.ToString();
|
||||
await _secureStream.AuthenticateAsClientAsync(targetHost, certs, _sslProtocols, true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Close();
|
||||
throw;
|
||||
}
|
||||
|
||||
InputStream = _secureStream;
|
||||
OutputStream = _secureStream;
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
if (_client != null)
|
||||
{
|
||||
_client.Dispose();
|
||||
_client = null;
|
||||
}
|
||||
|
||||
if (_secureStream != null)
|
||||
{
|
||||
_secureStream.Dispose();
|
||||
_secureStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,191 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.IO.Pipes;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports.Server
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TNamedPipeServerTransport : TServerTransport
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the address of the Pipe on the localhost.
|
||||
/// </summary>
|
||||
private readonly string _pipeAddress;
|
||||
|
||||
private bool _asyncMode = true;
|
||||
private volatile bool _isPending = true;
|
||||
|
||||
private NamedPipeServerStream _stream = null;
|
||||
|
||||
public TNamedPipeServerTransport(string pipeAddress)
|
||||
{
|
||||
_pipeAddress = pipeAddress;
|
||||
}
|
||||
|
||||
public override void Listen()
|
||||
{
|
||||
// nothing to do here
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_stream != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
//TODO: check for disconection
|
||||
_stream.Disconnect();
|
||||
_stream.Dispose();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stream = null;
|
||||
_isPending = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsClientPending()
|
||||
{
|
||||
return _isPending;
|
||||
}
|
||||
|
||||
private void EnsurePipeInstance()
|
||||
{
|
||||
if (_stream == null)
|
||||
{
|
||||
var direction = PipeDirection.InOut;
|
||||
var maxconn = 254;
|
||||
var mode = PipeTransmissionMode.Byte;
|
||||
var options = _asyncMode ? PipeOptions.Asynchronous : PipeOptions.None;
|
||||
var inbuf = 4096;
|
||||
var outbuf = 4096;
|
||||
// TODO: security
|
||||
|
||||
try
|
||||
{
|
||||
_stream = new NamedPipeServerStream(_pipeAddress, direction, maxconn, mode, options, inbuf, outbuf);
|
||||
}
|
||||
catch (NotImplementedException) // Mono still does not support async, fallback to sync
|
||||
{
|
||||
if (_asyncMode)
|
||||
{
|
||||
options &= (~PipeOptions.Asynchronous);
|
||||
_stream = new NamedPipeServerStream(_pipeAddress, direction, maxconn, mode, options, inbuf,
|
||||
outbuf);
|
||||
_asyncMode = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task<TClientTransport> AcceptImplementationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
EnsurePipeInstance();
|
||||
|
||||
await _stream.WaitForConnectionAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var trans = new ServerTransport(_stream);
|
||||
_stream = null; // pass ownership to ServerTransport
|
||||
|
||||
//_isPending = false;
|
||||
|
||||
return trans;
|
||||
}
|
||||
catch (TTransportException)
|
||||
{
|
||||
Close();
|
||||
throw;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Close();
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private class ServerTransport : TClientTransport
|
||||
{
|
||||
private readonly NamedPipeServerStream _stream;
|
||||
|
||||
public ServerTransport(NamedPipeServerStream stream)
|
||||
{
|
||||
_stream = stream;
|
||||
}
|
||||
|
||||
public override bool IsOpen => _stream != null && _stream.IsConnected;
|
||||
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
_stream?.Dispose();
|
||||
}
|
||||
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (_stream == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
return await _stream.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (_stream == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen);
|
||||
}
|
||||
|
||||
await _stream.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
_stream?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,150 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Transports.Client;
|
||||
|
||||
namespace Thrift.Transports.Server
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TServerFramedTransport : TServerTransport
|
||||
{
|
||||
private readonly int _clientTimeout;
|
||||
private readonly int _port;
|
||||
private TcpListener _server;
|
||||
|
||||
public TServerFramedTransport(TcpListener listener)
|
||||
: this(listener, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public TServerFramedTransport(TcpListener listener, int clientTimeout)
|
||||
{
|
||||
_server = listener;
|
||||
_clientTimeout = clientTimeout;
|
||||
}
|
||||
|
||||
public TServerFramedTransport(int port)
|
||||
: this(port, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public TServerFramedTransport(int port, int clientTimeout)
|
||||
{
|
||||
_port = port;
|
||||
_clientTimeout = clientTimeout;
|
||||
try
|
||||
{
|
||||
// Make server socket
|
||||
_server = new TcpListener(IPAddress.Any, _port);
|
||||
_server.Server.NoDelay = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_server = null;
|
||||
throw new TTransportException("Could not create ServerSocket on port " + port + ".");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Listen()
|
||||
{
|
||||
// Make sure not to block on accept
|
||||
if (_server != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_server.Start();
|
||||
}
|
||||
catch (SocketException sx)
|
||||
{
|
||||
throw new TTransportException("Could not accept on listening socket: " + sx.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsClientPending()
|
||||
{
|
||||
return _server.Pending();
|
||||
}
|
||||
|
||||
protected override async Task<TClientTransport> AcceptImplementationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TClientTransport>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (_server == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TFramedClientTransport tSocketTransport = null;
|
||||
var tcpClient = await _server.AcceptTcpClientAsync().ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
tSocketTransport = new TFramedClientTransport(new TSocketClientTransport(tcpClient)
|
||||
{
|
||||
Timeout = _clientTimeout
|
||||
});
|
||||
|
||||
return tSocketTransport;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (tSocketTransport != null)
|
||||
{
|
||||
tSocketTransport.Dispose();
|
||||
}
|
||||
else // Otherwise, clean it up ourselves.
|
||||
{
|
||||
((IDisposable) tcpClient).Dispose();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new TTransportException(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_server != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new TTransportException("WARNING: Could not close server socket: " + ex);
|
||||
}
|
||||
_server = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,174 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Transports.Client;
|
||||
|
||||
namespace Thrift.Transports.Server
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TServerSocketTransport : TServerTransport
|
||||
{
|
||||
private readonly int _clientTimeout;
|
||||
private readonly int _port;
|
||||
private readonly bool _useBufferedSockets;
|
||||
private readonly bool _useFramedTransport;
|
||||
private TcpListener _server;
|
||||
|
||||
public TServerSocketTransport(TcpListener listener)
|
||||
: this(listener, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public TServerSocketTransport(TcpListener listener, int clientTimeout)
|
||||
{
|
||||
_server = listener;
|
||||
_clientTimeout = clientTimeout;
|
||||
}
|
||||
|
||||
public TServerSocketTransport(int port)
|
||||
: this(port, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public TServerSocketTransport(int port, int clientTimeout)
|
||||
: this(port, clientTimeout, false)
|
||||
{
|
||||
}
|
||||
|
||||
public TServerSocketTransport(int port, int clientTimeout, bool useBufferedSockets):
|
||||
this(port, clientTimeout, useBufferedSockets, false)
|
||||
{
|
||||
}
|
||||
|
||||
public TServerSocketTransport(int port, int clientTimeout, bool useBufferedSockets, bool useFramedTransport)
|
||||
{
|
||||
_port = port;
|
||||
_clientTimeout = clientTimeout;
|
||||
_useBufferedSockets = useBufferedSockets;
|
||||
_useFramedTransport = useFramedTransport;
|
||||
try
|
||||
{
|
||||
// Make server socket
|
||||
_server = new TcpListener(IPAddress.Any, _port);
|
||||
_server.Server.NoDelay = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_server = null;
|
||||
throw new TTransportException("Could not create ServerSocket on port " + port + ".");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Listen()
|
||||
{
|
||||
// Make sure not to block on accept
|
||||
if (_server != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_server.Start();
|
||||
}
|
||||
catch (SocketException sx)
|
||||
{
|
||||
throw new TTransportException("Could not accept on listening socket: " + sx.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsClientPending()
|
||||
{
|
||||
return _server.Pending();
|
||||
}
|
||||
|
||||
protected override async Task<TClientTransport> AcceptImplementationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TClientTransport>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (_server == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TClientTransport tSocketTransport = null;
|
||||
var tcpClient = await _server.AcceptTcpClientAsync().ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
tSocketTransport = new TSocketClientTransport(tcpClient)
|
||||
{
|
||||
Timeout = _clientTimeout
|
||||
};
|
||||
|
||||
if (_useBufferedSockets)
|
||||
{
|
||||
tSocketTransport = new TBufferedClientTransport(tSocketTransport);
|
||||
}
|
||||
|
||||
if (_useFramedTransport)
|
||||
{
|
||||
tSocketTransport = new TFramedClientTransport(tSocketTransport);
|
||||
}
|
||||
|
||||
return tSocketTransport;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (tSocketTransport != null)
|
||||
{
|
||||
tSocketTransport.Dispose();
|
||||
}
|
||||
else // Otherwise, clean it up ourselves.
|
||||
{
|
||||
((IDisposable) tcpClient).Dispose();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new TTransportException(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_server != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new TTransportException("WARNING: Could not close server socket: " + ex);
|
||||
}
|
||||
_server = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,177 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Transports.Client;
|
||||
|
||||
namespace Thrift.Transports.Server
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TTlsServerSocketTransport : TServerTransport
|
||||
{
|
||||
private readonly RemoteCertificateValidationCallback _clientCertValidator;
|
||||
private readonly int _clientTimeout = 0;
|
||||
private readonly LocalCertificateSelectionCallback _localCertificateSelectionCallback;
|
||||
private readonly int _port;
|
||||
private readonly X509Certificate2 _serverCertificate;
|
||||
private readonly SslProtocols _sslProtocols;
|
||||
private readonly bool _useBufferedSockets;
|
||||
private readonly bool _useFramedTransport;
|
||||
private TcpListener _server;
|
||||
|
||||
public TTlsServerSocketTransport(int port, X509Certificate2 certificate)
|
||||
: this(port, false, certificate)
|
||||
{
|
||||
}
|
||||
|
||||
public TTlsServerSocketTransport(
|
||||
int port,
|
||||
bool useBufferedSockets,
|
||||
X509Certificate2 certificate,
|
||||
RemoteCertificateValidationCallback clientCertValidator = null,
|
||||
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
|
||||
SslProtocols sslProtocols = SslProtocols.Tls12)
|
||||
: this(port, useBufferedSockets, false, certificate,
|
||||
clientCertValidator, localCertificateSelectionCallback, sslProtocols)
|
||||
{
|
||||
}
|
||||
|
||||
public TTlsServerSocketTransport(
|
||||
int port,
|
||||
bool useBufferedSockets,
|
||||
bool useFramedTransport,
|
||||
X509Certificate2 certificate,
|
||||
RemoteCertificateValidationCallback clientCertValidator = null,
|
||||
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
|
||||
SslProtocols sslProtocols = SslProtocols.Tls12)
|
||||
{
|
||||
if (!certificate.HasPrivateKey)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown,
|
||||
"Your server-certificate needs to have a private key");
|
||||
}
|
||||
|
||||
_port = port;
|
||||
_serverCertificate = certificate;
|
||||
_useBufferedSockets = useBufferedSockets;
|
||||
_useFramedTransport = useFramedTransport;
|
||||
_clientCertValidator = clientCertValidator;
|
||||
_localCertificateSelectionCallback = localCertificateSelectionCallback;
|
||||
_sslProtocols = sslProtocols;
|
||||
|
||||
try
|
||||
{
|
||||
// Create server socket
|
||||
_server = new TcpListener(IPAddress.Any, _port);
|
||||
_server.Server.NoDelay = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_server = null;
|
||||
throw new TTransportException($"Could not create ServerSocket on port {port}.");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Listen()
|
||||
{
|
||||
// Make sure accept is not blocking
|
||||
if (_server != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_server.Start();
|
||||
}
|
||||
catch (SocketException sx)
|
||||
{
|
||||
throw new TTransportException($"Could not accept on listening socket: {sx.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsClientPending()
|
||||
{
|
||||
return _server.Pending();
|
||||
}
|
||||
|
||||
protected override async Task<TClientTransport> AcceptImplementationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<TClientTransport>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (_server == null)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var client = await _server.AcceptTcpClientAsync().ConfigureAwait(false);
|
||||
client.SendTimeout = client.ReceiveTimeout = _clientTimeout;
|
||||
|
||||
//wrap the client in an SSL Socket passing in the SSL cert
|
||||
var tTlsSocket = new TTlsSocketClientTransport(client, _serverCertificate, true, _clientCertValidator,
|
||||
_localCertificateSelectionCallback, _sslProtocols);
|
||||
|
||||
await tTlsSocket.SetupTlsAsync().ConfigureAwait(false);
|
||||
|
||||
TClientTransport trans = tTlsSocket;
|
||||
|
||||
if (_useBufferedSockets)
|
||||
{
|
||||
trans = new TBufferedClientTransport(trans);
|
||||
}
|
||||
|
||||
if (_useFramedTransport)
|
||||
{
|
||||
trans = new TFramedClientTransport(trans);
|
||||
}
|
||||
|
||||
return trans;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new TTransportException(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_server != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new TTransportException($"WARNING: Could not close server socket: {ex}");
|
||||
}
|
||||
|
||||
_server = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,179 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports
|
||||
{
|
||||
//TODO: think about client info
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public abstract class TClientTransport : IDisposable
|
||||
{
|
||||
//TODO: think how to avoid peek byte
|
||||
private readonly byte[] _peekBuffer = new byte[1];
|
||||
private bool _hasPeekByte;
|
||||
public abstract bool IsOpen { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public async Task<bool> PeekAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
//If we already have a byte read but not consumed, do nothing.
|
||||
if (_hasPeekByte)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//If transport closed we can't peek.
|
||||
if (!IsOpen)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Try to read one byte. If succeeds we will need to store it for the next read.
|
||||
try
|
||||
{
|
||||
var bytes = await ReadAsync(_peekBuffer, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
if (bytes == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_hasPeekByte = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual async Task OpenAsync()
|
||||
{
|
||||
await OpenAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task OpenAsync(CancellationToken cancellationToken);
|
||||
|
||||
public abstract void Close();
|
||||
|
||||
protected static void ValidateBufferArgs(byte[] buffer, int offset, int length)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(buffer));
|
||||
}
|
||||
|
||||
if (offset < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(offset), "Buffer offset is smaller than zero.");
|
||||
}
|
||||
|
||||
if (length < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(length), "Buffer length is smaller than zero.");
|
||||
}
|
||||
|
||||
if (offset + length > buffer.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(buffer), "Not enough data.");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<int> ReadAsync(byte[] buffer, int offset, int length)
|
||||
{
|
||||
return await ReadAsync(buffer, offset, length, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task<int> ReadAllAsync(byte[] buffer, int offset, int length)
|
||||
{
|
||||
return await ReadAllAsync(buffer, offset, length, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task<int> ReadAllAsync(byte[] buffer, int offset, int length,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ValidateBufferArgs(buffer, offset, length);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<int>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var retrieved = 0;
|
||||
|
||||
//If we previously peeked a byte, we need to use that first.
|
||||
if (_hasPeekByte)
|
||||
{
|
||||
buffer[offset + retrieved++] = _peekBuffer[0];
|
||||
_hasPeekByte = false;
|
||||
}
|
||||
|
||||
while (retrieved < length)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return await Task.FromCanceled<int>(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var returnedCount = await ReadAsync(buffer, offset + retrieved, length - retrieved, cancellationToken).ConfigureAwait(false);
|
||||
if (returnedCount <= 0)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.EndOfFile,
|
||||
"Cannot read, Remote side has closed");
|
||||
}
|
||||
retrieved += returnedCount;
|
||||
}
|
||||
return retrieved;
|
||||
}
|
||||
|
||||
public virtual async Task WriteAsync(byte[] buffer)
|
||||
{
|
||||
await WriteAsync(buffer, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task WriteAsync(byte[] buffer, CancellationToken cancellationToken)
|
||||
{
|
||||
await WriteAsync(buffer, 0, buffer.Length, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual async Task WriteAsync(byte[] buffer, int offset, int length)
|
||||
{
|
||||
await WriteAsync(buffer, offset, length, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken);
|
||||
|
||||
public virtual async Task FlushAsync()
|
||||
{
|
||||
await FlushAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task FlushAsync(CancellationToken cancellationToken);
|
||||
|
||||
protected abstract void Dispose(bool disposing);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Thrift.Transports
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public abstract class TServerTransport
|
||||
{
|
||||
public abstract void Listen();
|
||||
public abstract void Close();
|
||||
public abstract bool IsClientPending();
|
||||
|
||||
protected virtual async Task<TClientTransport> AcceptImplementationAsync()
|
||||
{
|
||||
return await AcceptImplementationAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected abstract Task<TClientTransport> AcceptImplementationAsync(CancellationToken cancellationToken);
|
||||
|
||||
public async Task<TClientTransport> AcceptAsync()
|
||||
{
|
||||
return await AcceptAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<TClientTransport> AcceptAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var transport = await AcceptImplementationAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (transport == null)
|
||||
{
|
||||
throw new TTransportException($"{nameof(AcceptImplementationAsync)} should not return null");
|
||||
}
|
||||
|
||||
return transport;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Transports
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TTransportException : TException
|
||||
{
|
||||
public enum ExceptionType
|
||||
{
|
||||
Unknown,
|
||||
NotOpen,
|
||||
AlreadyOpen,
|
||||
TimedOut,
|
||||
EndOfFile,
|
||||
Interrupted
|
||||
}
|
||||
|
||||
protected ExceptionType ExType;
|
||||
|
||||
public TTransportException()
|
||||
{
|
||||
}
|
||||
|
||||
public TTransportException(ExceptionType exType)
|
||||
: this()
|
||||
{
|
||||
ExType = exType;
|
||||
}
|
||||
|
||||
public TTransportException(ExceptionType exType, string message)
|
||||
: base(message)
|
||||
{
|
||||
ExType = exType;
|
||||
}
|
||||
|
||||
public TTransportException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public ExceptionType Type => ExType;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
// 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
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
namespace Thrift.Transports
|
||||
{
|
||||
/// <summary>
|
||||
/// From Mark Slee & Aditya Agarwal of Facebook:
|
||||
/// Factory class used to create wrapped instance of Transports.
|
||||
/// This is used primarily in servers, which get Transports from
|
||||
/// a ServerTransport and then may want to mutate them (i.e. create
|
||||
/// a BufferedTransport from the underlying base transport).
|
||||
/// </summary>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class TTransportFactory
|
||||
{
|
||||
public virtual TClientTransport GetTransport(TClientTransport trans)
|
||||
{
|
||||
return trans;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,12 @@ using System.Runtime.CompilerServices;
|
|||
|
||||
#if SIGNED
|
||||
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Jaeger.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
|
||||
[assembly: InternalsVisibleTo("Benchmarks, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
|
||||
|
||||
// Used by Moq.
|
||||
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
|
||||
#else
|
||||
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Jaeger.Tests")]
|
||||
[assembly: InternalsVisibleTo("Benchmarks")]
|
||||
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2] // Used by Moq.
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -16,17 +16,19 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using OpenTelemetry.Trace.Export;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
namespace OpenTelemetry.Exporter.Jaeger
|
||||
{
|
||||
public interface IJaegerUdpBatcher : IDisposable
|
||||
{
|
||||
// TODO: Internal Thrift type shouldn't really be on public interface.
|
||||
Process Process { get; }
|
||||
|
||||
Task<int> AppendAsync(JaegerSpan span, CancellationToken cancellationToken);
|
||||
ValueTask<int> AppendAsync(SpanData span, CancellationToken cancellationToken);
|
||||
|
||||
Task<int> CloseAsync(CancellationToken cancellationToken);
|
||||
ValueTask<int> CloseAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task<int> FlushAsync(CancellationToken cancellationToken);
|
||||
ValueTask<int> FlushAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,22 +19,24 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class Batch : TAbstractBase
|
||||
internal class Batch : TUnionBase
|
||||
{
|
||||
public Batch(Process process, List<JaegerSpan> spans = null)
|
||||
public Batch(Process process, IEnumerable<JaegerSpan> spans = null)
|
||||
{
|
||||
this.Process = process ?? throw new ArgumentNullException(nameof(process));
|
||||
this.Spans = spans ?? new List<JaegerSpan>();
|
||||
this.Spans = spans;
|
||||
}
|
||||
|
||||
public Process Process { get; }
|
||||
|
||||
public List<JaegerSpan> Spans { get; }
|
||||
public IEnumerable<JaegerSpan> Spans { get; set; }
|
||||
|
||||
internal List<BufferWriterMemory> SpanMessages { get; set; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
@ -60,10 +62,12 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
field.Type = TType.List;
|
||||
field.ID = 2;
|
||||
|
||||
var spans = this.Spans ?? Enumerable.Empty<JaegerSpan>();
|
||||
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
{
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, this.Spans.Count()), cancellationToken);
|
||||
foreach (JaegerSpan s in this.Spans)
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, spans.Count()), cancellationToken);
|
||||
foreach (var s in spans)
|
||||
{
|
||||
await s.WriteAsync(oprot, cancellationToken);
|
||||
}
|
||||
|
|
@ -91,5 +95,53 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
sb.Append(")");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
internal static async Task WriteAsync(byte[] processMessage, IEnumerable<BufferWriterMemory> spanMessages, TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
oprot.IncrementRecursionDepth();
|
||||
try
|
||||
{
|
||||
var struc = new TStruct("Batch");
|
||||
|
||||
await oprot.WriteStructBeginAsync(struc, cancellationToken);
|
||||
|
||||
var field = new TField
|
||||
{
|
||||
Name = "process",
|
||||
Type = TType.Struct,
|
||||
ID = 1,
|
||||
};
|
||||
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
await oprot.Transport.WriteAsync(processMessage, cancellationToken);
|
||||
await oprot.WriteFieldEndAsync(cancellationToken);
|
||||
|
||||
field.Name = "spans";
|
||||
field.Type = TType.List;
|
||||
field.ID = 2;
|
||||
|
||||
var spans = spanMessages ?? Enumerable.Empty<BufferWriterMemory>();
|
||||
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
{
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, spans.Count()), cancellationToken);
|
||||
|
||||
foreach (var s in spans)
|
||||
{
|
||||
await oprot.Transport.WriteAsync(s.BufferWriter.Buffer, s.Offset, s.Count, cancellationToken);
|
||||
}
|
||||
|
||||
await oprot.WriteListEndAsync(cancellationToken);
|
||||
}
|
||||
|
||||
await oprot.WriteFieldEndAsync(cancellationToken);
|
||||
await oprot.WriteFieldStopAsync(cancellationToken);
|
||||
await oprot.WriteStructEndAsync(cancellationToken);
|
||||
}
|
||||
finally
|
||||
{
|
||||
oprot.DecrementRecursionDepth();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
// <copyright file="BufferWriter.cs" company="OpenTelemetry Authors">
|
||||
// Copyright 2018, OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
internal sealed class BufferWriter
|
||||
{
|
||||
private int index;
|
||||
|
||||
public BufferWriter(int initialCapacity)
|
||||
{
|
||||
if (initialCapacity < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(initialCapacity));
|
||||
}
|
||||
|
||||
this.Buffer = new byte[initialCapacity];
|
||||
}
|
||||
|
||||
public byte[] Buffer { get; private set; }
|
||||
|
||||
public BufferWriterMemory GetMemory(int length)
|
||||
{
|
||||
this.CheckAndResizeBuffer(length);
|
||||
var memory = new BufferWriterMemory(this, this.index, length);
|
||||
this.index += length;
|
||||
return memory;
|
||||
}
|
||||
|
||||
public void Clear() => this.index = 0;
|
||||
|
||||
private void CheckAndResizeBuffer(int length)
|
||||
{
|
||||
int availableSpace = this.Buffer.Length - this.index;
|
||||
|
||||
if (length > availableSpace)
|
||||
{
|
||||
int growBy = Math.Max(length, this.Buffer.Length);
|
||||
|
||||
int newSize = checked(this.Buffer.Length + growBy);
|
||||
|
||||
var previousBuffer = this.Buffer.AsSpan(0, this.index);
|
||||
|
||||
this.Buffer = new byte[newSize];
|
||||
|
||||
previousBuffer.CopyTo(this.Buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
// <copyright file="BufferWriterMemory.cs" company="OpenTelemetry Authors">
|
||||
// Copyright 2018, OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
internal readonly struct BufferWriterMemory
|
||||
{
|
||||
public BufferWriterMemory(BufferWriter bufferWriter, int offset, int count)
|
||||
{
|
||||
this.BufferWriter = bufferWriter;
|
||||
this.Offset = offset;
|
||||
this.Count = count;
|
||||
}
|
||||
|
||||
public BufferWriter BufferWriter { get; }
|
||||
|
||||
public int Offset { get; }
|
||||
|
||||
public int Count { get; }
|
||||
|
||||
public BufferWriterMemory Expand(int length)
|
||||
{
|
||||
return new BufferWriterMemory(this.BufferWriter, this.Offset, this.Count + length);
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
var array = new byte[this.Count];
|
||||
Array.Copy(this.BufferWriter.Buffer, this.Offset, array, 0, this.Count);
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,20 +13,18 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class EmitBatchArgs : TAbstractBase
|
||||
internal class EmitBatchArgs : TUnionBase
|
||||
{
|
||||
public EmitBatchArgs()
|
||||
{
|
||||
}
|
||||
|
||||
public Batch Batch { get; set; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
|
|
@ -72,5 +70,33 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
sb.Append(")");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
internal static async Task WriteAsync(byte[] processMessage, IEnumerable<BufferWriterMemory> spanMessages, TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
oprot.IncrementRecursionDepth();
|
||||
try
|
||||
{
|
||||
var struc = new TStruct("emitBatch_args");
|
||||
await oprot.WriteStructBeginAsync(struc, cancellationToken);
|
||||
|
||||
var field = new TField
|
||||
{
|
||||
Name = "batch",
|
||||
Type = TType.Struct,
|
||||
ID = 1,
|
||||
};
|
||||
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
await Batch.WriteAsync(processMessage, spanMessages, oprot, cancellationToken);
|
||||
await oprot.WriteFieldEndAsync(cancellationToken);
|
||||
|
||||
await oprot.WriteFieldStopAsync(cancellationToken);
|
||||
await oprot.WriteStructEndAsync(cancellationToken);
|
||||
}
|
||||
finally
|
||||
{
|
||||
oprot.DecrementRecursionDepth();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// <copyright file="IJaegerUdpClient.cs" company="OpenTelemetry Authors">
|
||||
// <copyright file="IJaegerClient.cs" company="OpenTelemetry Authors">
|
||||
// Copyright 2018, OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
|
@ -15,11 +15,12 @@
|
|||
// </copyright>
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public interface IJaegerUdpClient : IDisposable
|
||||
internal interface IJaegerClient : IDisposable
|
||||
{
|
||||
bool Connected { get; }
|
||||
|
||||
|
|
@ -29,6 +30,8 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
void Close();
|
||||
|
||||
Task<int> SendAsync(byte[] datagram, int bytes);
|
||||
ValueTask<int> SendAsync(byte[] buffer, CancellationToken cancellationToken = default);
|
||||
|
||||
ValueTask<int> SendAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
|
|
@ -13,33 +13,31 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Transports;
|
||||
using Thrift.Transport;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class InMemoryTransport : TClientTransport
|
||||
internal class InMemoryTransport : TTransport
|
||||
{
|
||||
private readonly MemoryStream byteStream;
|
||||
private bool isDisposed;
|
||||
private readonly BufferWriter bufferWriter;
|
||||
private BufferWriterMemory? buffer;
|
||||
|
||||
public InMemoryTransport()
|
||||
public InMemoryTransport(int initialCapacity = 512)
|
||||
{
|
||||
this.byteStream = new MemoryStream();
|
||||
this.bufferWriter = new BufferWriter(initialCapacity);
|
||||
}
|
||||
|
||||
public override bool IsOpen => true;
|
||||
|
||||
public override Task OpenAsync(CancellationToken cancellationToken)
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
|
|
@ -47,42 +45,71 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
// do nothing
|
||||
}
|
||||
|
||||
public override Task<int> ReadAsync(
|
||||
byte[] buffer,
|
||||
int offset,
|
||||
int length,
|
||||
CancellationToken cancellationToken) => this.byteStream.ReadAsync(buffer, offset, length, cancellationToken);
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, CancellationToken cancellationToken) => this.byteStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) => this.byteStream.WriteAsync(buffer, offset, length, cancellationToken);
|
||||
|
||||
public override Task FlushAsync(CancellationToken cancellationToken)
|
||||
public override ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
var memory = this.bufferWriter.GetMemory(length);
|
||||
|
||||
var span = new ReadOnlySpan<byte>(buffer, offset, length);
|
||||
span.CopyTo(new Span<byte>(memory.BufferWriter.Buffer, memory.Offset, memory.Count));
|
||||
|
||||
if (!this.buffer.HasValue)
|
||||
{
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
this.buffer = memory;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Resize if we already had a window into the current buffer.
|
||||
this.buffer = this.buffer.Value.Expand(memory.Count);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public byte[] GetBuffer() => this.byteStream.ToArray();
|
||||
|
||||
public void Reset() => this.byteStream.SetLength(0);
|
||||
|
||||
// IDisposable
|
||||
protected override void Dispose(bool disposing)
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!this.isDisposed)
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
this.byteStream?.Dispose();
|
||||
}
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
if (!this.buffer.HasValue)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
this.isDisposed = true;
|
||||
var buffer = this.buffer.Value.ToArray();
|
||||
this.buffer = null;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public BufferWriterMemory ToBuffer()
|
||||
{
|
||||
if (!this.buffer.HasValue)
|
||||
{
|
||||
return new BufferWriterMemory(this.bufferWriter, 0, 0);
|
||||
}
|
||||
|
||||
var buffer = this.buffer.Value;
|
||||
this.buffer = null;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
this.buffer = null;
|
||||
this.bufferWriter.Clear();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public struct Int128
|
||||
internal readonly struct Int128
|
||||
{
|
||||
public static Int128 Empty = default;
|
||||
|
||||
|
|
@ -57,8 +57,8 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
this.Low = BitConverter.IsLittleEndian ? longs[0] : longs[1];
|
||||
}
|
||||
|
||||
public long High { get; set; }
|
||||
public long High { get; }
|
||||
|
||||
public long Low { get; set; }
|
||||
public long Low { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ using OpenTelemetry.Trace.Export;
|
|||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public static class JaegerConversionExtensions
|
||||
internal static class JaegerConversionExtensions
|
||||
{
|
||||
private const string StatusCode = "ot.status_code";
|
||||
private const string StatusDescription = "ot.status_description";
|
||||
|
|
@ -56,9 +56,11 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
["db.instance"] = 4, // peer.service for Redis.
|
||||
};
|
||||
|
||||
private static readonly Dictionary<CanonicalCode, string> CanonicalCodeDictionary = new Dictionary<CanonicalCode, string>();
|
||||
|
||||
public static JaegerSpan ToJaegerSpan(this SpanData span)
|
||||
{
|
||||
var jaegerTags = new List<JaegerTag>();
|
||||
var jaegerTags = PooledList<JaegerTag>.Create();
|
||||
|
||||
Tuple<string, int> peerService = null;
|
||||
foreach (var label in span.Attributes)
|
||||
|
|
@ -72,7 +74,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
peerService = new Tuple<string, int>(tag.VStr, priority);
|
||||
}
|
||||
|
||||
jaegerTags.Add(tag);
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, tag);
|
||||
}
|
||||
|
||||
// Send peer.service for remote calls. If priority = 0 that means peer.service was already included.
|
||||
|
|
@ -80,12 +82,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
&& peerService != null
|
||||
&& peerService.Item2 > 0)
|
||||
{
|
||||
jaegerTags.Add(new JaegerTag
|
||||
{
|
||||
Key = "peer.service",
|
||||
VType = JaegerTagType.STRING,
|
||||
VStr = peerService.Item1,
|
||||
});
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, new JaegerTag("peer.service", JaegerTagType.STRING, vStr: peerService.Item1));
|
||||
}
|
||||
|
||||
// The Span.Kind must translate into a tag.
|
||||
|
|
@ -113,12 +110,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
if (spanKind != null)
|
||||
{
|
||||
jaegerTags.Add(new JaegerTag
|
||||
{
|
||||
Key = "span.kind",
|
||||
VType = JaegerTagType.STRING,
|
||||
VStr = spanKind,
|
||||
});
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, new JaegerTag("span.kind", JaegerTagType.STRING, vStr: spanKind));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -127,10 +119,10 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
switch (label.Key)
|
||||
{
|
||||
case Resource.LibraryNameKey:
|
||||
jaegerTags.Add(label.ToJaegerTag());
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, label.ToJaegerTag());
|
||||
break;
|
||||
case Resource.LibraryVersionKey:
|
||||
jaegerTags.Add(label.ToJaegerTag());
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, label.ToJaegerTag());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -139,38 +131,20 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
if (status.IsValid)
|
||||
{
|
||||
jaegerTags.Add(new JaegerTag
|
||||
if (!CanonicalCodeDictionary.TryGetValue(status.CanonicalCode, out string statusCode))
|
||||
{
|
||||
Key = StatusCode,
|
||||
VType = JaegerTagType.STRING,
|
||||
VStr = status.CanonicalCode.ToString(),
|
||||
});
|
||||
statusCode = status.CanonicalCode.ToString();
|
||||
CanonicalCodeDictionary.Add(status.CanonicalCode, statusCode);
|
||||
}
|
||||
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, new JaegerTag(StatusCode, JaegerTagType.STRING, vStr: statusCode));
|
||||
|
||||
if (status.Description != null)
|
||||
{
|
||||
jaegerTags.Add(new JaegerTag
|
||||
{
|
||||
Key = StatusDescription,
|
||||
VType = JaegerTagType.STRING,
|
||||
VStr = status.Description,
|
||||
});
|
||||
PooledList<JaegerTag>.Add(ref jaegerTags, new JaegerTag(StatusDescription, JaegerTagType.STRING, vStr: status.Description));
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<JaegerLog> jaegerLogs = null;
|
||||
|
||||
if (span.Events != null)
|
||||
{
|
||||
jaegerLogs = span.Events.Select(e => e.ToJaegerLog()).AsEnumerable();
|
||||
}
|
||||
|
||||
IEnumerable<JaegerSpanRef> refs = null;
|
||||
|
||||
if (span.Links != null)
|
||||
{
|
||||
refs = span.Links.Select(l => l.ToJaegerSpanRef()).Where(l => l != null).AsEnumerable();
|
||||
}
|
||||
|
||||
var traceId = Int128.Empty;
|
||||
var spanId = Int128.Empty;
|
||||
var parentSpanId = Int128.Empty;
|
||||
|
|
@ -182,20 +156,62 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
parentSpanId = new Int128(span.ParentSpanId);
|
||||
}
|
||||
|
||||
return new JaegerSpan
|
||||
return new JaegerSpan(
|
||||
peerServiceName: peerService?.Item1,
|
||||
traceIdLow: traceId.Low,
|
||||
traceIdHigh: traceId.High,
|
||||
spanId: spanId.Low,
|
||||
parentSpanId: parentSpanId.Low,
|
||||
operationName: span.Name,
|
||||
flags: (span.Context.TraceOptions & ActivityTraceFlags.Recorded) > 0 ? 0x1 : 0,
|
||||
startTime: ToEpochMicroseconds(span.StartTimestamp),
|
||||
duration: ToEpochMicroseconds(span.EndTimestamp) - ToEpochMicroseconds(span.StartTimestamp),
|
||||
references: span.Links.ToJaegerSpanRefs(),
|
||||
tags: jaegerTags,
|
||||
logs: span.Events.ToJaegerLogs());
|
||||
}
|
||||
|
||||
public static PooledList<JaegerSpanRef> ToJaegerSpanRefs(this IEnumerable<Link> links)
|
||||
{
|
||||
bool created = false;
|
||||
PooledList<JaegerSpanRef> references = default;
|
||||
|
||||
foreach (var link in links ?? Array.Empty<Link>())
|
||||
{
|
||||
TraceIdHigh = traceId.High,
|
||||
TraceIdLow = traceId.Low,
|
||||
SpanId = spanId.Low,
|
||||
ParentSpanId = parentSpanId.Low,
|
||||
OperationName = span.Name,
|
||||
References = refs,
|
||||
Flags = (span.Context.TraceOptions & ActivityTraceFlags.Recorded) > 0 ? 0x1 : 0,
|
||||
StartTime = ToEpochMicroseconds(span.StartTimestamp),
|
||||
Duration = ToEpochMicroseconds(span.EndTimestamp) - ToEpochMicroseconds(span.StartTimestamp),
|
||||
JaegerTags = jaegerTags,
|
||||
Logs = jaegerLogs,
|
||||
};
|
||||
if (!created)
|
||||
{
|
||||
references = PooledList<JaegerSpanRef>.Create();
|
||||
created = true;
|
||||
}
|
||||
|
||||
PooledList<JaegerSpanRef>.Add(ref references, link.ToJaegerSpanRef());
|
||||
}
|
||||
|
||||
return references;
|
||||
}
|
||||
|
||||
public static PooledList<JaegerLog> ToJaegerLogs(this IEnumerable<Event> events)
|
||||
{
|
||||
bool created = false;
|
||||
PooledList<JaegerLog> logs = default;
|
||||
|
||||
foreach (var e in events ?? Array.Empty<Event>())
|
||||
{
|
||||
if (e == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!created)
|
||||
{
|
||||
logs = PooledList<JaegerLog>.Create();
|
||||
created = true;
|
||||
}
|
||||
|
||||
PooledList<JaegerLog>.Add(ref logs, e.ToJaegerLog());
|
||||
}
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
public static JaegerTag ToJaegerTag(this KeyValuePair<string, object> attribute)
|
||||
|
|
@ -203,36 +219,37 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
switch (attribute.Value)
|
||||
{
|
||||
case string s:
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.STRING, VStr = s };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.STRING, vStr: s);
|
||||
case int i:
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.LONG, VLong = Convert.ToInt64(i) };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(i));
|
||||
case long l:
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.LONG, VLong = l };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.LONG, vLong: l);
|
||||
case float f:
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.DOUBLE, VDouble = Convert.ToDouble(f) };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.DOUBLE, vDouble: Convert.ToDouble(f));
|
||||
case double d:
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.DOUBLE, VDouble = d };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.DOUBLE, vDouble: d);
|
||||
case bool b:
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.BOOL, VBool = b };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.BOOL, vBool: b);
|
||||
}
|
||||
|
||||
return new JaegerTag { Key = attribute.Key, VType = JaegerTagType.STRING, VStr = attribute.Value.ToString() };
|
||||
return new JaegerTag(attribute.Key, JaegerTagType.STRING, vStr: attribute.Value.ToString());
|
||||
}
|
||||
|
||||
public static JaegerLog ToJaegerLog(this Event timedEvent)
|
||||
{
|
||||
var tags = timedEvent.Attributes.Select(a => a.ToJaegerTag()).ToList();
|
||||
var tags = PooledList<JaegerTag>.Create();
|
||||
|
||||
foreach (var attribute in timedEvent.Attributes)
|
||||
{
|
||||
PooledList<JaegerTag>.Add(ref tags, attribute.ToJaegerTag());
|
||||
}
|
||||
|
||||
// Matches what OpenTracing and OpenTelemetry defines as the event name.
|
||||
// https://github.com/opentracing/specification/blob/master/semantic_conventions.md#log-fields-table
|
||||
// https://github.com/open-telemetry/opentelemetry-specification/pull/397/files
|
||||
tags.Add(new JaegerTag { Key = "message", VType = JaegerTagType.STRING, VStr = timedEvent.Name });
|
||||
PooledList<JaegerTag>.Add(ref tags, new JaegerTag("message", JaegerTagType.STRING, vStr: timedEvent.Name));
|
||||
|
||||
return new JaegerLog
|
||||
{
|
||||
Timestamp = timedEvent.Timestamp.ToEpochMicroseconds(),
|
||||
Fields = tags,
|
||||
};
|
||||
return new JaegerLog(timedEvent.Timestamp.ToEpochMicroseconds(), tags);
|
||||
}
|
||||
|
||||
public static JaegerSpanRef ToJaegerSpanRef(this Link link)
|
||||
|
|
@ -246,13 +263,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
spanId = new Int128(link.Context.SpanId);
|
||||
}
|
||||
|
||||
return new JaegerSpanRef
|
||||
{
|
||||
TraceIdHigh = traceId.High,
|
||||
TraceIdLow = traceId.Low,
|
||||
SpanId = spanId.Low,
|
||||
RefType = JaegerSpanRefType.CHILD_OF,
|
||||
};
|
||||
return new JaegerSpanRef(JaegerSpanRefType.CHILD_OF, traceId.Low, traceId.High, spanId.Low);
|
||||
}
|
||||
|
||||
public static long ToEpochMicroseconds(this DateTimeOffset timestamp)
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ using System;
|
|||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerExporterException : Exception
|
||||
internal class JaegerExporterException : Exception
|
||||
{
|
||||
public JaegerExporterException(string message, Exception originalException)
|
||||
: base(message, originalException)
|
||||
|
|
|
|||
|
|
@ -13,32 +13,26 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerLog : TAbstractBase
|
||||
internal readonly struct JaegerLog : TUnionBase
|
||||
{
|
||||
public JaegerLog()
|
||||
{
|
||||
}
|
||||
|
||||
public JaegerLog(long timestamp, IEnumerable<JaegerTag> fields)
|
||||
: this()
|
||||
public JaegerLog(long timestamp, in PooledList<JaegerTag> fields)
|
||||
{
|
||||
this.Timestamp = timestamp;
|
||||
this.Fields = fields ?? Enumerable.Empty<JaegerTag>();
|
||||
this.Fields = fields;
|
||||
}
|
||||
|
||||
public long Timestamp { get; set; }
|
||||
public long Timestamp { get; }
|
||||
|
||||
public IEnumerable<JaegerTag> Fields { get; set; }
|
||||
public PooledList<JaegerTag> Fields { get; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
@ -65,11 +59,11 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
{
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, this.Fields.Count()), cancellationToken);
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, this.Fields.Count), cancellationToken);
|
||||
|
||||
foreach (JaegerTag jt in this.Fields)
|
||||
for (int i = 0; i < this.Fields.Count; i++)
|
||||
{
|
||||
await jt.WriteAsync(oprot, cancellationToken);
|
||||
await this.Fields[i].WriteAsync(oprot, cancellationToken);
|
||||
}
|
||||
|
||||
await oprot.WriteListEndAsync(cancellationToken);
|
||||
|
|
|
|||
|
|
@ -13,25 +13,31 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerSpan : TAbstractBase
|
||||
internal readonly struct JaegerSpan : TUnionBase
|
||||
{
|
||||
public JaegerSpan()
|
||||
{
|
||||
}
|
||||
|
||||
public JaegerSpan(long traceIdLow, long traceIdHigh, long spanId, long parentSpanId, string operationName, int flags, long startTime, long duration)
|
||||
: this()
|
||||
public JaegerSpan(
|
||||
string peerServiceName,
|
||||
long traceIdLow,
|
||||
long traceIdHigh,
|
||||
long spanId,
|
||||
long parentSpanId,
|
||||
string operationName,
|
||||
int flags,
|
||||
long startTime,
|
||||
long duration,
|
||||
in PooledList<JaegerSpanRef> references,
|
||||
in PooledList<JaegerTag> tags,
|
||||
in PooledList<JaegerLog> logs)
|
||||
{
|
||||
this.PeerServiceName = peerServiceName;
|
||||
this.TraceIdLow = traceIdLow;
|
||||
this.TraceIdHigh = traceIdHigh;
|
||||
this.SpanId = spanId;
|
||||
|
|
@ -40,29 +46,34 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
this.Flags = flags;
|
||||
this.StartTime = startTime;
|
||||
this.Duration = duration;
|
||||
this.References = references;
|
||||
this.Tags = tags;
|
||||
this.Logs = logs;
|
||||
}
|
||||
|
||||
public long TraceIdLow { get; set; }
|
||||
public string PeerServiceName { get; }
|
||||
|
||||
public long TraceIdHigh { get; set; }
|
||||
public long TraceIdLow { get; }
|
||||
|
||||
public long SpanId { get; set; }
|
||||
public long TraceIdHigh { get; }
|
||||
|
||||
public long ParentSpanId { get; set; }
|
||||
public long SpanId { get; }
|
||||
|
||||
public string OperationName { get; set; }
|
||||
public long ParentSpanId { get; }
|
||||
|
||||
public IEnumerable<JaegerSpanRef> References { get; set; }
|
||||
public string OperationName { get; }
|
||||
|
||||
public int Flags { get; set; }
|
||||
public PooledList<JaegerSpanRef> References { get; }
|
||||
|
||||
public long StartTime { get; set; }
|
||||
public int Flags { get; }
|
||||
|
||||
public long Duration { get; set; }
|
||||
public long StartTime { get; }
|
||||
|
||||
public IEnumerable<JaegerTag> JaegerTags { get; set; }
|
||||
public long Duration { get; }
|
||||
|
||||
public IEnumerable<JaegerLog> Logs { get; set; }
|
||||
public PooledList<JaegerTag> Tags { get; }
|
||||
|
||||
public PooledList<JaegerLog> Logs { get; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
@ -115,18 +126,18 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
await oprot.WriteStringAsync(this.OperationName, cancellationToken);
|
||||
await oprot.WriteFieldEndAsync(cancellationToken);
|
||||
|
||||
if (this.References is IEnumerable<JaegerSpanRef> references)
|
||||
if (!this.References.IsEmpty)
|
||||
{
|
||||
field.Name = "references";
|
||||
field.Type = TType.List;
|
||||
field.ID = 6;
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
{
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, references.Count()), cancellationToken);
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, this.References.Count), cancellationToken);
|
||||
|
||||
foreach (JaegerSpanRef sr in references)
|
||||
for (int i = 0; i < this.References.Count; i++)
|
||||
{
|
||||
await sr.WriteAsync(oprot, cancellationToken);
|
||||
await this.References[i].WriteAsync(oprot, cancellationToken);
|
||||
}
|
||||
|
||||
await oprot.WriteListEndAsync(cancellationToken);
|
||||
|
|
@ -159,7 +170,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
await oprot.WriteI64Async(this.Duration, cancellationToken);
|
||||
await oprot.WriteFieldEndAsync(cancellationToken);
|
||||
|
||||
if (this.JaegerTags is IEnumerable<JaegerTag> jaegerTags)
|
||||
if (!this.Tags.IsEmpty)
|
||||
{
|
||||
field.Name = "JaegerTags";
|
||||
field.Type = TType.List;
|
||||
|
|
@ -167,11 +178,11 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
{
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, jaegerTags.Count()), cancellationToken);
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, this.Tags.Count), cancellationToken);
|
||||
|
||||
foreach (JaegerTag jt in jaegerTags)
|
||||
for (int i = 0; i < this.Tags.Count; i++)
|
||||
{
|
||||
await jt.WriteAsync(oprot, cancellationToken);
|
||||
await this.Tags[i].WriteAsync(oprot, cancellationToken);
|
||||
}
|
||||
|
||||
await oprot.WriteListEndAsync(cancellationToken);
|
||||
|
|
@ -180,18 +191,18 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
await oprot.WriteFieldEndAsync(cancellationToken);
|
||||
}
|
||||
|
||||
if (this.Logs is IEnumerable<JaegerLog> logs)
|
||||
if (!this.Logs.IsEmpty)
|
||||
{
|
||||
field.Name = "logs";
|
||||
field.Type = TType.List;
|
||||
field.ID = 11;
|
||||
await oprot.WriteFieldBeginAsync(field, cancellationToken);
|
||||
{
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, logs.Count()), cancellationToken);
|
||||
await oprot.WriteListBeginAsync(new TList(TType.Struct, this.Logs.Count), cancellationToken);
|
||||
|
||||
foreach (JaegerLog jl in logs)
|
||||
for (int i = 0; i < this.Logs.Count; i++)
|
||||
{
|
||||
await jl.WriteAsync(oprot, cancellationToken);
|
||||
await this.Logs[i].WriteAsync(oprot, cancellationToken);
|
||||
}
|
||||
|
||||
await oprot.WriteListEndAsync(cancellationToken);
|
||||
|
|
@ -209,6 +220,21 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
}
|
||||
}
|
||||
|
||||
public void Return()
|
||||
{
|
||||
this.References.Return();
|
||||
this.Tags.Return();
|
||||
if (!this.Logs.IsEmpty)
|
||||
{
|
||||
for (int i = 0; i < this.Logs.Count; i++)
|
||||
{
|
||||
this.Logs[i].Fields.Return();
|
||||
}
|
||||
|
||||
this.Logs.Return();
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var sb = new StringBuilder("Span(");
|
||||
|
|
@ -222,10 +248,10 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
sb.Append(this.ParentSpanId);
|
||||
sb.Append(", OperationName: ");
|
||||
sb.Append(this.OperationName);
|
||||
if (this.References is IEnumerable<JaegerSpanRef> jaegerSpanRef)
|
||||
if (!this.References.IsEmpty)
|
||||
{
|
||||
sb.Append(", References: ");
|
||||
sb.Append(jaegerSpanRef);
|
||||
sb.Append(this.References);
|
||||
}
|
||||
|
||||
sb.Append(", Flags: ");
|
||||
|
|
@ -234,16 +260,16 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
sb.Append(this.StartTime);
|
||||
sb.Append(", Duration: ");
|
||||
sb.Append(this.Duration);
|
||||
if (this.JaegerTags is IEnumerable<JaegerTag> tags)
|
||||
if (!this.Tags.IsEmpty)
|
||||
{
|
||||
sb.Append(", JaegerTags: ");
|
||||
sb.Append(tags);
|
||||
sb.Append(this.Tags);
|
||||
}
|
||||
|
||||
if (this.Logs is IEnumerable<JaegerLog> logs)
|
||||
if (!this.Logs.IsEmpty)
|
||||
{
|
||||
sb.Append(", Logs: ");
|
||||
sb.Append(logs);
|
||||
sb.Append(this.Logs);
|
||||
}
|
||||
|
||||
sb.Append(")");
|
||||
|
|
|
|||
|
|
@ -16,19 +16,14 @@
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerSpanRef : TAbstractBase
|
||||
internal readonly struct JaegerSpanRef : TUnionBase
|
||||
{
|
||||
public JaegerSpanRef()
|
||||
{
|
||||
}
|
||||
|
||||
public JaegerSpanRef(JaegerSpanRefType refType, long traceIdLow, long traceIdHigh, long spanId)
|
||||
: this()
|
||||
{
|
||||
this.RefType = refType;
|
||||
this.TraceIdLow = traceIdLow;
|
||||
|
|
@ -36,13 +31,13 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
this.SpanId = spanId;
|
||||
}
|
||||
|
||||
public JaegerSpanRefType RefType { get; set; }
|
||||
public JaegerSpanRefType RefType { get; }
|
||||
|
||||
public long TraceIdLow { get; set; }
|
||||
public long TraceIdLow { get; }
|
||||
|
||||
public long TraceIdHigh { get; set; }
|
||||
public long TraceIdHigh { get; }
|
||||
|
||||
public long SpanId { get; set; }
|
||||
public long SpanId { get; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
/// <summary>
|
||||
/// Represents the different types of Jaeger Spans.
|
||||
/// </summary>
|
||||
public enum JaegerSpanRefType
|
||||
internal enum JaegerSpanRefType
|
||||
{
|
||||
/// <summary>
|
||||
/// A child span
|
||||
|
|
|
|||
|
|
@ -16,37 +16,45 @@
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerTag : TAbstractBase
|
||||
internal readonly struct JaegerTag : TUnionBase
|
||||
{
|
||||
public JaegerTag()
|
||||
{
|
||||
}
|
||||
|
||||
public JaegerTag(string key, JaegerTagType vType)
|
||||
: this()
|
||||
public JaegerTag(
|
||||
string key,
|
||||
JaegerTagType vType,
|
||||
string vStr = null,
|
||||
double? vDouble = null,
|
||||
bool? vBool = null,
|
||||
long? vLong = null,
|
||||
byte[] vBinary = null)
|
||||
{
|
||||
this.Key = key;
|
||||
this.VType = vType;
|
||||
|
||||
this.VStr = vStr;
|
||||
this.VDouble = vDouble;
|
||||
this.VBool = vBool;
|
||||
this.VLong = vLong;
|
||||
this.VBinary = vBinary;
|
||||
}
|
||||
|
||||
public string Key { get; set; }
|
||||
public string Key { get; }
|
||||
|
||||
public JaegerTagType VType { get; set; }
|
||||
public JaegerTagType VType { get; }
|
||||
|
||||
public string VStr { get; set; }
|
||||
public string VStr { get; }
|
||||
|
||||
public double? VDouble { get; set; }
|
||||
public double? VDouble { get; }
|
||||
|
||||
public bool? VBool { get; set; }
|
||||
public bool? VBool { get; }
|
||||
|
||||
public long? VLong { get; set; }
|
||||
public long? VLong { get; }
|
||||
|
||||
public byte[] VBinary { get; set; }
|
||||
public byte[] VBinary { get; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
/// <summary>
|
||||
/// Indicates the data type of a Jaeger tag.
|
||||
/// </summary>
|
||||
public enum JaegerTagType
|
||||
internal enum JaegerTagType
|
||||
{
|
||||
/// <summary>
|
||||
/// Tag contains a string
|
||||
|
|
|
|||
|
|
@ -14,15 +14,16 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerThriftClient : TBaseClient, IDisposable
|
||||
internal class JaegerThriftClient : TBaseClient, IDisposable
|
||||
{
|
||||
public JaegerThriftClient(TProtocol protocol)
|
||||
: this(protocol, protocol)
|
||||
|
|
@ -38,12 +39,22 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
{
|
||||
await this.OutputProtocol.WriteMessageBeginAsync(new TMessage("emitBatch", TMessageType.Oneway, this.SeqId), cancellationToken);
|
||||
|
||||
var args = new EmitBatchArgs();
|
||||
args.Batch = batch;
|
||||
var args = new EmitBatchArgs
|
||||
{
|
||||
Batch = batch,
|
||||
};
|
||||
|
||||
await args.WriteAsync(this.OutputProtocol, cancellationToken);
|
||||
await this.OutputProtocol.WriteMessageEndAsync(cancellationToken);
|
||||
await this.OutputProtocol.Transport.FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
internal async Task EmitBatchAsync(byte[] processMessage, IEnumerable<BufferWriterMemory> spanMessages, CancellationToken cancellationToken)
|
||||
{
|
||||
await this.OutputProtocol.WriteMessageBeginAsync(new TMessage("emitBatch", TMessageType.Oneway, this.SeqId), cancellationToken);
|
||||
await EmitBatchArgs.WriteAsync(processMessage, spanMessages, this.OutputProtocol, cancellationToken);
|
||||
await this.OutputProtocol.WriteMessageEndAsync(cancellationToken);
|
||||
await this.OutputProtocol.Transport.FlushAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,13 @@ using System.IO;
|
|||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Transports;
|
||||
using Thrift.Transport;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerThriftClientTransport : TClientTransport
|
||||
internal class JaegerThriftClientTransport : TTransport
|
||||
{
|
||||
private readonly IJaegerUdpClient udpClient;
|
||||
private readonly IJaegerClient client;
|
||||
private readonly MemoryStream byteStream;
|
||||
private bool isDisposed = false;
|
||||
|
||||
|
|
@ -33,69 +33,76 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
{
|
||||
}
|
||||
|
||||
public JaegerThriftClientTransport(string host, int port, MemoryStream stream, IJaegerUdpClient client)
|
||||
public JaegerThriftClientTransport(string host, int port, MemoryStream stream, IJaegerClient client)
|
||||
{
|
||||
this.byteStream = stream;
|
||||
this.udpClient = client;
|
||||
this.udpClient.Connect(host, port);
|
||||
this.client = client;
|
||||
this.client.Connect(host, port);
|
||||
}
|
||||
|
||||
public override bool IsOpen => this.udpClient.Connected;
|
||||
public override bool IsOpen => this.client.Connected;
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
this.udpClient.Close();
|
||||
this.client.Close();
|
||||
}
|
||||
|
||||
public override Task FlushAsync(CancellationToken cancellationToken)
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var bytes = this.byteStream.ToArray();
|
||||
|
||||
if (bytes.Length == 0)
|
||||
// GetBuffer returns the underlying storage, which saves an allocation over ToArray.
|
||||
if (!this.byteStream.TryGetBuffer(out var buffer))
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
buffer = new ArraySegment<byte>(this.byteStream.ToArray(), 0, (int)this.byteStream.Length);
|
||||
}
|
||||
|
||||
this.byteStream.SetLength(0);
|
||||
if (buffer.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return this.udpClient.SendAsync(bytes, bytes.Length);
|
||||
await this.client.SendAsync(buffer.Array, buffer.Offset, buffer.Count).ConfigureAwait(false);
|
||||
}
|
||||
catch (SocketException se)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown, $"Cannot flush because of socket exception. UDP Packet size was {bytes.Length}. Exception message: {se.Message}");
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown, $"Cannot flush because of socket exception. UDP Packet size was {buffer.Count}. Exception message: {se.Message}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new TTransportException(TTransportException.ExceptionType.Unknown, $"Cannot flush closed transport. {e.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.byteStream.SetLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
public override Task OpenAsync(CancellationToken cancellationToken)
|
||||
public override async Task OpenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// Do nothing
|
||||
return Task.CompletedTask;
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.FromCanceled(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
public override ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, CancellationToken cancellationToken)
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
return this.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
|
||||
}
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
|
||||
{
|
||||
return this.byteStream.WriteAsync(buffer, offset, length, cancellationToken);
|
||||
#if NETSTANDARD2_1
|
||||
await this.byteStream.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, length), cancellationToken).ConfigureAwait(false);
|
||||
#else
|
||||
await this.byteStream.WriteAsync(buffer, offset, length).ConfigureAwait(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{nameof(JaegerThriftClientTransport)}(Client={this.udpClient.RemoteEndPoint})";
|
||||
return $"{nameof(JaegerThriftClientTransport)}(Client={this.client.RemoteEndPoint})";
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
|
|
@ -103,7 +110,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
if (!this.isDisposed && disposing)
|
||||
{
|
||||
this.byteStream?.Dispose();
|
||||
this.udpClient?.Dispose();
|
||||
this.client?.Dispose();
|
||||
}
|
||||
|
||||
this.isDisposed = true;
|
||||
|
|
|
|||
|
|
@ -18,16 +18,20 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
|
||||
using OpenTelemetry.Trace.Export;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Transport;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerUdpBatcher : IJaegerUdpBatcher
|
||||
internal class JaegerUdpBatcher : IJaegerUdpBatcher
|
||||
{
|
||||
private readonly int maxPacketSize;
|
||||
private readonly ITProtocolFactory protocolFactory;
|
||||
private readonly JaegerThriftClientTransport clientTransport;
|
||||
private readonly TProtocolFactory protocolFactory;
|
||||
private readonly TTransport clientTransport;
|
||||
private readonly JaegerThriftClient thriftClient;
|
||||
private readonly InMemoryTransport memoryTransport;
|
||||
private readonly TProtocol memoryProtocol;
|
||||
|
||||
private readonly SemaphoreSlim flushLock = new SemaphoreSlim(1);
|
||||
private readonly TimeSpan maxFlushInterval;
|
||||
|
|
@ -38,7 +42,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
private bool disposedValue = false; // To detect redundant calls
|
||||
|
||||
public JaegerUdpBatcher(JaegerExporterOptions options)
|
||||
public JaegerUdpBatcher(JaegerExporterOptions options, TTransport clientTransport = null)
|
||||
{
|
||||
if (options is null)
|
||||
{
|
||||
|
|
@ -50,10 +54,13 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
options.MaxFlushInterval = TimeSpan.FromSeconds(10);
|
||||
}
|
||||
|
||||
this.maxPacketSize = (!options.MaxPacketSize.HasValue || options.MaxPacketSize == 0) ? JaegerExporterOptions.DefaultMaxPacketSize : options.MaxPacketSize.Value;
|
||||
this.maxPacketSize = (!options.MaxPacketSize.HasValue || options.MaxPacketSize <= 0) ? JaegerExporterOptions.DefaultMaxPacketSize : options.MaxPacketSize.Value;
|
||||
this.protocolFactory = new TCompactProtocol.Factory();
|
||||
this.clientTransport = new JaegerThriftClientTransport(options.AgentHost, options.AgentPort);
|
||||
this.thriftClient = new JaegerThriftClient(this.protocolFactory.GetProtocol(this.clientTransport));
|
||||
this.clientTransport = clientTransport ?? new JaegerThriftClientTransport(options.AgentHost, options.AgentPort);
|
||||
this.thriftClient = new JaegerThriftClient(this.protocolFactory.GetProtocol(this.clientTransport));
|
||||
this.memoryTransport = new InMemoryTransport(16000);
|
||||
this.memoryProtocol = this.protocolFactory.GetProtocol(this.memoryTransport);
|
||||
|
||||
this.Process = new Process(options.ServiceName, options.ProcessTags);
|
||||
|
||||
this.maxFlushInterval = options.MaxFlushInterval;
|
||||
|
|
@ -70,44 +77,51 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
};
|
||||
}
|
||||
|
||||
public Process Process { get; private set; }
|
||||
public Process Process { get; internal set; }
|
||||
|
||||
internal IDictionary<string, Batch> CurrentBatches { get; } = new Dictionary<string, Batch>();
|
||||
|
||||
public async Task<int> AppendAsync(JaegerSpan span, CancellationToken cancellationToken)
|
||||
{
|
||||
internal IDictionary<string, Batch> CurrentBatches { get; } = new Dictionary<string, Batch>();
|
||||
|
||||
public async ValueTask<int> AppendAsync(SpanData span, CancellationToken cancellationToken)
|
||||
{
|
||||
if (this.processCache == null)
|
||||
{
|
||||
this.Process.ByteSize = await this.GetSize(this.Process).ConfigureAwait(false);
|
||||
this.Process.Message = this.BuildThriftMessage(this.Process).ToArray();
|
||||
this.processCache = new Dictionary<string, Process>
|
||||
{
|
||||
[this.Process.ServiceName] = this.Process,
|
||||
};
|
||||
}
|
||||
|
||||
string spanServiceName = span.JaegerTags?.FirstOrDefault(t => t.Key == "peer.service")?.VStr ?? this.Process.ServiceName;
|
||||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
string spanServiceName = jaegerSpan.PeerServiceName ?? this.Process.ServiceName;
|
||||
|
||||
if (!this.processCache.TryGetValue(spanServiceName, out var spanProcess))
|
||||
{
|
||||
spanProcess = new Process(spanServiceName, this.Process.Tags);
|
||||
spanProcess.ByteSize = await this.GetSize(spanProcess).ConfigureAwait(false);
|
||||
spanProcess.Message = this.BuildThriftMessage(spanProcess).ToArray();
|
||||
this.processCache.Add(spanServiceName, spanProcess);
|
||||
}
|
||||
|
||||
int spanByteSize = await this.GetSize(span).ConfigureAwait(false);
|
||||
var spanMessage = this.BuildThriftMessage(jaegerSpan);
|
||||
|
||||
if (spanByteSize + spanProcess.ByteSize > this.maxPacketSize)
|
||||
jaegerSpan.Return();
|
||||
|
||||
if (spanMessage.Count + spanProcess.Message.Length > this.maxPacketSize)
|
||||
{
|
||||
throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanByteSize + spanProcess.ByteSize}, max = {this.maxPacketSize}", null);
|
||||
throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanMessage.Count + spanProcess.Message.Length}, max = {this.maxPacketSize}", null);
|
||||
}
|
||||
|
||||
int spanTotalBytesNeeded = spanByteSize;
|
||||
int spanTotalBytesNeeded = spanMessage.Count;
|
||||
if (!this.CurrentBatches.TryGetValue(spanServiceName, out var spanBatch))
|
||||
{
|
||||
spanBatch = new Batch(spanProcess);
|
||||
spanBatch = new Batch(spanProcess)
|
||||
{
|
||||
SpanMessages = new List<BufferWriterMemory>(),
|
||||
};
|
||||
this.CurrentBatches.Add(spanServiceName, spanBatch);
|
||||
|
||||
spanTotalBytesNeeded += spanProcess.ByteSize;
|
||||
spanTotalBytesNeeded += spanProcess.Message.Length;
|
||||
}
|
||||
|
||||
var flushedSpanCount = 0;
|
||||
|
|
@ -121,8 +135,8 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
flushedSpanCount = await this.FlushAsyncInternal(true, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Flushing effectively erases the spanBatch we were working on, so we have to rebuild it.
|
||||
spanBatch.Spans.Clear();
|
||||
spanTotalBytesNeeded = spanByteSize + spanProcess.ByteSize;
|
||||
spanBatch.SpanMessages.Clear();
|
||||
spanTotalBytesNeeded = spanMessage.Count + spanProcess.Message.Length;
|
||||
this.CurrentBatches.Add(spanServiceName, spanBatch);
|
||||
}
|
||||
else
|
||||
|
|
@ -131,7 +145,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
}
|
||||
|
||||
// add span to batch and wait for more spans
|
||||
spanBatch.Spans.Add(span);
|
||||
spanBatch.SpanMessages.Add(spanMessage);
|
||||
this.batchByteSize += spanTotalBytesNeeded;
|
||||
}
|
||||
finally
|
||||
|
|
@ -140,35 +154,43 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
}
|
||||
|
||||
return flushedSpanCount;
|
||||
}
|
||||
}
|
||||
|
||||
public ValueTask<int> FlushAsync(CancellationToken cancellationToken) => this.FlushAsyncInternal(false, cancellationToken);
|
||||
|
||||
public Task<int> FlushAsync(CancellationToken cancellationToken) => this.FlushAsyncInternal(false, cancellationToken);
|
||||
|
||||
public Task<int> CloseAsync(CancellationToken cancellationToken) => this.FlushAsyncInternal(false, cancellationToken);
|
||||
public ValueTask<int> CloseAsync(CancellationToken cancellationToken) => this.FlushAsyncInternal(false, cancellationToken);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in Dispose(bool disposing).
|
||||
this.Dispose(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected async Task SendAsync(IEnumerable<Batch> batches, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var batch in batches)
|
||||
{
|
||||
await this.thriftClient.EmitBatchAsync(batch, cancellationToken).ConfigureAwait(false);
|
||||
await this.thriftClient.EmitBatchAsync(batch.Process.Message, batch.SpanMessages, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JaegerExporterException($"Could not send {batches.Select(b => b.Spans.Count()).Sum()} spans", ex);
|
||||
throw new JaegerExporterException($"Could not send {batches.Select(b => b.SpanMessages.Count()).Sum()} spans", ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
{
|
||||
try
|
||||
{
|
||||
this.CloseAsync(CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
if (!this.disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
|
|
@ -176,13 +198,15 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
this.maxFlushIntervalTimer.Dispose();
|
||||
this.thriftClient.Dispose();
|
||||
this.clientTransport.Dispose();
|
||||
this.memoryProtocol.Dispose();
|
||||
this.flushLock.Dispose();
|
||||
}
|
||||
|
||||
this.disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> FlushAsyncInternal(bool lockAlreadyHeld, CancellationToken cancellationToken)
|
||||
private async ValueTask<int> FlushAsyncInternal(bool lockAlreadyHeld, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!lockAlreadyHeld)
|
||||
{
|
||||
|
|
@ -193,7 +217,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
{
|
||||
this.maxFlushIntervalTimer.Enabled = false;
|
||||
|
||||
int n = this.CurrentBatches.Values.Sum(b => b.Spans.Count);
|
||||
int n = this.CurrentBatches.Values.Sum(b => b.SpanMessages.Count);
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
|
|
@ -208,6 +232,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
{
|
||||
this.CurrentBatches.Clear();
|
||||
this.batchByteSize = 0;
|
||||
this.memoryTransport.Reset();
|
||||
}
|
||||
|
||||
return n;
|
||||
|
|
@ -221,13 +246,29 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
}
|
||||
}
|
||||
|
||||
private async Task<int> GetSize(TAbstractBase thriftBase)
|
||||
private BufferWriterMemory BuildThriftMessage(Process process)
|
||||
{
|
||||
using (var memoryTransport = new InMemoryTransport())
|
||||
{
|
||||
await thriftBase.WriteAsync(this.protocolFactory.GetProtocol(memoryTransport), CancellationToken.None).ConfigureAwait(false);
|
||||
return memoryTransport.GetBuffer().Length;
|
||||
var task = process.WriteAsync(this.memoryProtocol, CancellationToken.None);
|
||||
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
return this.memoryTransport.ToBuffer();
|
||||
}
|
||||
|
||||
// Prevents boxing of JaegerSpan struct.
|
||||
private BufferWriterMemory BuildThriftMessage(in JaegerSpan jaegerSpan)
|
||||
{
|
||||
var task = jaegerSpan.WriteAsync(this.memoryProtocol, CancellationToken.None);
|
||||
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
return this.memoryTransport.ToBuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,13 +13,15 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
public class JaegerUdpClient : IJaegerUdpClient
|
||||
internal class JaegerUdpClient : IJaegerClient
|
||||
{
|
||||
private readonly UdpClient client;
|
||||
|
||||
|
|
@ -36,7 +38,39 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
|
||||
public void Connect(string host, int port) => this.client.Connect(host, port);
|
||||
|
||||
public Task<int> SendAsync(byte[] datagram, int bytes) => this.client.SendAsync(datagram, bytes);
|
||||
public ValueTask<int> SendAsync(byte[] buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return this.SendAsync(buffer, 0, buffer?.Length ?? 0);
|
||||
}
|
||||
|
||||
public ValueTask<int> SendAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var socket = this.client.Client;
|
||||
|
||||
var asyncResult = socket.BeginSend(
|
||||
buffer,
|
||||
offset,
|
||||
count,
|
||||
SocketFlags.None,
|
||||
callback: null,
|
||||
state: null);
|
||||
|
||||
if (asyncResult.CompletedSynchronously)
|
||||
{
|
||||
return new ValueTask<int>(socket.EndSend(asyncResult));
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<int>();
|
||||
|
||||
ThreadPool.RegisterWaitForSingleObject(
|
||||
waitObject: asyncResult.AsyncWaitHandle,
|
||||
callBack: (s, t) => tcs.SetResult(socket.EndSend(asyncResult)),
|
||||
state: null,
|
||||
timeout: TimeSpan.MaxValue,
|
||||
executeOnlyOnce: true);
|
||||
|
||||
return new ValueTask<int>(tcs.Task);
|
||||
}
|
||||
|
||||
public void Dispose() => this.client.Dispose();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,148 @@
|
|||
// <copyright file="PooledList.cs" company="OpenTelemetry Authors">
|
||||
// Copyright 2018, OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
{
|
||||
internal readonly struct PooledList<T> : IEnumerable<T>, ICollection
|
||||
{
|
||||
private static readonly ArrayPool<T> Pool = ArrayPool<T>.Create(4096, 64);
|
||||
private static int lastAllocatedSize = 64;
|
||||
|
||||
private readonly T[] buffer;
|
||||
|
||||
private PooledList(T[] buffer, int count)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
this.Count = count;
|
||||
}
|
||||
|
||||
public int Count { get; }
|
||||
|
||||
public bool IsEmpty => this.Count == 0;
|
||||
|
||||
bool ICollection.IsSynchronized => false;
|
||||
|
||||
object ICollection.SyncRoot => this;
|
||||
|
||||
public ref T this[int index]
|
||||
{
|
||||
get => ref this.buffer[index];
|
||||
}
|
||||
|
||||
public static PooledList<T> Create()
|
||||
{
|
||||
return new PooledList<T>(Pool.Rent(lastAllocatedSize), 0);
|
||||
}
|
||||
|
||||
public static void Add(ref PooledList<T> list, T item)
|
||||
{
|
||||
var buffer = list.buffer;
|
||||
|
||||
if (buffer == null)
|
||||
{
|
||||
throw new InvalidOperationException("Items cannot be added to an empty pool instance.");
|
||||
}
|
||||
|
||||
if (list.Count >= buffer.Length)
|
||||
{
|
||||
lastAllocatedSize = buffer.Length * 2;
|
||||
var previousBuffer = buffer;
|
||||
|
||||
buffer = Pool.Rent(lastAllocatedSize);
|
||||
|
||||
var span = previousBuffer.AsSpan();
|
||||
span.CopyTo(buffer);
|
||||
Pool.Return(previousBuffer);
|
||||
}
|
||||
|
||||
buffer[list.Count] = item;
|
||||
list = new PooledList<T>(buffer, list.Count + 1);
|
||||
}
|
||||
|
||||
public static void Clear(ref PooledList<T> list)
|
||||
{
|
||||
list = new PooledList<T>(list.buffer, 0);
|
||||
}
|
||||
|
||||
public void Return()
|
||||
{
|
||||
var buffer = this.buffer;
|
||||
if (buffer != null)
|
||||
{
|
||||
Pool.Return(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection.CopyTo(Array array, int index) => throw new NotSupportedException();
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return new Enumerator(in this);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return new Enumerator(in this);
|
||||
}
|
||||
|
||||
private struct Enumerator : IEnumerator<T>, IEnumerator
|
||||
{
|
||||
private readonly T[] buffer;
|
||||
private readonly int count;
|
||||
private int index;
|
||||
private T current;
|
||||
|
||||
public Enumerator(in PooledList<T> list)
|
||||
{
|
||||
this.buffer = list.buffer;
|
||||
this.count = list.Count;
|
||||
this.index = 0;
|
||||
this.current = default;
|
||||
}
|
||||
|
||||
public T Current { get => this.current; }
|
||||
|
||||
object IEnumerator.Current { get => this.Current; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (this.index < this.count)
|
||||
{
|
||||
this.current = this.buffer[this.index++];
|
||||
return true;
|
||||
}
|
||||
|
||||
this.index = this.count + 1;
|
||||
this.current = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
this.index = 0;
|
||||
this.current = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,7 @@ namespace OpenTelemetry.Exporter.Jaeger
|
|||
{
|
||||
public class JaegerTraceExporter : SpanExporter, IDisposable
|
||||
{
|
||||
private readonly SemaphoreSlim exportLock = new SemaphoreSlim(1);
|
||||
private bool libraryResourceApplied = false;
|
||||
private bool disposedValue = false; // To detect redundant dispose calls
|
||||
|
||||
|
|
@ -43,31 +44,37 @@ namespace OpenTelemetry.Exporter.Jaeger
|
|||
|
||||
public override async Task<ExportResult> ExportAsync(IEnumerable<SpanData> otelSpanList, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!this.libraryResourceApplied && otelSpanList.Count() > 0)
|
||||
await this.exportLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var libraryResource = otelSpanList.First().LibraryResource;
|
||||
if (!this.libraryResourceApplied && otelSpanList.Count() > 0)
|
||||
{
|
||||
var libraryResource = otelSpanList.First().LibraryResource;
|
||||
|
||||
this.ApplyLibraryResource(libraryResource ?? Resource.Empty);
|
||||
this.ApplyLibraryResource(libraryResource ?? Resource.Empty);
|
||||
|
||||
this.libraryResourceApplied = true;
|
||||
this.libraryResourceApplied = true;
|
||||
}
|
||||
|
||||
foreach (var s in otelSpanList)
|
||||
{
|
||||
// avoid cancelling here: this is no return point: if we reached this point
|
||||
// and cancellation is requested, it's better if we try to finish sending spans rather than drop it
|
||||
await this.JaegerAgentUdpBatcher.AppendAsync(s, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// TODO jaeger status to ExportResult
|
||||
return ExportResult.Success;
|
||||
}
|
||||
|
||||
var jaegerSpans = otelSpanList.Select(sdl => sdl.ToJaegerSpan());
|
||||
|
||||
foreach (var s in jaegerSpans)
|
||||
finally
|
||||
{
|
||||
// avoid cancelling here: this is no return point: if we reached this point
|
||||
// and cancellation is requested, it's better if we try to finish sending spans rather than drop it
|
||||
await this.JaegerAgentUdpBatcher.AppendAsync(s, CancellationToken.None);
|
||||
this.exportLock.Release();
|
||||
}
|
||||
|
||||
// TODO jaeger status to ExportResult
|
||||
return ExportResult.Success;
|
||||
}
|
||||
|
||||
public override Task ShutdownAsync(CancellationToken cancellationToken)
|
||||
public override async Task ShutdownAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return this.JaegerAgentUdpBatcher.FlushAsync(cancellationToken);
|
||||
await this.JaegerAgentUdpBatcher.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
|
@ -135,6 +142,7 @@ namespace OpenTelemetry.Exporter.Jaeger
|
|||
if (disposing)
|
||||
{
|
||||
this.JaegerAgentUdpBatcher.Dispose();
|
||||
this.exportLock.Dispose();
|
||||
}
|
||||
|
||||
this.disposedValue = true;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
|
||||
<Authors>OpenTelemetry authors</Authors>
|
||||
<PackageIconUrl>https://opentelemetry.io/img/logos/opentelemetry-icon-color.png</PackageIconUrl>
|
||||
<PackageProjectUrl>https://OpenTelemetry.io</PackageProjectUrl>
|
||||
|
|
@ -13,17 +13,12 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenTelemetry\OpenTelemetry.csproj" />
|
||||
<ProjectReference Include="..\..\lib\Thrift\Thrift.csproj" PrivateAssets="All" />
|
||||
<PackageReference Include="ApacheThrift" Version="0.13.0.1" PrivateAssets="All" />
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetMyPackageFiles</TargetsForTfmSpecificBuildOutput>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenTelemetry\OpenTelemetry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="GetMyPackageFiles">
|
||||
<ItemGroup>
|
||||
<BuildOutputInPackage Include="$(OutputPath)Thrift.dll" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -18,15 +18,17 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocols.Entities;
|
||||
using OpenTelemetry.Exporter.Jaeger.Implementation;
|
||||
using Thrift.Protocol;
|
||||
using Thrift.Protocol.Entities;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
||||
namespace OpenTelemetry.Exporter.Jaeger
|
||||
{
|
||||
public class Process : TAbstractBase
|
||||
public class Process : TUnionBase
|
||||
{
|
||||
public Process()
|
||||
public Process(string serviceName)
|
||||
{
|
||||
this.ServiceName = serviceName;
|
||||
}
|
||||
|
||||
public Process(string serviceName, IDictionary<string, object> processTags)
|
||||
|
|
@ -35,21 +37,19 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
|
|||
}
|
||||
|
||||
internal Process(string serviceName, IDictionary<string, JaegerTag> processTags)
|
||||
: this()
|
||||
: this(serviceName)
|
||||
{
|
||||
this.ServiceName = serviceName;
|
||||
|
||||
if (processTags != null)
|
||||
{
|
||||
this.Tags = processTags;
|
||||
}
|
||||
}
|
||||
|
||||
public string ServiceName { get; set; }
|
||||
public string ServiceName { get; internal set; }
|
||||
|
||||
public IDictionary<string, JaegerTag> Tags { get; set; }
|
||||
internal IDictionary<string, JaegerTag> Tags { get; set; }
|
||||
|
||||
internal int ByteSize { get; set; }
|
||||
internal byte[] Message { get; set; }
|
||||
|
||||
public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
@ -35,7 +35,6 @@ namespace OpenTelemetry.Trace.Export
|
|||
private readonly int maxQueueSize;
|
||||
private readonly int maxExportBatchSize;
|
||||
private readonly TimeSpan scheduleDelay;
|
||||
private readonly Task worker;
|
||||
private readonly SpanExporter exporter;
|
||||
private CancellationTokenSource cts;
|
||||
private volatile int currentQueueSize;
|
||||
|
|
@ -58,7 +57,6 @@ namespace OpenTelemetry.Trace.Export
|
|||
/// <param name="exporter">Exporter instance.</param>
|
||||
public BatchingSpanProcessor(SpanExporter exporter) : this(exporter, DefaultMaxQueueSize, DefaultScheduleDelay, DefaultMaxExportBatchSize)
|
||||
{
|
||||
this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -80,7 +78,7 @@ namespace OpenTelemetry.Trace.Export
|
|||
throw new ArgumentOutOfRangeException(nameof(maxExportBatchSize));
|
||||
}
|
||||
|
||||
this.exporter = exporter;
|
||||
this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
|
||||
this.maxQueueSize = maxQueueSize;
|
||||
this.scheduleDelay = scheduleDelay;
|
||||
this.maxExportBatchSize = maxExportBatchSize;
|
||||
|
|
@ -91,7 +89,7 @@ namespace OpenTelemetry.Trace.Export
|
|||
// worker task that will last for lifetime of processor.
|
||||
// No need to specify long running - it is useless if any async calls are made internally.
|
||||
// Threads are also useless as exporter tasks run in thread pool threads.
|
||||
this.worker = Task.Factory.StartNew(s => this.Worker((CancellationToken)s), this.cts.Token);
|
||||
Task.Factory.StartNew(s => this.Worker((CancellationToken)s), this.cts.Token);
|
||||
}
|
||||
|
||||
public override void OnStart(SpanData span)
|
||||
|
|
@ -124,7 +122,7 @@ namespace OpenTelemetry.Trace.Export
|
|||
if (!this.stopping)
|
||||
{
|
||||
this.stopping = true;
|
||||
|
||||
|
||||
// This will stop the loop after current batch finishes.
|
||||
this.cts.Cancel(false);
|
||||
this.cts.Dispose();
|
||||
|
|
@ -148,11 +146,31 @@ namespace OpenTelemetry.Trace.Export
|
|||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool isDisposing)
|
||||
{
|
||||
if (!this.stopping)
|
||||
{
|
||||
this.ShutdownAsync(CancellationToken.None).ContinueWith(_ => { }).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
if (isDisposing)
|
||||
{
|
||||
if (this.exporter is IDisposable disposableExporter)
|
||||
{
|
||||
try
|
||||
{
|
||||
disposableExporter.Dispose();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
OpenTelemetrySdkEventSource.Log.SpanProcessorException("Dispose", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExportBatchAsync(CancellationToken cancellationToken)
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ namespace OpenTelemetry.Trace.Export
|
|||
/// <summary>
|
||||
/// Implements simple span processor that exports spans in OnEnd call without batching.
|
||||
/// </summary>
|
||||
public class SimpleSpanProcessor : SpanProcessor
|
||||
public class SimpleSpanProcessor : SpanProcessor, IDisposable
|
||||
{
|
||||
private readonly SpanExporter exporter;
|
||||
private bool disposed = false;
|
||||
|
|
@ -75,5 +75,28 @@ namespace OpenTelemetry.Trace.Export
|
|||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool isDisposing)
|
||||
{
|
||||
if (isDisposing)
|
||||
{
|
||||
if (this.exporter is IDisposable disposableExporter)
|
||||
{
|
||||
try
|
||||
{
|
||||
disposableExporter.Dispose();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
OpenTelemetrySdkEventSource.Log.SpanProcessorException("Dispose", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
||||
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ using System.Linq;
|
|||
using OpenTelemetry.Exporter.Jaeger.Implementation;
|
||||
using OpenTelemetry.Resources;
|
||||
using OpenTelemetry.Trace;
|
||||
using OpenTelemetry.Trace.Configuration;
|
||||
using OpenTelemetry.Trace.Export;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -30,16 +29,6 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
{
|
||||
public class JaegerSpanConverterTest
|
||||
{
|
||||
private const long MillisPerSecond = 1000L;
|
||||
private const long NanosPerMillisecond = 1000 * 1000;
|
||||
private const long NanosPerSecond = NanosPerMillisecond * MillisPerSecond;
|
||||
private readonly Tracer tracer;
|
||||
|
||||
public JaegerSpanConverterTest()
|
||||
{
|
||||
tracer = TracerFactory.Create(b => { }).GetTracer(null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JaegerSpanConverterTest_ConvertSpanToJaegerSpan_AllPropertiesSet()
|
||||
{
|
||||
|
|
@ -52,14 +41,14 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
Assert.Equal("Name", jaegerSpan.OperationName);
|
||||
Assert.Equal(2, jaegerSpan.Logs.Count());
|
||||
Assert.Equal(2, jaegerSpan.Logs.Count);
|
||||
|
||||
Assert.Equal(traceIdAsInt.High, jaegerSpan.TraceIdHigh);
|
||||
Assert.Equal(traceIdAsInt.Low, jaegerSpan.TraceIdLow);
|
||||
Assert.Equal(spanIdAsInt.Low, jaegerSpan.SpanId);
|
||||
Assert.Equal(new Int128(span.ParentSpanId).Low, jaegerSpan.ParentSpanId);
|
||||
|
||||
Assert.Equal(span.Links.Count(), jaegerSpan.References.Count());
|
||||
Assert.Equal(span.Links.Count(), jaegerSpan.References.Count);
|
||||
var references = jaegerSpan.References.ToArray();
|
||||
var jaegerRef = references[0];
|
||||
Assert.Equal(linkTraceIdAsInt.High, jaegerRef.TraceIdHigh);
|
||||
|
|
@ -71,7 +60,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
Assert.Equal(span.StartTimestamp.ToEpochMicroseconds(), jaegerSpan.StartTime);
|
||||
Assert.Equal((long)((span.EndTimestamp - span.StartTimestamp).TotalMilliseconds * 1000), jaegerSpan.Duration);
|
||||
|
||||
var tags = jaegerSpan.JaegerTags.ToArray();
|
||||
var tags = jaegerSpan.Tags.ToArray();
|
||||
var tag = tags[0];
|
||||
Assert.Equal(JaegerTagType.STRING, tag.VType);
|
||||
Assert.Equal("stringKey", tag.Key);
|
||||
|
|
@ -134,14 +123,14 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
Assert.Equal("Name", jaegerSpan.OperationName);
|
||||
Assert.Equal(2, jaegerSpan.Logs.Count());
|
||||
Assert.Equal(2, jaegerSpan.Logs.Count);
|
||||
|
||||
Assert.Equal(traceIdAsInt.High, jaegerSpan.TraceIdHigh);
|
||||
Assert.Equal(traceIdAsInt.Low, jaegerSpan.TraceIdLow);
|
||||
Assert.Equal(spanIdAsInt.Low, jaegerSpan.SpanId);
|
||||
Assert.Equal(new Int128(span.ParentSpanId).Low, jaegerSpan.ParentSpanId);
|
||||
|
||||
Assert.Equal(span.Links.Count(), jaegerSpan.References.Count());
|
||||
Assert.Equal(span.Links.Count(), jaegerSpan.References.Count);
|
||||
var references = jaegerSpan.References.ToArray();
|
||||
var jaegerRef = references[0];
|
||||
Assert.Equal(linkTraceIdAsInt.High, jaegerRef.TraceIdHigh);
|
||||
|
|
@ -154,7 +143,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
Assert.Equal((long)((span.EndTimestamp - span.StartTimestamp).TotalMilliseconds * 1000), jaegerSpan.Duration);
|
||||
|
||||
// 2 tags: span.kind & ot.status_code.
|
||||
Assert.Equal(2, jaegerSpan.JaegerTags.Count());
|
||||
Assert.Equal(2, jaegerSpan.Tags.Count);
|
||||
|
||||
var logs = jaegerSpan.Logs.ToArray();
|
||||
var jaegerLog = logs[0];
|
||||
|
|
@ -200,7 +189,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
Assert.Equal(spanIdAsInt.Low, jaegerSpan.SpanId);
|
||||
Assert.Equal(new Int128(span.ParentSpanId).Low, jaegerSpan.ParentSpanId);
|
||||
|
||||
Assert.Equal(span.Links.Count(), jaegerSpan.References.Count());
|
||||
Assert.Equal(span.Links.Count(), jaegerSpan.References.Count);
|
||||
var references = jaegerSpan.References.ToArray();
|
||||
var jaegerRef = references[0];
|
||||
Assert.Equal(linkTraceIdAsInt.High, jaegerRef.TraceIdHigh);
|
||||
|
|
@ -213,7 +202,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
Assert.Equal(span.EndTimestamp.ToEpochMicroseconds()
|
||||
- span.StartTimestamp.ToEpochMicroseconds(), jaegerSpan.Duration);
|
||||
|
||||
var tags = jaegerSpan.JaegerTags.ToArray();
|
||||
var tags = jaegerSpan.Tags.ToArray();
|
||||
var tag = tags[0];
|
||||
Assert.Equal(JaegerTagType.STRING, tag.VType);
|
||||
Assert.Equal("stringKey", tag.Key);
|
||||
|
|
@ -250,7 +239,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
Assert.Equal("Name", jaegerSpan.OperationName);
|
||||
Assert.Equal(2, jaegerSpan.Logs.Count());
|
||||
Assert.Equal(2, jaegerSpan.Logs.Count);
|
||||
|
||||
Assert.Equal(traceIdAsInt.High, jaegerSpan.TraceIdHigh);
|
||||
Assert.Equal(traceIdAsInt.Low, jaegerSpan.TraceIdLow);
|
||||
|
|
@ -265,7 +254,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
Assert.Equal(span.EndTimestamp.ToEpochMicroseconds()
|
||||
- span.StartTimestamp.ToEpochMicroseconds(), jaegerSpan.Duration);
|
||||
|
||||
var tags = jaegerSpan.JaegerTags.ToArray();
|
||||
var tags = jaegerSpan.Tags.ToArray();
|
||||
var tag = tags[0];
|
||||
Assert.Equal(JaegerTagType.STRING, tag.VType);
|
||||
Assert.Equal("stringKey", tag.Key);
|
||||
|
|
@ -337,7 +326,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
// Assert
|
||||
Assert.Null(jaegerSpan.JaegerTags.FirstOrDefault(t => t.Key == "peer.service"));
|
||||
Assert.DoesNotContain(jaegerSpan.Tags, t => t.Key == "peer.service");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -354,9 +343,8 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
// Assert
|
||||
var tag = jaegerSpan.JaegerTags.FirstOrDefault(t => t.Key == "peer.service");
|
||||
Assert.NotNull(tag);
|
||||
Assert.Equal("RemoteServiceName", tag.VStr);
|
||||
Assert.Contains(jaegerSpan.Tags, t => t.Key == "peer.service");
|
||||
Assert.Equal("RemoteServiceName", jaegerSpan.Tags.First(t => t.Key == "peer.service").VStr);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -375,10 +363,9 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
// Assert
|
||||
var tags = jaegerSpan.JaegerTags.Where(t => t.Key == "peer.service");
|
||||
var tags = jaegerSpan.Tags.Where(t => t.Key == "peer.service");
|
||||
Assert.Single(tags);
|
||||
var tag = tags.First();
|
||||
Assert.NotNull(tag);
|
||||
Assert.Equal("RemoteServiceName", tag.VStr);
|
||||
}
|
||||
|
||||
|
|
@ -394,9 +381,9 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
|
||||
var jaegerSpan = span.ToJaegerSpan();
|
||||
|
||||
Assert.Contains(jaegerSpan.JaegerTags, t => t.Key == Resource.LibraryNameKey && t.VStr == "libname");
|
||||
Assert.Contains(jaegerSpan.JaegerTags, t => t.Key == Resource.LibraryVersionKey && t.VStr == "libversion");
|
||||
Assert.DoesNotContain(jaegerSpan.JaegerTags, t => t.Key == Resource.ServiceNameKey && t.VStr == "MyService");
|
||||
Assert.Contains(jaegerSpan.Tags, t => t.Key == Resource.LibraryNameKey && t.VStr == "libname");
|
||||
Assert.Contains(jaegerSpan.Tags, t => t.Key == Resource.LibraryVersionKey && t.VStr == "libversion");
|
||||
Assert.DoesNotContain(jaegerSpan.Tags, t => t.Key == Resource.ServiceNameKey && t.VStr == "MyService");
|
||||
}
|
||||
|
||||
internal static SpanData CreateTestSpan(
|
||||
|
|
|
|||
|
|
@ -13,36 +13,27 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
|
||||
using OpenTelemetry.Trace.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using Moq;
|
||||
using System.Threading.Tasks;
|
||||
using OpenTelemetry.Exporter.Jaeger.Implementation;
|
||||
using OpenTelemetry.Trace;
|
||||
using OpenTelemetry.Trace.Export;
|
||||
using Thrift.Protocols;
|
||||
using Thrift.Protocol;
|
||||
using Xunit;
|
||||
using Process = OpenTelemetry.Exporter.Jaeger.Implementation.Process;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
||||
{
|
||||
public class JaegerThriftIntegrationTest
|
||||
{
|
||||
private readonly Tracer tracer;
|
||||
|
||||
public JaegerThriftIntegrationTest()
|
||||
{
|
||||
tracer = TracerFactory.Create(b => { }).GetTracer(null);
|
||||
}
|
||||
public const string TestPayloadBase64 = "goEBCWVtaXRCYXRjaBwcGAx0ZXN0IHByb2Nlc3MZHBgQdGVzdF9wcm9jZXNzX3RhZxUAGAp0ZXN0X3ZhbHVlAAAZHBab5cuG2OehhdwBFuPakI2n2cCVLhaAjfWp6NHt6dQBFrK5moSni5GXGBgETmFtZRkcFQAWm+XLhtjnoYXcARbj2pCNp9nAlS4W/Y6j+bqS9fbuAQAVAhaAgLPexpa/BRaAnJw5GYwYCXN0cmluZ0tleRUAGAV2YWx1ZQAYB2xvbmdLZXkVBkYCABgIbG9uZ0tleTIVBkYCABgJZG91YmxlS2V5FQInAAAAAAAA8D8AGApkb3VibGVLZXkyFQInAAAAAAAA8D8AGAdib29sS2V5FQQxABgJc3Bhbi5raW5kFQAYBmNsaWVudAAYDm90LnN0YXR1c19jb2RlFQAYAk9rABksFoCAs97Glr8FGSwYA2tleRUAGAV2YWx1ZQAYB21lc3NhZ2UVABgGRXZlbnQxAAAWgICz3saWvwUZLBgDa2V5FQAYBXZhbHVlABgHbWVzc2FnZRUAGAZFdmVudDIAAAAAAA==";
|
||||
|
||||
[Fact]
|
||||
public async void JaegerThriftIntegrationTest_TAbstractBaseGeneratesConsistentThriftPayload()
|
||||
public async Task JaegerThriftIntegrationTest_TAbstractBaseGeneratesConsistentThriftPayload()
|
||||
{
|
||||
var validJaegerThriftPayload = Convert.FromBase64String("goEBCWVtaXRCYXRjaBwcGAx0ZXN0IHByb2Nlc3MZHBgQdGVzdF9wcm9jZXNzX3RhZxUAGAp0ZXN0X3ZhbHVlAAAZHBab5cuG2OehhdwBFuPakI2n2cCVLhaAjfWp6NHt6dQBFrK5moSni5GXGBgETmFtZRkcFQAWm+XLhtjnoYXcARbj2pCNp9nAlS4W/Y6j+bqS9fbuAQAVAhaAgLPexpa/BRaAnJw5GYwYCXN0cmluZ0tleRUAGAV2YWx1ZQAYB2xvbmdLZXkVBkYCABgIbG9uZ0tleTIVBkYCABgJZG91YmxlS2V5FQInAAAAAAAA8D8AGApkb3VibGVLZXkyFQInAAAAAAAA8D8AGAdib29sS2V5FQQxABgJc3Bhbi5raW5kFQAYBmNsaWVudAAYDm90LnN0YXR1c19jb2RlFQAYAk9rABksFoCAs97Glr8FGSwYA2tleRUAGAV2YWx1ZQAYB21lc3NhZ2UVABgGRXZlbnQxAAAWgICz3saWvwUZLBgDa2V5FQAYBXZhbHVlABgHbWVzc2FnZRUAGAZFdmVudDIAAAAAAA==");
|
||||
var validJaegerThriftPayload = Convert.FromBase64String(TestPayloadBase64);
|
||||
|
||||
using (var memoryTransport = new InMemoryTransport())
|
||||
{
|
||||
|
|
@ -50,18 +41,18 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var thriftClient = new JaegerThriftClient(protocolFactory.GetProtocol(memoryTransport));
|
||||
var spanData = CreateTestSpan();
|
||||
var span = spanData.ToJaegerSpan();
|
||||
var process = new Process("test process", new Dictionary<string, object> { { "test_process_tag", "test_value" } });
|
||||
var process = TestProcess;
|
||||
var batch = new Batch(process, new List<JaegerSpan> { span });
|
||||
|
||||
await thriftClient.EmitBatchAsync(batch, CancellationToken.None);
|
||||
|
||||
var buff = memoryTransport.GetBuffer();
|
||||
|
||||
Assert.Equal(validJaegerThriftPayload, buff);
|
||||
Assert.Equal(validJaegerThriftPayload, memoryTransport.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private SpanData CreateTestSpan()
|
||||
internal static Process TestProcess { get; } = new Process("test process", new Dictionary<string, object> { { "test_process_tag", "test_value" } });
|
||||
|
||||
internal static SpanData CreateTestSpan()
|
||||
{
|
||||
var startTimestamp = new DateTimeOffset(2019, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
||||
var endTimestamp = startTimestamp.AddSeconds(60);
|
||||
|
|
|
|||
|
|
@ -34,16 +34,16 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerUdpBatcher = new JaegerUdpBatcher(options);
|
||||
|
||||
// Act
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
var batches = jaegerUdpBatcher.CurrentBatches.Values;
|
||||
|
||||
// Assert
|
||||
Assert.Single(batches);
|
||||
Assert.Equal("TestService", batches.First().Process.ServiceName);
|
||||
Assert.Equal(3, batches.First().Spans.Count());
|
||||
Assert.Equal(3, batches.First().SpanMessages.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -55,16 +55,15 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerUdpBatcher = new JaegerUdpBatcher(options);
|
||||
|
||||
// Act
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(
|
||||
JaegerSpanConverterTest.CreateTestSpan(
|
||||
additionalAttributes: new Dictionary<string, object>
|
||||
{
|
||||
["peer.service"] = "MySQL",
|
||||
})
|
||||
.ToJaegerSpan(),
|
||||
}),
|
||||
CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
var batches = jaegerUdpBatcher.CurrentBatches.Values;
|
||||
|
||||
|
|
@ -73,11 +72,11 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
|
||||
var PrimaryBatch = batches.Where(b => b.Process.ServiceName == "TestService");
|
||||
Assert.Single(PrimaryBatch);
|
||||
Assert.Equal(2, PrimaryBatch.First().Spans.Count());
|
||||
Assert.Equal(2, PrimaryBatch.First().SpanMessages.Count());
|
||||
|
||||
var MySQLBatch = batches.Where(b => b.Process.ServiceName == "MySQL");
|
||||
Assert.Single(MySQLBatch);
|
||||
Assert.Single(MySQLBatch.First().Spans);
|
||||
Assert.Single(MySQLBatch.First().SpanMessages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -89,9 +88,9 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var jaegerUdpBatcher = new JaegerUdpBatcher(options);
|
||||
|
||||
// Act
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
var flushCount = await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan().ToJaegerSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
var flushCount = await jaegerUdpBatcher.AppendAsync(JaegerSpanConverterTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
var batches = jaegerUdpBatcher.CurrentBatches.Values;
|
||||
|
||||
|
|
@ -99,7 +98,39 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
Assert.Equal(2, flushCount);
|
||||
Assert.Single(batches);
|
||||
Assert.Equal("TestService", batches.First().Process.ServiceName);
|
||||
Assert.Single(batches.First().Spans);
|
||||
Assert.Single(batches.First().SpanMessages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JaegerUdpBatcher_IntegrationTest()
|
||||
{
|
||||
var validJaegerThriftPayload = Convert.FromBase64String(JaegerThriftIntegrationTest.TestPayloadBase64);
|
||||
|
||||
var memoryTransport = new InMemoryTransport();
|
||||
|
||||
using (var jaegerUdpBatcher = new JaegerUdpBatcher(
|
||||
new JaegerExporterOptions { MaxFlushInterval = TimeSpan.FromHours(1) },
|
||||
memoryTransport))
|
||||
{
|
||||
jaegerUdpBatcher.Process = JaegerThriftIntegrationTest.TestProcess;
|
||||
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerThriftIntegrationTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
await jaegerUdpBatcher.FlushAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
Assert.Equal(Convert.ToBase64String(validJaegerThriftPayload), Convert.ToBase64String(memoryTransport.ToArray()));
|
||||
|
||||
memoryTransport.Reset();
|
||||
|
||||
await jaegerUdpBatcher.AppendAsync(JaegerThriftIntegrationTest.CreateTestSpan(), CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
await jaegerUdpBatcher.FlushAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
// SeqNo is the second byte.
|
||||
validJaegerThriftPayload[2]++;
|
||||
|
||||
Assert.Equal(Convert.ToBase64String(validJaegerThriftPayload), Convert.ToBase64String(memoryTransport.ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using OpenTelemetry.Exporter.Jaeger.Implementation;
|
||||
using Thrift.Transports;
|
||||
using Thrift.Transport;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
||||
|
|
@ -27,7 +27,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
public class ThriftUdpClientTransportTests: IDisposable
|
||||
{
|
||||
private MemoryStream testingMemoryStream = new MemoryStream();
|
||||
private readonly Mock<IJaegerUdpClient> mockClient = new Mock<IJaegerUdpClient>();
|
||||
private readonly Mock<IJaegerClient> mockClient = new Mock<IJaegerClient>();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
|
@ -97,7 +97,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var tInfo = transport.FlushAsync();
|
||||
|
||||
Assert.True(tInfo.IsCompleted);
|
||||
this.mockClient.Verify(t => t.SendAsync(It.IsAny<byte[]>(), It.IsAny<int>()), Times.Never);
|
||||
this.mockClient.Verify(t => t.SendAsync(It.IsAny<byte[]>(), CancellationToken.None), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -112,7 +112,7 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var tInfo = transport.FlushAsync();
|
||||
|
||||
Assert.True(tInfo.IsCompleted);
|
||||
this.mockClient.Verify(t => t.SendAsync(It.IsAny<byte[]>(), 8), Times.Once);
|
||||
this.mockClient.Verify(t => t.SendAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -123,10 +123,10 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests.Implementation
|
|||
var streamBytes = new byte[] { 0x20, 0x10, 0x40, 0x30, 0x18, 0x14, 0x10, 0x28 };
|
||||
this.testingMemoryStream = new MemoryStream(streamBytes);
|
||||
|
||||
//this.mockClient.Setup(t => t.SendAsync(It.IsAny<byte[]>(), It.IsAny<int>())).Throws<Exception>("message, yo");
|
||||
this.mockClient.Setup(t => t.SendAsync(It.IsAny<byte[]>(), It.IsAny<int>())).Throws(new Exception("message, yo"));
|
||||
this.mockClient.Setup(t => t.SendAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), CancellationToken.None)).Throws(new Exception("message, yo"));
|
||||
|
||||
var transport = new JaegerThriftClientTransport(host, port, this.testingMemoryStream, this.mockClient.Object);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<TTransportException>(() => transport.FlushAsync());
|
||||
|
||||
Assert.Equal("Cannot flush closed transport. message, yo", ex.Message);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
<PropertyGroup>
|
||||
<Description>Unit test project for Jaeger Exporter for OpenTelemetry</Description>
|
||||
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
|
||||
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net46</TargetFrameworks>
|
||||
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net461</TargetFrameworks>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
|
@ -13,20 +13,30 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.13.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
|
||||
<PackageReference Include="ApacheThrift" Version="0.13.0.1" ExcludeAssets="Compile" GeneratePathProperty="true" />
|
||||
<PackageReference Update="coverlet.msbuild" Version="2.8.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
|
||||
<ProjectReference Include="..\..\src\OpenTelemetry.Exporter.Jaeger\OpenTelemetry.Exporter.Jaeger.csproj" />
|
||||
<ProjectReference Include="..\..\lib\Thrift\Thrift.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="ApacheThrift">
|
||||
<HintPath>$(PkgApacheThrift)\lib\netstandard2.0\Thrift.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -7,9 +7,12 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,12 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="1.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@
|
|||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
|
||||
<ProjectReference Include="..\..\src\OpenTelemetry.Exporter.Zipkin\OpenTelemetry.Exporter.Zipkin.csproj" />
|
||||
<ProjectReference Include="..\..\lib\Thrift\Thrift.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue