Skip to content

Commit

Permalink
Added cancellation token parameter for both ReceiveDataAsync methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcidev committed Feb 24, 2017
1 parent 07c2e6d commit 55e0927
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Arci.Networking/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Arci.Networking.Security;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Arci.Networking
{
Expand Down Expand Up @@ -77,10 +78,11 @@ public IEnumerable<Packet> ReceiveData(bool decrypt)
/// Receive data as a list of packets from server
/// </summary>
/// <param name="decrypt">Decrypt data with Aes key if set</param>
/// <param name="token">Token to cancel awaited reading</param>
/// <returns>List of packets received from server. Blocks thread until data become available.</returns>
public async Task<IEnumerable<Packet>> ReceiveDataAsync(bool decrypt)
public async Task<IEnumerable<Packet>> ReceiveDataAsync(bool decrypt, CancellationToken? token = null)
{
return transformStreamToPackets(await ReceiveDataAsync(), decrypt);
return transformStreamToPackets(await ReceiveDataAsync(token), decrypt);
}

/// <summary>
Expand All @@ -98,10 +100,11 @@ public byte[] ReceiveData()
/// <summary>
/// Receives data as byte stream asynchronously
/// </summary>
/// <param name="token">Token to cancel awaited reading</param>
/// <returns>Byte stream of received data. Blocks thread until data become available.</returns>
public async Task<byte[]> ReceiveDataAsync()
public async Task<byte[]> ReceiveDataAsync(CancellationToken? token = null)
{
return await readDataAsync();
return await readDataAsync(token);
}

/// <summary>
Expand All @@ -128,10 +131,10 @@ private byte[] readData()
return data.Take(length).ToArray();
}

private async Task<byte[]> readDataAsync()
private async Task<byte[]> readDataAsync(CancellationToken? token)
{
var data = new byte[Packet.MaxPacketSize];
int length = await stream.ReadAsync(data, 0, Packet.MaxPacketSize);
int length = token.HasValue ? await stream.ReadAsync(data, 0, Packet.MaxPacketSize, token.Value) : await stream.ReadAsync(data, 0, Packet.MaxPacketSize);
if (length == 0)
return null;

Expand Down

0 comments on commit 55e0927

Please sign in to comment.