From 55e09278a33f32c88e6266d785dff5bc1f85a21d Mon Sep 17 00:00:00 2001 From: Arci Date: Fri, 24 Feb 2017 22:36:10 +0100 Subject: [PATCH] Added cancellation token parameter for both ReceiveDataAsync methods --- Arci.Networking/Client.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Arci.Networking/Client.cs b/Arci.Networking/Client.cs index ea10d5d..2cd4d85 100644 --- a/Arci.Networking/Client.cs +++ b/Arci.Networking/Client.cs @@ -5,6 +5,7 @@ using Arci.Networking.Security; using System.Collections.Generic; using System.Threading.Tasks; +using System.Threading; namespace Arci.Networking { @@ -77,10 +78,11 @@ public IEnumerable ReceiveData(bool decrypt) /// Receive data as a list of packets from server /// /// Decrypt data with Aes key if set + /// Token to cancel awaited reading /// List of packets received from server. Blocks thread until data become available. - public async Task> ReceiveDataAsync(bool decrypt) + public async Task> ReceiveDataAsync(bool decrypt, CancellationToken? token = null) { - return transformStreamToPackets(await ReceiveDataAsync(), decrypt); + return transformStreamToPackets(await ReceiveDataAsync(token), decrypt); } /// @@ -98,10 +100,11 @@ public byte[] ReceiveData() /// /// Receives data as byte stream asynchronously /// + /// Token to cancel awaited reading /// Byte stream of received data. Blocks thread until data become available. - public async Task ReceiveDataAsync() + public async Task ReceiveDataAsync(CancellationToken? token = null) { - return await readDataAsync(); + return await readDataAsync(token); } /// @@ -128,10 +131,10 @@ private byte[] readData() return data.Take(length).ToArray(); } - private async Task readDataAsync() + private async Task 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;