mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 11:45:43 +08:00
Fix Xamarin connect freeze (#1545)
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
* [Client] Fixed connection freeze when using Xamarin etc.
|
||||||
@@ -123,7 +123,7 @@ namespace MQTTnet.Tests.Client
|
|||||||
//Assert.IsInstanceOfType(exception.InnerException, typeof(SocketException));
|
//Assert.IsInstanceOfType(exception.InnerException, typeof(SocketException));
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(100); // disconnected handler is called async
|
await LongTestDelay(); // disconnected handler is called async
|
||||||
Assert.IsTrue(disconnectHandlerCalled);
|
Assert.IsTrue(disconnectHandlerCalled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ namespace MQTTnet.Adapter
|
|||||||
{
|
{
|
||||||
const uint ErrorOperationAborted = 0x800703E3;
|
const uint ErrorOperationAborted = 0x800703E3;
|
||||||
const int ReadBufferSize = 4096;
|
const int ReadBufferSize = 4096;
|
||||||
|
|
||||||
readonly IMqttChannel _channel;
|
readonly IMqttChannel _channel;
|
||||||
readonly byte[] _fixedHeaderBuffer = new byte[2];
|
readonly byte[] _fixedHeaderBuffer = new byte[2];
|
||||||
readonly MqttNetSourceLogger _logger;
|
readonly MqttNetSourceLogger _logger;
|
||||||
@@ -73,7 +73,26 @@ namespace MQTTnet.Adapter
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _channel.ConnectAsync(cancellationToken).ConfigureAwait(false);
|
/*
|
||||||
|
* We have to implement a small workaround here to support connecting in Xamarin
|
||||||
|
* with a disabled WiFi network. If the WiFi is disabled the connect method will
|
||||||
|
* block forever. Even a cancellation token is not supported properly.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var connectTask = _channel.ConnectAsync(cancellationToken);
|
||||||
|
|
||||||
|
var timeout = new TaskCompletionSource<object>();
|
||||||
|
using (cancellationToken.Register(() => timeout.TrySetResult(null)))
|
||||||
|
{
|
||||||
|
await Task.WhenAny(connectTask, timeout.Task).ConfigureAwait(false);
|
||||||
|
if (timeout.Task.IsCompleted && !connectTask.IsCompleted)
|
||||||
|
{
|
||||||
|
throw new OperationCanceledException("MQTT connect cancelled.", cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure that the exception from the connect task gets thrown.
|
||||||
|
await connectTask.ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user