mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
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:
parent
a425d142f9
commit
549b831577
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue