Skip to content

Commit 3c7dc47

Browse files
Merge pull request #8 from spotware/dev
Dev
2 parents 88f44d3 + e3a56cf commit 3c7dc47

7 files changed

Lines changed: 29 additions & 21 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
# OpenAPI.Net
22

3-
[![NuGet version (Spotware.OpenAPI.Net)](https://buildstats.info/nuget/Spotware.OpenAPI.Net)](https://www.nuget.org/packages/Spotware.OpenAPI.Net/)
3+
[![NuGet version (cTrader.OpenAPI.Net)](https://buildstats.info/nuget/cTrader.OpenAPI.Net)](https://www.nuget.org/packages/cTrader.OpenAPI.Net/)
44

55
cTrader Open API .NET Rx library
66

77
This library allows you to easily use and integrate cTrader Open API on your .NET applications.
88

99
Its written by using RX streams so it makes API usage very easy and allows you to do a lot with few lines of code.
1010

11-
Current version of library is based on .NET standard 2.1, you can't use it on .NET framework apps, you can only use it on .NET core or .NET 5 and later.
11+
It uses channels and array pools to avoid too many allocations, we tried our best to make it as efficient as possible.
12+
13+
Current version of library is targets .NET 6, so you can't use it on .NET framework apps.
14+
15+
Please check the samples, we have some good samples for all kinds of .NET apps.
16+
17+
Feel free to fork and improve it!
1218

1319
Documentation: [https://spotware.github.io/OpenAPI.Net/](https://spotware.github.io/OpenAPI.Net/)
1420

1521
## Dependencies
1622

1723
* [protobuf](https://github.com/protocolbuffers/protobuf)
1824
* [Reactive](https://github.com/dotnet/reactive)
19-
* [System.Text.Json](https://www.nuget.org/packages/System.Text.Json/)
2025
* [websocket-client](https://github.com/Marfusios/websocket-client)
2126

2227
## Licence

src/OpenAPI.Net/Helpers/NameValueCollectionToQueryString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static string ToQueryString(this NameValueCollection collection)
1010
{
1111
if (collection == null) return string.Empty;
1212

13-
StringBuilder stringBuilder = new StringBuilder();
13+
StringBuilder stringBuilder = new();
1414

1515
foreach (string key in collection.Keys)
1616
{

src/OpenAPI.Net/OpenAPI.Net.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<PackageLicenseExpression>MIT</PackageLicenseExpression>
77
<PackageProjectUrl>https://github.com/spotware/OpenAPI.Net</PackageProjectUrl>
88
<RepositoryUrl>https://github.com/spotware/OpenAPI.Net</RepositoryUrl>
9-
<PackageTags>cTrader, Open API, Spotware</PackageTags>
10-
<Description>A .NET RX library for Spotware Open API</Description>
11-
<PackageId>Spotware.OpenAPI.Net</PackageId>
12-
<Version>1.3.9</Version>
9+
<PackageTags>cTrader, Open API, Spotware, cTrader</PackageTags>
10+
<Description>A .NET RX library for cTrader Open API</Description>
11+
<PackageId>cTrader.OpenAPI.Net</PackageId>
12+
<Version>1.4.0-rc0</Version>
1313
<Platforms>AnyCPU</Platforms>
1414
<Company>Spotware</Company>
1515
<Authors>Spotware</Authors>
@@ -29,7 +29,6 @@
2929
<ItemGroup>
3030
<PackageReference Include="Google.Protobuf" Version="3.19.4" />
3131
<PackageReference Include="System.Reactive" Version="5.0.0" />
32-
<PackageReference Include="System.Text.Json" Version="6.0.2" />
3332
<PackageReference Include="Websocket.Client" Version="4.4.43" />
3433
</ItemGroup>
3534

src/OpenAPI.Net/OpenClient.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313
using Websocket.Client;
1414
using System.Net.WebSockets;
1515
using System.Threading.Channels;
16+
using System.Buffers;
1617

1718
namespace OpenAPI.Net
1819
{
1920
public sealed class OpenClient : IDisposable, IObservable<IMessage>
2021
{
2122
private readonly TimeSpan _heartbeatInerval;
2223

23-
private readonly ProtoHeartbeatEvent _heartbeatEvent = new ProtoHeartbeatEvent();
24+
private readonly ProtoHeartbeatEvent _heartbeatEvent = new();
2425

2526
private readonly Channel<ProtoMessage> _messagesChannel = Channel.CreateUnbounded<ProtoMessage>();
2627

27-
private readonly ConcurrentDictionary<int, IObserver<IMessage>> _observers = new ConcurrentDictionary<int, IObserver<IMessage>>();
28+
private readonly ConcurrentDictionary<int, IObserver<IMessage>> _observers = new();
2829

29-
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
30+
private readonly CancellationTokenSource _cancellationTokenSource = new();
3031

3132
private readonly TimeSpan _requestDelay;
3233

@@ -376,11 +377,12 @@ private async Task StartSendingMessages(CancellationToken cancellationToken)
376377
/// <returns>Task</returns>
377378
private async void ReadTcp(CancellationToken cancellationToken)
378379
{
380+
var lengthArray = new byte[sizeof(int)];
381+
379382
try
380383
{
381384
while (!IsDisposed)
382385
{
383-
var lengthArray = new byte[sizeof(int)];
384386

385387
var readBytes = 0;
386388

@@ -400,7 +402,7 @@ private async void ReadTcp(CancellationToken cancellationToken)
400402

401403
if (length <= 0) continue;
402404

403-
var data = new byte[length];
405+
var data = ArrayPool<byte>.Shared.Rent(length);
404406

405407
readBytes = 0;
406408

@@ -414,7 +416,9 @@ private async void ReadTcp(CancellationToken cancellationToken)
414416
}
415417
while (readBytes < length);
416418

417-
var message = ProtoMessage.Parser.ParseFrom(data);
419+
var message = ProtoMessage.Parser.ParseFrom(data, 0, length);
420+
421+
ArrayPool<byte>.Shared.Return(data);
418422

419423
OnNext(message);
420424
}

src/WPF.Sample/App.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
5454
containerRegistry.RegisterDialog<AccountAuthView>();
5555

5656
// Services
57-
OpenClient liveClientFactory() => new OpenClient(ApiInfo.LiveHost, ApiInfo.Port, TimeSpan.FromSeconds(10));
58-
OpenClient demoClientFactory() => new OpenClient(ApiInfo.DemoHost, ApiInfo.Port, TimeSpan.FromSeconds(10));
57+
OpenClient liveClientFactory() => new(ApiInfo.LiveHost, ApiInfo.Port, TimeSpan.FromSeconds(10));
58+
OpenClient demoClientFactory() => new(ApiInfo.DemoHost, ApiInfo.Port, TimeSpan.FromSeconds(10));
5959

6060
var apiService = new ApiService(liveClientFactory, demoClientFactory);
6161

src/WPF.Sample/Services/ChartingService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IChartingService : IDisposable
1212

1313
public sealed class ChartingService : IChartingService
1414
{
15-
private readonly List<IDisposable> _disposables = new List<IDisposable>();
15+
private readonly List<IDisposable> _disposables = new();
1616

1717
public void Dispose()
1818
{

src/WinForms.Sample/Main.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public partial class Main : Form
3535
// private int _apiPort = 5035;
3636
private readonly OpenClient _client;
3737

38-
private static Queue _writeQueue = new Queue(); // not thread safe
39-
private static Queue _readQueue = new Queue(); // not thread safe
38+
private static Queue _writeQueue = new(); // not thread safe
39+
private static Queue _readQueue = new(); // not thread safe
4040
private static Queue writeQueueSync = Queue.Synchronized(_writeQueue); // thread safe
4141
private static Queue readQueueSync = Queue.Synchronized(_readQueue); // thread safe
4242
private static volatile bool isShutdown;

0 commit comments

Comments
 (0)