Instead of relying on a cancellation token, which caused some confusion on client implementation,

Loop will run within constrained intervals which will be the longer waiting period for a scheduled action to run.
This commit is contained in:
Paulo Rogerio Panhoto 2018-02-22 11:12:32 -03:00
parent a425d142f9
commit 549b831577
3 changed files with 13 additions and 9 deletions

View file

@ -159,9 +159,9 @@ namespace TLSharp.Core.Network
return null;
}
public async Task<byte[]> Receive(CancellationToken token)
public async Task<byte[]> Receive(int timeoutms)
{
var result = DecodeMessage ((await _transport.Receieve (token)).Body);
var result = DecodeMessage ((await _transport.Receieve (timeoutms)).Body);
using (var messageStream = new MemoryStream (result.Item1, false))
using (var messageReader = new BinaryReader (messageStream))

View file

@ -87,12 +87,16 @@ namespace TLSharp.Core.Network
return new TcpMessage(seq, body);
}
public async Task<TcpMessage> Receieve(CancellationToken token)
public async Task<TcpMessage> Receieve(int timeoutms)
{
var stream = _tcpClient.GetStream();
var packetLengthBytes = new byte[4];
if (await stream.ReadAsync(packetLengthBytes, 0, 4, token) != 4)
var recvTask = stream.ReadAsync(packetLengthBytes, 0, 4);
var task = await Task.WhenAny(recvTask, Task.Delay(timeoutms));
if (task != recvTask)
throw new TimeoutException();
if (recvTask.Result != 4)
throw new InvalidOperationException("Couldn't read the packet length");
int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);

View file

@ -117,15 +117,15 @@ namespace TLSharp.Core
}
}
public async Task MainLoopAsync(CancellationTokenSource source)
public async Task MainLoopAsync(int timeslicems)
{
for (;;)
{
try
{
logger.Trace("Socket waiting");
await WaitEventAsync(source.Token);
} catch (OperationCanceledException)
await WaitEventAsync(timeslicems);
} catch (TimeoutException)
{
}
finally
@ -162,9 +162,9 @@ namespace TLSharp.Core
}
}
public async Task WaitEventAsync(CancellationToken token)
public async Task WaitEventAsync(int timeoutms)
{
await _sender.Receive (token);
await _sender.Receive (timeoutms);
}
public bool IsUserAuthorized()