fix(agent): async UDP receive hangs — explicit timeout via Task.WhenAny
Refs #54 #55 #56 Socket.ReceiveTimeout only applies to synchronous Receive() — the async ReceiveAsync(ct) on the ONVIF multicast, DVRIP broadcast and unicast ONVIF probe waited forever when no camera replied, so a scan never completed (no response → hang). Enforce the listen window explicitly with Task.WhenAny(receiveTask, Task.Delay(...)): - WS-Discovery multicast: 5s listen window - DVRIP broadcast: 2s listen window - unicast ONVIF probe: OnvifUdpTimeout (500ms) Also fixed ValueTask→Task (.AsTask()) for the WhenAny overload.
This commit is contained in:
1 parent
59a864bdfc
commit
33a08f5ea0
1 file changed
+26
-29
@@ -222,21 +222,17 @@ public sealed class NetworkCameraScanner
|
||||
await Task.Delay(100, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
while (!ct.IsCancellationRequested)
|
||||
// Socket.ReceiveTimeout does NOT apply to async ReceiveAsync —
|
||||
// enforce the window explicitly or the scan hangs forever.
|
||||
var listenUntil = DateTime.UtcNow + TimeSpan.FromSeconds(2);
|
||||
while (!ct.IsCancellationRequested && DateTime.UtcNow < listenUntil)
|
||||
{
|
||||
UdpReceiveResult result;
|
||||
try
|
||||
{
|
||||
result = await udp.ReceiveAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
break; // receive timeout
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
var receiveTask = udp.ReceiveAsync(ct).AsTask();
|
||||
var completed = await Task.WhenAny(receiveTask, Task.Delay(TimeSpan.FromSeconds(2), ct)).ConfigureAwait(false);
|
||||
if (completed != receiveTask)
|
||||
break; // no more replies expected
|
||||
result = await receiveTask.ConfigureAwait(false);
|
||||
|
||||
var json = Encoding.UTF8.GetString(result.Buffer);
|
||||
var info = ParseDvripNetCommon(json);
|
||||
@@ -344,23 +340,18 @@ public sealed class NetworkCameraScanner
|
||||
|
||||
var multicast = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 3702);
|
||||
await udp.SendAsync(Encoding.UTF8.GetBytes(probe), multicast, ct).ConfigureAwait(false);
|
||||
udp.Client.ReceiveTimeout = 5000;
|
||||
|
||||
while (!ct.IsCancellationRequested)
|
||||
// Socket.ReceiveTimeout does NOT apply to async ReceiveAsync —
|
||||
// enforce the window explicitly or the scan hangs forever.
|
||||
var listenUntil = DateTime.UtcNow + TimeSpan.FromSeconds(5);
|
||||
while (!ct.IsCancellationRequested && DateTime.UtcNow < listenUntil)
|
||||
{
|
||||
UdpReceiveResult result;
|
||||
try
|
||||
{
|
||||
result = await udp.ReceiveAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
break; // receive timeout
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
var receiveTask = udp.ReceiveAsync(ct).AsTask();
|
||||
var completed = await Task.WhenAny(receiveTask, Task.Delay(TimeSpan.FromSeconds(5), ct)).ConfigureAwait(false);
|
||||
if (completed != receiveTask)
|
||||
break; // no more replies expected
|
||||
result = await receiveTask.ConfigureAwait(false);
|
||||
|
||||
var xml = Encoding.UTF8.GetString(result.Buffer);
|
||||
if (!xml.Contains("onvif", StringComparison.OrdinalIgnoreCase))
|
||||
@@ -556,7 +547,6 @@ public sealed class NetworkCameraScanner
|
||||
try
|
||||
{
|
||||
using var udp = new UdpClient();
|
||||
udp.Client.ReceiveTimeout = (int)OnvifUdpTimeout.TotalMilliseconds;
|
||||
udp.Connect(ip, port);
|
||||
|
||||
var messageId = Guid.NewGuid().ToString();
|
||||
@@ -578,7 +568,14 @@ public sealed class NetworkCameraScanner
|
||||
</soap:Envelope>";
|
||||
|
||||
await udp.SendAsync(Encoding.UTF8.GetBytes(probe), ct).ConfigureAwait(false);
|
||||
var result = await udp.ReceiveAsync(ct).ConfigureAwait(false);
|
||||
|
||||
// Socket.ReceiveTimeout does NOT apply to async ReceiveAsync —
|
||||
// enforce the window explicitly or the probe hangs forever.
|
||||
var receiveTask = udp.ReceiveAsync(ct).AsTask();
|
||||
var completed = await Task.WhenAny(receiveTask, Task.Delay(OnvifUdpTimeout, ct)).ConfigureAwait(false);
|
||||
if (completed != receiveTask)
|
||||
return null;
|
||||
var result = await receiveTask.ConfigureAwait(false);
|
||||
var xml = Encoding.UTF8.GetString(result.Buffer);
|
||||
|
||||
var doc = XDocument.Parse(xml);
|
||||
|
||||
Reference in new issue
Block a user