private void CreateNotifier(Socket socket) { Task.Factory.StartNew(async(o) => { try { var request = SsdpHelper.CreateRendererSSDP(3); while (true) { socket.SendTo(request, new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900)); var delay = _config.Configuration.DlnaOptions.ClientDiscoveryIntervalSeconds * 1000; await Task.Delay(delay).ConfigureAwait(false); } } catch (OperationCanceledException) { } catch (Exception ex) { _logger.ErrorException("Error in notifier", ex); } }, _tokenSource.Token, TaskCreationOptions.LongRunning); }
/// <summary> /// Creates a socket for the interface and listends for data. /// </summary> /// <param name="localIp">The local ip.</param> /// <param name="networkInterfaceIndex">Index of the network interface.</param> private void CreateListener(IPAddress localIp, int networkInterfaceIndex) { Task.Factory.StartNew(async(o) => { try { var socket = GetMulticastSocket(networkInterfaceIndex); var endPoint = new IPEndPoint(localIp, 1900); socket.Bind(endPoint); _logger.Info("Creating SSDP listener"); var receiveBuffer = new byte[64000]; CreateNotifier(socket); while (!_tokenSource.IsCancellationRequested) { var receivedBytes = await socket.ReceiveAsync(receiveBuffer, 0, 64000); if (receivedBytes > 0) { var args = SsdpHelper.ParseSsdpResponse(receiveBuffer, endPoint); TryCreateController(args, localIp); } } _logger.Info("SSDP listener - Task completed"); } catch (OperationCanceledException) { } catch (Exception e) { _logger.ErrorException("Error in listener", e); } }, _tokenSource.Token, TaskCreationOptions.LongRunning); }
/// <summary> /// Creates a socket for the interface and listends for data. /// </summary> /// <param name="localIp">The local ip.</param> private void CreateListener(IPAddress localIp) { Task.Factory.StartNew(async(o) => { try { var socket = GetMulticastSocket(); socket.Bind(new IPEndPoint(localIp, 0)); _logger.Info("Creating SSDP listener"); var receiveBuffer = new byte[64000]; CreateNotifier(socket); while (!_tokenSource.IsCancellationRequested) { var receivedBytes = await socket.ReceiveAsync(receiveBuffer, 0, 64000); if (receivedBytes > 0) { var rawData = Encoding.UTF8.GetString(receiveBuffer, 0, receivedBytes); var uri = SsdpHelper.ParseSsdpResponse(rawData); TryCreateController(uri); } } _logger.Info("SSDP listener - Task completed"); } catch (OperationCanceledException c) { } catch (Exception e) { _logger.ErrorException("Error in listener", e); } }, _tokenSource.Token, TaskCreationOptions.LongRunning); }