Datagram.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. namespace MediaBrowser.Dlna.Ssdp
  7. {
  8. public class Datagram
  9. {
  10. public EndPoint ToEndPoint { get; private set; }
  11. public EndPoint FromEndPoint { get; private set; }
  12. public string Message { get; private set; }
  13. public bool IsBroadcast { get; private set; }
  14. public bool EnableDebugLogging { get; private set; }
  15. private readonly ILogger _logger;
  16. public Datagram(EndPoint toEndPoint, EndPoint fromEndPoint, ILogger logger, string message, bool isBroadcast, bool enableDebugLogging)
  17. {
  18. Message = message;
  19. _logger = logger;
  20. EnableDebugLogging = enableDebugLogging;
  21. IsBroadcast = isBroadcast;
  22. FromEndPoint = fromEndPoint;
  23. ToEndPoint = toEndPoint;
  24. }
  25. public void Send()
  26. {
  27. var msg = Encoding.ASCII.GetBytes(Message);
  28. var socket = CreateSocket();
  29. if (socket == null)
  30. {
  31. return;
  32. }
  33. if (FromEndPoint != null)
  34. {
  35. try
  36. {
  37. socket.Bind(FromEndPoint);
  38. }
  39. catch (Exception ex)
  40. {
  41. if (EnableDebugLogging)
  42. {
  43. _logger.ErrorException("Error binding datagram socket", ex);
  44. }
  45. if (IsBroadcast)
  46. {
  47. CloseSocket(socket, false);
  48. return;
  49. }
  50. }
  51. }
  52. try
  53. {
  54. socket.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, ToEndPoint, result =>
  55. {
  56. try
  57. {
  58. socket.EndSend(result);
  59. }
  60. catch (Exception ex)
  61. {
  62. if (EnableDebugLogging)
  63. {
  64. _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString());
  65. }
  66. }
  67. finally
  68. {
  69. CloseSocket(socket, true);
  70. }
  71. }, null);
  72. }
  73. catch (Exception ex)
  74. {
  75. _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString());
  76. CloseSocket(socket, false);
  77. }
  78. }
  79. private void CloseSocket(Socket socket, bool logError)
  80. {
  81. try
  82. {
  83. socket.Close();
  84. }
  85. catch (Exception ex)
  86. {
  87. if (logError && EnableDebugLogging)
  88. {
  89. _logger.ErrorException("Error closing datagram socket", ex);
  90. }
  91. }
  92. }
  93. private Socket CreateSocket()
  94. {
  95. try
  96. {
  97. var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  98. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  99. socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  100. if (IsBroadcast)
  101. {
  102. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  103. }
  104. return socket;
  105. }
  106. catch (Exception ex)
  107. {
  108. _logger.ErrorException("Error creating socket", ex);
  109. return null;
  110. }
  111. }
  112. }
  113. }