Skip to content

Commit

Permalink
Merge pull request #12 from spotware/dev
Browse files Browse the repository at this point in the history
Removed arrary pool
  • Loading branch information
amusleh-spotware-com authored Mar 25, 2022
2 parents e8e3893 + 8996b0f commit 0982e94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 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.0</Version>
<Version>1.4.1</Version>
<Platforms>AnyCPU</Platforms>
<Company>Spotware</Company>
<Authors>Spotware</Authors>
Expand Down
38 changes: 22 additions & 16 deletions src/OpenAPI.Net/OpenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,32 +377,29 @@ private async Task StartSendingMessages(CancellationToken cancellationToken)
/// <returns>Task</returns>
private async void ReadTcp(CancellationToken cancellationToken)
{
var lengthArray = new byte[sizeof(int)];
var dataLength = new byte[4];

try
{
while (!IsDisposed)
{

var readBytes = 0;

do
{
var count = lengthArray.Length - readBytes;
var count = dataLength.Length - readBytes;

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

if (readBytes == 0) new InvalidOperationException("Remote host closed the connection");
if (readBytes == 0) throw new InvalidOperationException("Remote host closed the connection");
}
while (readBytes < lengthArray.Length);

Array.Reverse(lengthArray);
while (readBytes < dataLength.Length);

var length = BitConverter.ToInt32(lengthArray, 0);
var length = GetLength(dataLength);

if (length <= 0) continue;

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

readBytes = 0;

Expand All @@ -412,20 +409,15 @@ private async void ReadTcp(CancellationToken cancellationToken)

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

if (readBytes == 0) new InvalidOperationException("Remote host closed the connection");
if (readBytes == 0) throw new InvalidOperationException("Remote host closed the connection");
}
while (readBytes < length);

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

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

OnNext(message);
}
}
catch (Exception ex) when (ex is OperationCanceledException)
{
}
catch (Exception ex)
{
var exception = new ReceiveException(ex);
Expand All @@ -434,6 +426,20 @@ private async void ReadTcp(CancellationToken cancellationToken)
}
}

/// <summary>
/// Returns the length of a received message without causing extra allocation
/// </summary>
/// <param name="lengthBytes">The byte arrary of received lenght data</param>
/// <returns>int</returns>
private int GetLength(byte[] lengthBytes)
{
var lengthSpan = lengthBytes.AsSpan();

lengthSpan.Reverse();

return BitConverter.ToInt32(lengthSpan);
}

/// <summary>
/// Writes the message bytes to TCP stream
/// </summary>
Expand Down

0 comments on commit 0982e94

Please sign in to comment.