Skip to content

Commit

Permalink
Merge pull request #14 from spotware/dev
Browse files Browse the repository at this point in the history
Fixed the issue with using array pool instead of allocating new array…
  • Loading branch information
amusleh-spotware-com authored Mar 28, 2022
2 parents 0982e94 + 47664b7 commit 13c9679
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/OpenAPI.Net/OpenAPI.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageTags>cTrader, Open API, Spotware, cTrader</PackageTags>
<Description>A .NET RX library for cTrader Open API</Description>
<PackageId>cTrader.OpenAPI.Net</PackageId>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<Platforms>AnyCPU</Platforms>
<Company>Spotware</Company>
<Authors>Spotware</Authors>
Expand Down
9 changes: 7 additions & 2 deletions src/OpenAPI.Net/OpenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ private async Task StartSendingMessages(CancellationToken cancellationToken)
private async void ReadTcp(CancellationToken cancellationToken)
{
var dataLength = new byte[4];
byte[] data = null;

try
{
Expand All @@ -399,13 +400,13 @@ private async void ReadTcp(CancellationToken cancellationToken)

if (length <= 0) continue;

var data = new byte[length];
data = ArrayPool<byte>.Shared.Rent(length);

readBytes = 0;

do
{
var count = data.Length - readBytes;
var count = length - readBytes;

readBytes += await _sslStream.ReadAsync(data, readBytes, count, cancellationToken).ConfigureAwait(false);

Expand All @@ -415,11 +416,15 @@ private async void ReadTcp(CancellationToken cancellationToken)

var message = ProtoMessage.Parser.ParseFrom(data, 0, length);

ArrayPool<byte>.Shared.Return(data);

OnNext(message);
}
}
catch (Exception ex)
{
if (data is not null) ArrayPool<byte>.Shared.Return(data);

var exception = new ReceiveException(ex);

OnError(exception);
Expand Down

0 comments on commit 13c9679

Please sign in to comment.