2013-12-07 08:52:38 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-27 15:13:46 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-12-07 08:52:38 -07:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-02-26 09:10:55 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-23 15:31:51 -07:00
|
|
|
|
using System;
|
2013-05-27 11:34:03 -07:00
|
|
|
|
using System.Linq;
|
2013-02-23 15:31:51 -07:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-03-06 22:34:00 -07:00
|
|
|
|
namespace MediaBrowser.Server.Implementations.Udp
|
2013-02-23 15:31:51 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a Udp Server
|
|
|
|
|
/// </summary>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
public class UdpServer : IDisposable
|
2013-02-23 15:31:51 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
/// The _logger
|
2013-02-23 15:31:51 -07:00
|
|
|
|
/// </summary>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
private readonly ILogger _logger;
|
2013-02-23 15:31:51 -07:00
|
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _network manager
|
|
|
|
|
/// </summary>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
private readonly INetworkManager _networkManager;
|
2013-05-27 11:34:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _HTTP server
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IHttpServer _httpServer;
|
2013-02-26 09:10:55 -07:00
|
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _server configuration manager
|
|
|
|
|
/// </summary>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
2013-04-10 09:04:13 -07:00
|
|
|
|
|
2013-12-07 08:52:38 -07:00
|
|
|
|
private bool _isDisposed;
|
|
|
|
|
|
2013-02-26 09:10:55 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="UdpServer" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
/// <param name="networkManager">The network manager.</param>
|
2013-04-10 09:04:13 -07:00
|
|
|
|
/// <param name="serverConfigurationManager">The server configuration manager.</param>
|
2013-05-27 11:34:03 -07:00
|
|
|
|
/// <param name="httpServer">The HTTP server.</param>
|
|
|
|
|
public UdpServer(ILogger logger, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager, IHttpServer httpServer)
|
2013-02-26 09:10:55 -07:00
|
|
|
|
{
|
2013-03-27 15:13:46 -07:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_networkManager = networkManager;
|
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2013-05-27 11:34:03 -07:00
|
|
|
|
_httpServer = httpServer;
|
2013-02-26 09:10:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 15:31:51 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raises the <see cref="E:MessageReceived" /> event.
|
|
|
|
|
/// </summary>
|
2013-04-10 09:04:13 -07:00
|
|
|
|
/// <param name="e">The <see cref="UdpMessageReceivedEventArgs"/> instance containing the event data.</param>
|
2013-03-27 15:13:46 -07:00
|
|
|
|
private async void OnMessageReceived(UdpMessageReceivedEventArgs e)
|
2013-02-23 15:31:51 -07:00
|
|
|
|
{
|
2013-04-10 09:04:13 -07:00
|
|
|
|
const string context = "Server";
|
2013-03-27 15:13:46 -07:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
|
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);
|
2013-03-27 15:13:46 -07:00
|
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
|
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.");
|
|
|
|
|
}
|
2013-03-27 15:13:46 -07:00
|
|
|
|
}
|
2013-02-23 15:31:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the local ip address.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string GetLocalIpAddress()
|
|
|
|
|
{
|
|
|
|
|
var localAddresses = _networkManager.GetLocalIpAddresses().ToList();
|
|
|
|
|
|
|
|
|
|
// Cross-check the local ip addresses with addresses that have been received on with the http server
|
|
|
|
|
var matchedAddress = _httpServer.LocalEndPoints
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(i => i.Split(':').FirstOrDefault())
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.FirstOrDefault(i => localAddresses.Contains(i, StringComparer.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
// Return the first matched address, if found, or the first known local address
|
|
|
|
|
return matchedAddress ?? localAddresses.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 15:31:51 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _udp client
|
|
|
|
|
/// </summary>
|
|
|
|
|
private UdpClient _udpClient;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the specified port.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="port">The port.</param>
|
|
|
|
|
public void Start(int port)
|
|
|
|
|
{
|
|
|
|
|
_udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, port));
|
|
|
|
|
|
|
|
|
|
_udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
|
|
2013-12-07 08:52:38 -07:00
|
|
|
|
Task.Run(() => StartListening());
|
2013-02-23 15:31:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-07 08:52:38 -07:00
|
|
|
|
private async void StartListening()
|
|
|
|
|
{
|
|
|
|
|
while (!_isDisposed)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await GetResult().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
OnMessageReceived(result);
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task<UdpReceiveResult> GetResult()
|
2013-02-23 15:31:51 -07:00
|
|
|
|
{
|
2013-12-07 08:52:38 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _udpClient.ReceiveAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(new UdpReceiveResult(new byte[] { }, new IPEndPoint(IPAddress.Any, 0)));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error receiving udp message", ex);
|
|
|
|
|
return Task.FromResult(new UdpReceiveResult(new byte[] { }, new IPEndPoint(IPAddress.Any, 0)));
|
|
|
|
|
}
|
2013-02-23 15:31:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [message received].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void OnMessageReceived(UdpReceiveResult message)
|
|
|
|
|
{
|
2013-02-26 09:10:55 -07:00
|
|
|
|
if (message.RemoteEndPoint.Port == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-02-23 15:31:51 -07:00
|
|
|
|
var bytes = message.Buffer;
|
|
|
|
|
|
|
|
|
|
OnMessageReceived(new UdpMessageReceivedEventArgs
|
|
|
|
|
{
|
|
|
|
|
Bytes = bytes,
|
|
|
|
|
RemoteEndPoint = message.RemoteEndPoint.ToString()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops this instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2013-12-07 08:52:38 -07:00
|
|
|
|
_isDisposed = true;
|
|
|
|
|
|
2013-03-09 23:45:16 -07:00
|
|
|
|
if (_udpClient != null)
|
|
|
|
|
{
|
|
|
|
|
_udpClient.Close();
|
|
|
|
|
}
|
2013-02-23 15:31:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the async.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">The data.</param>
|
|
|
|
|
/// <param name="ipAddress">The ip address.</param>
|
|
|
|
|
/// <param name="port">The port.</param>
|
|
|
|
|
/// <returns>Task{System.Int32}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">data</exception>
|
|
|
|
|
public Task SendAsync(string data, string ipAddress, int port)
|
|
|
|
|
{
|
|
|
|
|
return SendAsync(Encoding.UTF8.GetBytes(data), ipAddress, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the async.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">The bytes.</param>
|
|
|
|
|
/// <param name="ipAddress">The ip address.</param>
|
|
|
|
|
/// <param name="port">The port.</param>
|
|
|
|
|
/// <returns>Task{System.Int32}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">bytes</exception>
|
|
|
|
|
public Task SendAsync(byte[] bytes, string ipAddress, int port)
|
|
|
|
|
{
|
|
|
|
|
if (bytes == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("bytes");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(ipAddress))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ipAddress");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _udpClient.SendAsync(bytes, bytes.Length, ipAddress, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the async.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">The bytes.</param>
|
|
|
|
|
/// <param name="remoteEndPoint">The remote end point.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-05-27 11:34:03 -07:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// bytes
|
|
|
|
|
/// or
|
|
|
|
|
/// remoteEndPoint
|
|
|
|
|
/// </exception>
|
2013-03-12 15:51:10 -07:00
|
|
|
|
public async Task SendAsync(byte[] bytes, string remoteEndPoint)
|
2013-02-23 15:31:51 -07:00
|
|
|
|
{
|
|
|
|
|
if (bytes == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("bytes");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(remoteEndPoint))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("remoteEndPoint");
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-05 10:13:32 -07:00
|
|
|
|
await _udpClient.SendAsync(bytes, bytes.Length, _networkManager.Parse(remoteEndPoint)).ConfigureAwait(false);
|
2013-03-12 15:51:10 -07:00
|
|
|
|
|
2013-03-27 15:13:46 -07:00
|
|
|
|
_logger.Info("Udp message sent to {0}", remoteEndPoint);
|
2013-02-23 15:31:51 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|