void _ssdpHandler_MessageReceived(object sender, SsdpMessageEventArgs e) { string nts; e.Headers.TryGetValue("NTS", out nts); if (String.Equals(e.Method, "NOTIFY", StringComparison.OrdinalIgnoreCase) && String.Equals(nts, "ssdp:byebye", StringComparison.OrdinalIgnoreCase) && !_disposed) { EventHelper.FireEventIfNotNull(DeviceLeft, this, e, _logger); return; } try { var ip = _networkManager.GetLocalIpAddresses().FirstOrDefault(); if (ip != null) { e.LocalIp = IPAddress.Parse(ip); TryCreateDevice(e); } } catch (OperationCanceledException) { } catch (Exception ex) { _logger.ErrorException("Error creating play to controller", ex); } }
private void OnMessageReceived(SsdpMessageEventArgs args) { if (string.Equals(args.Method, "M-SEARCH", StringComparison.OrdinalIgnoreCase)) { RespondToSearch(args.EndPoint, args.Headers["st"]); } EventHelper.FireEventIfNotNull(MessageReceived, this, args, _logger); }
private async void OnMessageReceived(SsdpMessageEventArgs args) { if (string.Equals(args.Method, "M-SEARCH", StringComparison.OrdinalIgnoreCase)) { string mx = null; args.Headers.TryGetValue("mx", out mx); int delaySeconds; if (!string.IsNullOrWhiteSpace(mx) && int.TryParse(mx, NumberStyles.Any, CultureInfo.InvariantCulture, out delaySeconds) && delaySeconds > 0) { if (_config.GetDlnaConfiguration().EnableDebugLogging) { _logger.Debug("Delaying search response by {0} seconds", delaySeconds); } await Task.Delay(delaySeconds * 1000).ConfigureAwait(false); } RespondToSearch(args.EndPoint, args.Headers["st"]); } EventHelper.FireEventIfNotNull(MessageReceived, this, args, _logger); }
private async void OnMessageReceived(SsdpMessageEventArgs args) { if (string.Equals(args.Method, "M-SEARCH", StringComparison.OrdinalIgnoreCase)) { var headers = args.Headers; TimeSpan delay = GetSearchDelay(headers); if (_config.GetDlnaConfiguration().EnableDebugLogging) { _logger.Debug("Delaying search response by {0} seconds", delay.TotalSeconds); } await Task.Delay(delay).ConfigureAwait(false); string st; if (headers.TryGetValue("st", out st)) { RespondToSearch(args.EndPoint, st); } } EventHelper.FireEventIfNotNull(MessageReceived, this, args, _logger); }
private void TryCreateDevice(SsdpMessageEventArgs args) { string nts; args.Headers.TryGetValue("NTS", out nts); string usn; if (!args.Headers.TryGetValue("USN", out usn)) usn = string.Empty; string nt; if (!args.Headers.TryGetValue("NT", out nt)) nt = string.Empty; // Ignore when a device is indicating it's shutting down if (string.Equals(nts, "ssdp:byebye", StringComparison.OrdinalIgnoreCase)) { return; } // Need to be able to download device description string location; if (!args.Headers.TryGetValue("Location", out location) || string.IsNullOrEmpty(location)) { return; } if (_config.GetDlnaConfiguration().EnableDebugLogging) { var headerTexts = args.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value)); var headerText = string.Join(",", headerTexts.ToArray()); _logger.Debug("{0} Device message received from {1}. Headers: {2}", args.Method, args.EndPoint, headerText); } EventHelper.FireEventIfNotNull(DeviceDiscovered, this, args, _logger); }