SocketFactory.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using MediaBrowser.Model.Net;
  6. using Microsoft.Extensions.Logging;
  7. namespace Emby.Server.Implementations.Net
  8. {
  9. public class SocketFactory : ISocketFactory
  10. {
  11. public ISocket CreateUdpBroadcastSocket(int localPort)
  12. {
  13. if (localPort < 0)
  14. {
  15. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  16. }
  17. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  18. try
  19. {
  20. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  21. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  22. return new UdpSocket(retVal, localPort, IPAddress.Any);
  23. }
  24. catch
  25. {
  26. retVal?.Dispose();
  27. throw;
  28. }
  29. }
  30. /// <summary>
  31. /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
  32. /// </summary>
  33. /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
  34. public ISocket CreateSsdpUdpSocket(IPAddress localIpAddress, int localPort)
  35. {
  36. if (localPort < 0)
  37. {
  38. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  39. }
  40. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  41. try
  42. {
  43. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  44. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  45. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIpAddress));
  46. return new UdpSocket(retVal, localPort, localIpAddress);
  47. }
  48. catch
  49. {
  50. retVal?.Dispose();
  51. throw;
  52. }
  53. }
  54. /// <summary>
  55. /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
  56. /// </summary>
  57. /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
  58. /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
  59. /// <param name="localPort">The number of the local port to bind to.</param>
  60. /// <returns></returns>
  61. public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort, ILogger _logger)
  62. {
  63. if (ipAddress == null)
  64. {
  65. throw new ArgumentNullException(nameof(ipAddress));
  66. }
  67. if (ipAddress.Length == 0)
  68. {
  69. throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
  70. }
  71. if (multicastTimeToLive <= 0)
  72. {
  73. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  74. }
  75. if (localPort < 0)
  76. {
  77. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  78. }
  79. _logger.LogError("Created");
  80. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  81. try
  82. {
  83. // not supported on all platforms. throws on ubuntu with .net core 2.0
  84. retVal.ExclusiveAddressUse = false;
  85. }
  86. catch (SocketException)
  87. {
  88. }
  89. _logger.LogError("Exclusive false");
  90. try
  91. {
  92. // seeing occasional exceptions thrown on qnap
  93. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  94. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  95. }
  96. catch (SocketException)
  97. {
  98. }
  99. _logger.LogError("Reused");
  100. try
  101. {
  102. retVal.EnableBroadcast = true; // CHANGE
  103. _logger.LogError("Broadcast");
  104. // retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  105. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  106. var localIp = IPAddress.Any;
  107. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
  108. retVal.MulticastLoopback = true;
  109. _logger.LogError("Sorted");
  110. return new UdpSocket(retVal, localPort, localIp);
  111. }
  112. catch
  113. {
  114. retVal?.Dispose();
  115. throw;
  116. }
  117. }
  118. }
  119. }