jellyfin/MediaBrowser.Dlna/Ssdp/Datagram.cs

127 lines
3.9 KiB
C#
Raw Normal View History

2014-04-10 17:06:54 +02:00
using MediaBrowser.Model.Logging;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
2014-04-25 19:30:41 +02:00
namespace MediaBrowser.Dlna.Ssdp
2014-04-10 17:06:54 +02:00
{
public class Datagram
{
public EndPoint ToEndPoint { get; private set; }
public EndPoint FromEndPoint { get; private set; }
2014-04-10 17:06:54 +02:00
public string Message { get; private set; }
2015-10-18 03:18:29 +02:00
public bool IsBroadcast { get; private set; }
2015-05-04 19:44:25 +02:00
public bool EnableDebugLogging { get; private set; }
2014-04-25 19:30:41 +02:00
2014-04-10 17:06:54 +02:00
private readonly ILogger _logger;
2015-10-18 03:18:29 +02:00
public Datagram(EndPoint toEndPoint, EndPoint fromEndPoint, ILogger logger, string message, bool isBroadcast, bool enableDebugLogging)
2014-04-10 17:06:54 +02:00
{
Message = message;
_logger = logger;
2015-05-04 19:44:25 +02:00
EnableDebugLogging = enableDebugLogging;
2015-10-18 03:18:29 +02:00
IsBroadcast = isBroadcast;
2014-05-01 05:24:55 +02:00
FromEndPoint = fromEndPoint;
ToEndPoint = toEndPoint;
2014-04-10 17:06:54 +02:00
}
public void Send()
{
var msg = Encoding.ASCII.GetBytes(Message);
2015-05-08 00:27:01 +02:00
2015-09-23 18:17:03 +02:00
var socket = CreateSocket();
2015-05-08 00:27:01 +02:00
if (socket == null)
2014-04-10 17:06:54 +02:00
{
2015-05-08 00:27:01 +02:00
return;
}
2014-04-25 19:30:41 +02:00
2015-05-08 00:27:01 +02:00
if (FromEndPoint != null)
{
try
2014-05-01 05:24:55 +02:00
{
2015-05-08 00:27:01 +02:00
socket.Bind(FromEndPoint);
}
catch (Exception ex)
{
if (EnableDebugLogging)
2014-10-08 04:25:24 +02:00
{
2015-05-08 00:27:01 +02:00
_logger.ErrorException("Error binding datagram socket", ex);
2014-10-08 04:25:24 +02:00
}
2015-05-08 00:27:01 +02:00
2015-10-18 03:18:29 +02:00
if (IsBroadcast)
2014-10-08 04:25:24 +02:00
{
2015-05-08 00:27:01 +02:00
CloseSocket(socket, false);
return;
2014-10-08 04:25:24 +02:00
}
2014-05-01 05:24:55 +02:00
}
2015-05-08 00:27:01 +02:00
}
2014-04-25 19:30:41 +02:00
2015-05-08 00:27:01 +02:00
try
{
socket.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, ToEndPoint, result =>
2014-04-10 17:06:54 +02:00
{
try
{
2015-05-08 00:27:01 +02:00
socket.EndSend(result);
2014-04-10 17:06:54 +02:00
}
catch (Exception ex)
{
2015-09-23 18:17:03 +02:00
if (EnableDebugLogging)
2014-10-08 04:25:24 +02:00
{
_logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString());
}
2014-04-10 17:06:54 +02:00
}
finally
{
2015-05-08 00:27:01 +02:00
CloseSocket(socket, true);
2014-04-10 17:06:54 +02:00
}
}, null);
}
catch (Exception ex)
{
2014-08-26 04:30:52 +02:00
_logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString());
2015-05-08 00:27:01 +02:00
CloseSocket(socket, false);
}
}
private void CloseSocket(Socket socket, bool logError)
{
try
{
socket.Close();
}
catch (Exception ex)
{
if (logError && EnableDebugLogging)
{
_logger.ErrorException("Error closing datagram socket", ex);
}
2014-04-10 17:06:54 +02:00
}
}
2014-04-25 19:30:41 +02:00
2015-09-23 18:17:03 +02:00
private Socket CreateSocket()
2014-04-25 19:30:41 +02:00
{
2015-05-08 00:27:01 +02:00
try
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
2015-09-23 18:17:03 +02:00
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
2015-05-12 04:40:26 +02:00
2015-10-18 03:18:29 +02:00
if (IsBroadcast)
{
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
}
2015-05-08 00:27:01 +02:00
return socket;
}
catch (Exception ex)
{
_logger.ErrorException("Error creating socket", ex);
return null;
}
2014-04-25 19:30:41 +02:00
}
2014-04-10 17:06:54 +02:00
}
}