/// <summary> /// Raises the <see cref="E:MessageReceived" /> event. /// </summary> /// <param name="e">The <see cref="UdpMessageReceivedEventArgs"/> instance containing the event data.</param> private void OnMessageReceived(UdpMessageReceivedEventArgs e) { var responder = _responders.FirstOrDefault(i => i.Item2.SequenceEqual(e.Bytes)); var encoding = Encoding.UTF8; if (responder == null) { var text = Encoding.Unicode.GetString(e.Bytes); responder = _responders.FirstOrDefault(i => string.Equals(i.Item1, text, StringComparison.OrdinalIgnoreCase)); if (responder != null) { encoding = Encoding.Unicode; } } if (responder != null) { try { responder.Item3(e.RemoteEndPoint, encoding); } catch (Exception ex) { _logger.ErrorException("Error in OnMessageReceived", ex); } } }
/// <summary> /// Raises the <see cref="E:MessageReceived" /> event. /// </summary> /// <param name="e">The <see cref="UdpMessageReceivedEventArgs"/> instance containing the event data.</param> private async void OnMessageReceived(UdpMessageReceivedEventArgs e) { const string context = "Server"; var expectedMessage = String.Format("who is MediaBrowser{0}?", context); var expectedMessageBytes = Encoding.UTF8.GetBytes(expectedMessage); if (expectedMessageBytes.SequenceEqual(e.Bytes)) { _logger.Info("Received UDP server request from " + e.RemoteEndPoint); var localAddress = GetLocalIpAddress(); if (!string.IsNullOrEmpty(localAddress)) { // Send a response back with our ip address and port var response = String.Format("MediaBrowser{0}|{1}:{2}", context, GetLocalIpAddress(), _serverConfigurationManager.Configuration.HttpServerPortNumber); await SendAsync(Encoding.UTF8.GetBytes(response), e.RemoteEndPoint); } else { _logger.Warn("Unable to respond to udp request because the local ip address could not be determined."); } } }
/// <summary> /// Raises the <see cref="E:MessageReceived" /> event. /// </summary> /// <param name="e">The <see cref="UdpMessageReceivedEventArgs"/> instance containing the event data.</param> private void OnMessageReceived(UdpMessageReceivedEventArgs e) { var responder = _responders.FirstOrDefault(i => i.Item1.SequenceEqual(e.Bytes)); if (responder != null) { try { responder.Item2(e.RemoteEndPoint); } catch (Exception ex) { _logger.ErrorException("Error in OnMessageReceived", ex); } } }
/// <summary> /// Raises the <see cref="E:MessageReceived" /> event. /// </summary> /// <param name="e">The <see cref="UdpMessageReceivedEventArgs" /> instance containing the event data.</param> private async void OnMessageReceived(UdpMessageReceivedEventArgs e) { var context = "Server"; var expectedMessage = String.Format("who is MediaBrowser{0}?", context); var expectedMessageBytes = Encoding.UTF8.GetBytes(expectedMessage); if (expectedMessageBytes.SequenceEqual(e.Bytes)) { _logger.Info("Received UDP server request from " + e.RemoteEndPoint); // Send a response back with our ip address and port var response = String.Format("MediaBrowser{0}|{1}:{2}", context, _networkManager.GetLocalIpAddress(), _serverConfigurationManager.Configuration.HttpServerPortNumber); await SendAsync(Encoding.UTF8.GetBytes(response), e.RemoteEndPoint); } }
/// <summary> /// Raises the <see cref="E:MessageReceived" /> event. /// </summary> /// <param name="e">The <see cref="UdpMessageReceivedEventArgs"/> instance containing the event data.</param> private async void OnMessageReceived(UdpMessageReceivedEventArgs e) { var encoding = Encoding.UTF8; var responder = GetResponder(e.Bytes, encoding); if (responder == null) { encoding = Encoding.Unicode; responder = GetResponder(e.Bytes, encoding); } if (responder != null) { try { await responder.Item2.Item3(responder.Item1, e.RemoteEndPoint, encoding).ConfigureAwait(false); } catch (Exception ex) { _logger.ErrorException("Error in OnMessageReceived", ex); } } }