diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-11-04 19:57:21 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-11-04 19:57:21 -0400 |
| commit | 25312d7d03af665818cfd2cee2edb549e0e940f2 (patch) | |
| tree | 2220b08d58fb36b0b581a60cdc167d72e628b916 /RSSDP | |
| parent | 67ad1db6b77b2c2cb6d81c22808d99564a5f3ebc (diff) | |
add udp error handling
Diffstat (limited to 'RSSDP')
| -rw-r--r-- | RSSDP/SsdpCommunicationsServer.cs | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/RSSDP/SsdpCommunicationsServer.cs b/RSSDP/SsdpCommunicationsServer.cs index dadb1bff4..c0b9c6542 100644 --- a/RSSDP/SsdpCommunicationsServer.cs +++ b/RSSDP/SsdpCommunicationsServer.cs @@ -79,17 +79,6 @@ namespace Rssdp.Infrastructure } /// <summary> - /// Partial constructor. - /// </summary> - /// <param name="socketFactory">An implementation of the <see cref="ISocketFactory"/> interface that can be used to make new unicast and multicast sockets. Cannot be null.</param> - /// <param name="localPort">The specific local port to use for all sockets created by this instance. Specify zero to indicate the system should choose a free port itself.</param> - /// <exception cref="System.ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception> - public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort) - : this(socketFactory, localPort, SsdpConstants.SsdpDefaultMulticastTimeToLive) - { - } - - /// <summary> /// Full constructor. /// </summary> /// <param name="socketFactory">An implementation of the <see cref="ISocketFactory"/> interface that can be used to make new unicast and multicast sockets. Cannot be null.</param> @@ -170,7 +159,12 @@ namespace Rssdp.Infrastructure EnsureSendSocketCreated(); // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. - await Repeat(SsdpConstants.UdpResendCount, TimeSpan.FromMilliseconds(100), () => SendMessageIfSocketNotDisposed(messageData, destination)).ConfigureAwait(false); + for (var i = 0; i < SsdpConstants.UdpResendCount; i++) + { + await SendMessageIfSocketNotDisposed(messageData, destination).ConfigureAwait(false); + + await Task.Delay(100).ConfigureAwait(false); + } } /// <summary> @@ -188,8 +182,17 @@ namespace Rssdp.Infrastructure EnsureSendSocketCreated(); // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. - await Repeat(SsdpConstants.UdpResendCount, TimeSpan.FromMilliseconds(100), - () => SendMessageIfSocketNotDisposed(messageData, new IpEndPointInfo() { IpAddress = new IpAddressInfo { Address = SsdpConstants.MulticastLocalAdminAddress }, Port = SsdpConstants.MulticastPort })).ConfigureAwait(false); + for (var i = 0; i < SsdpConstants.UdpResendCount; i++) + { + await SendMessageIfSocketNotDisposed(messageData, new IpEndPointInfo + { + IpAddress = new IpAddressInfo { Address = SsdpConstants.MulticastLocalAdminAddress }, + Port = SsdpConstants.MulticastPort + + }).ConfigureAwait(false); + + await Task.Delay(100).ConfigureAwait(false); + } } /// <summary> @@ -255,28 +258,16 @@ namespace Rssdp.Infrastructure #region Private Methods - private async Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination) + private Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination) { var socket = _SendSocket; if (socket != null) { - await _SendSocket.SendAsync(messageData, messageData.Length, destination).ConfigureAwait(false); - } - else - { - ThrowIfDisposed(); + return _SendSocket.SendAsync(messageData, messageData.Length, destination); } - } - - private static async Task Repeat(int repetitions, TimeSpan delay, Func<Task> work) - { - for (int cnt = 0; cnt < repetitions; cnt++) - { - await work().ConfigureAwait(false); - if (delay != TimeSpan.Zero) - await Task.Delay(delay).ConfigureAwait(false); - } + ThrowIfDisposed(); + return Task.FromResult(true); } private IUdpSocket ListenForBroadcastsAsync() |
