aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Dlna/Ssdp/Datagram.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Dlna/Ssdp/Datagram.cs')
-rw-r--r--MediaBrowser.Dlna/Ssdp/Datagram.cs82
1 files changed, 82 insertions, 0 deletions
diff --git a/MediaBrowser.Dlna/Ssdp/Datagram.cs b/MediaBrowser.Dlna/Ssdp/Datagram.cs
new file mode 100644
index 000000000..0caf5c78f
--- /dev/null
+++ b/MediaBrowser.Dlna/Ssdp/Datagram.cs
@@ -0,0 +1,82 @@
+using MediaBrowser.Model.Logging;
+using System;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+
+namespace MediaBrowser.Dlna.Ssdp
+{
+ public class Datagram
+ {
+ public IPEndPoint EndPoint { get; private set; }
+ public IPAddress LocalAddress { get; private set; }
+ public string Message { get; private set; }
+
+ /// <summary>
+ /// The number of times to send the message
+ /// </summary>
+ public int TotalSendCount { get; private set; }
+
+ /// <summary>
+ /// The number of times the message has been sent
+ /// </summary>
+ public int SendCount { get; private set; }
+
+ private readonly ILogger _logger;
+
+ public Datagram(IPEndPoint endPoint, IPAddress localAddress, ILogger logger, string message, int totalSendCount)
+ {
+ Message = message;
+ _logger = logger;
+ TotalSendCount = totalSendCount;
+ LocalAddress = localAddress;
+ EndPoint = endPoint;
+ }
+
+ public void Send()
+ {
+ var msg = Encoding.ASCII.GetBytes(Message);
+ try
+ {
+ var client = CreateSocket();
+
+ client.Bind(new IPEndPoint(LocalAddress, 0));
+
+ client.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, EndPoint, result =>
+ {
+ try
+ {
+ client.EndSend(result);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error sending Datagram", ex);
+ }
+ finally
+ {
+ try
+ {
+ client.Close();
+ }
+ catch (Exception)
+ {
+ }
+ }
+ }, null);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error sending Datagram", ex);
+ }
+ ++SendCount;
+ }
+
+ private Socket CreateSocket()
+ {
+ var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ return socket;
+ }
+ }
+}