SocketFactory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using MediaBrowser.Model.Net;
  6. namespace Emby.Server.Implementations.Net
  7. {
  8. public class SocketFactory : ISocketFactory
  9. {
  10. public ISocket CreateUdpBroadcastSocket(int localPort)
  11. {
  12. if (localPort < 0)
  13. {
  14. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  15. }
  16. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  17. try
  18. {
  19. retVal.EnableBroadcast = true;
  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.EnableBroadcast = true;
  44. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  45. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  46. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIpAddress));
  47. return new UdpSocket(retVal, localPort, localIpAddress);
  48. }
  49. catch
  50. {
  51. retVal?.Dispose();
  52. throw;
  53. }
  54. }
  55. /// <summary>
  56. /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
  57. /// </summary>
  58. /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
  59. /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
  60. /// <param name="localPort">The number of the local port to bind to.</param>
  61. /// <returns></returns>
  62. public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  63. {
  64. if (ipAddress == null)
  65. {
  66. throw new ArgumentNullException(nameof(ipAddress));
  67. }
  68. if (ipAddress.Length == 0)
  69. {
  70. throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
  71. }
  72. if (multicastTimeToLive <= 0)
  73. {
  74. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  75. }
  76. if (localPort < 0)
  77. {
  78. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  79. }
  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. try
  90. {
  91. // seeing occasional exceptions thrown on qnap
  92. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  93. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  94. }
  95. catch (SocketException)
  96. {
  97. }
  98. try
  99. {
  100. retVal.EnableBroadcast = true;
  101. // retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  102. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  103. var localIp = IPAddress.Any;
  104. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
  105. retVal.MulticastLoopback = true;
  106. return new UdpSocket(retVal, localPort, localIp);
  107. }
  108. catch
  109. {
  110. retVal?.Dispose();
  111. throw;
  112. }
  113. }
  114. }
  115. }