2013-02-26 21:05:52 +00:00
|
|
|
|
using MediaBrowser.Common.Implementations.NetworkManagement;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-02-26 16:10:55 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-23 22:31:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-02-26 21:05:52 +00:00
|
|
|
|
namespace MediaBrowser.Common.Implementations.Udp
|
2013-02-23 22:31:51 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a Udp Server
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UdpServer : IUdpServer
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when [message received].
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<UdpMessageReceivedEventArgs> MessageReceived;
|
|
|
|
|
|
2013-02-26 16:10:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The logger.</value>
|
|
|
|
|
private ILogger Logger { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="UdpServer" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
public UdpServer(ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
Logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raises the <see cref="E:MessageReceived" /> event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">The <see cref="UdpMessageReceivedEventArgs" /> instance containing the event data.</param>
|
|
|
|
|
protected virtual void OnMessageReceived(UdpMessageReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
EventHandler<UdpMessageReceivedEventArgs> handler = MessageReceived;
|
|
|
|
|
if (handler != null) handler(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
|
|
|
|
CreateObservable().Subscribe(OnMessageReceived);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the observable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IObservable{UdpReceiveResult}.</returns>
|
|
|
|
|
private IObservable<UdpReceiveResult> CreateObservable()
|
|
|
|
|
{
|
|
|
|
|
return Observable.Create<UdpReceiveResult>(obs =>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
Observable.FromAsync(() =>
|
|
|
|
|
{
|
|
|
|
|
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)));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
.Subscribe(obs))
|
2013-02-23 22:31:51 +00:00
|
|
|
|
.Repeat()
|
|
|
|
|
.Retry()
|
|
|
|
|
.Publish()
|
|
|
|
|
.RefCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [message received].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void OnMessageReceived(UdpReceiveResult message)
|
|
|
|
|
{
|
2013-02-26 16:10:55 +00:00
|
|
|
|
if (message.RemoteEndPoint.Port == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-02-23 22:31:51 +00: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()
|
|
|
|
|
{
|
|
|
|
|
_udpClient.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
public Task SendAsync(byte[] bytes, string remoteEndPoint)
|
|
|
|
|
{
|
|
|
|
|
if (bytes == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("bytes");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(remoteEndPoint))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("remoteEndPoint");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _udpClient.SendAsync(bytes, bytes.Length, new NetworkManager().Parse(remoteEndPoint));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|