SocketFactory.cs 5.0 KB

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