2014-04-10 15:06:54 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
namespace MediaBrowser.Dlna.Ssdp
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
|
|
|
|
public class Datagram
|
|
|
|
|
{
|
2014-09-06 04:21:23 +00:00
|
|
|
|
public EndPoint ToEndPoint { get; private set; }
|
|
|
|
|
public EndPoint FromEndPoint { get; private set; }
|
2014-04-10 15:06:54 +00:00
|
|
|
|
public string Message { get; private set; }
|
2014-10-08 02:25:24 +00:00
|
|
|
|
public bool IgnoreBindFailure { get; private set; }
|
2014-04-25 17:30:41 +00:00
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2015-01-31 23:12:22 +00:00
|
|
|
|
public Datagram(EndPoint toEndPoint, EndPoint fromEndPoint, ILogger logger, string message, bool ignoreBindFailure)
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
|
|
|
|
Message = message;
|
|
|
|
|
_logger = logger;
|
2014-10-08 02:25:24 +00:00
|
|
|
|
IgnoreBindFailure = ignoreBindFailure;
|
2014-05-01 03:24:55 +00:00
|
|
|
|
FromEndPoint = fromEndPoint;
|
|
|
|
|
ToEndPoint = toEndPoint;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send()
|
|
|
|
|
{
|
|
|
|
|
var msg = Encoding.ASCII.GetBytes(Message);
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
var client = CreateSocket();
|
|
|
|
|
|
2014-05-01 03:24:55 +00:00
|
|
|
|
if (FromEndPoint != null)
|
|
|
|
|
{
|
2014-10-08 02:25:24 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Bind(FromEndPoint);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (!IgnoreBindFailure) throw;
|
|
|
|
|
}
|
2014-05-01 03:24:55 +00:00
|
|
|
|
}
|
2014-04-25 17:30:41 +00:00
|
|
|
|
|
2014-05-01 03:24:55 +00:00
|
|
|
|
client.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, ToEndPoint, result =>
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.EndSend(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-10-08 02:25:24 +00:00
|
|
|
|
if (!IgnoreBindFailure)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString());
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-08-26 02:30:52 +00:00
|
|
|
|
_logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString());
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-25 17:30:41 +00:00
|
|
|
|
|
|
|
|
|
private Socket CreateSocket()
|
|
|
|
|
{
|
|
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
|
|
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|