mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 11:03:07 +08:00
Merge branch 'develop' of https://github.com/chkr1011/MQTTnet into develop
# Conflicts: # MQTTnet.Core/Serializer/MqttPacketReader.cs # MQTTnet.Core/Serializer/MqttPacketSerializer.cs # Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Exceptions;
|
||||
|
||||
namespace MQTTnet.Core.Internal
|
||||
{
|
||||
public static class TaskExtensions
|
||||
{
|
||||
public static Task TimeoutAfter(this Task task, TimeSpan timeout)
|
||||
{
|
||||
return TimeoutAfter(task.ContinueWith(t => 0), timeout);
|
||||
}
|
||||
|
||||
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
|
||||
{
|
||||
using (var cancellationTokenSource = new CancellationTokenSource())
|
||||
{
|
||||
var tcs = new TaskCompletionSource<TResult>();
|
||||
|
||||
cancellationTokenSource.Token.Register(() =>
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
cancellationTokenSource.CancelAfter(timeout);
|
||||
task.ContinueWith(t =>
|
||||
{
|
||||
if (t.IsFaulted)
|
||||
{
|
||||
tcs.TrySetException(t.Exception);
|
||||
}
|
||||
|
||||
if (t.IsCompleted)
|
||||
{
|
||||
tcs.TrySetResult(t.Result);
|
||||
}
|
||||
}, cancellationTokenSource.Token);
|
||||
|
||||
return await tcs.Task;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
throw new MqttCommunicationTimedOutException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new MqttCommunicationException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user