SocketFactory.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using MediaBrowser.Model.Net;
  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.EnableBroadcast = true;
  21. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  22. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  23. return new UdpSocket(retVal, localPort, IPAddress.Any);
  24. }
  25. catch
  26. {
  27. retVal?.Dispose();
  28. throw;
  29. }
  30. }
  31. /// <summary>
  32. /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
  33. /// </summary>
  34. /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
  35. public ISocket CreateSsdpUdpSocket(IPAddress localIpAddress, int localPort)
  36. {
  37. if (localPort < 0)
  38. {
  39. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  40. }
  41. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  42. try
  43. {
  44. retVal.EnableBroadcast = true;
  45. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  46. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  47. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIpAddress));
  48. return new UdpSocket(retVal, localPort, localIpAddress);
  49. }
  50. catch
  51. {
  52. retVal?.Dispose();
  53. throw;
  54. }
  55. }
  56. /// <summary>
  57. /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
  58. /// </summary>
  59. /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
  60. /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
  61. /// <param name="localPort">The number of the local port to bind to.</param>
  62. /// <returns></returns>
  63. public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  64. {
  65. if (ipAddress == null)
  66. {
  67. throw new ArgumentNullException(nameof(ipAddress));
  68. }
  69. if (ipAddress.Length == 0)
  70. {
  71. throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
  72. }
  73. if (multicastTimeToLive <= 0)
  74. {
  75. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  76. }
  77. if (localPort < 0)
  78. {
  79. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  80. }
  81. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  82. try
  83. {
  84. // not supported on all platforms. throws on ubuntu with .net core 2.0
  85. retVal.ExclusiveAddressUse = false;
  86. }
  87. catch (SocketException)
  88. {
  89. }
  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. try
  100. {
  101. retVal.EnableBroadcast = true;
  102. // retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  103. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  104. var localIp = IPAddress.Any;
  105. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
  106. retVal.MulticastLoopback = true;
  107. return new UdpSocket(retVal, localPort, localIp);
  108. }
  109. catch
  110. {
  111. retVal?.Dispose();
  112. throw;
  113. }
  114. }
  115. }
  116. }