SocketFactory.cs 5.0 KB

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