2
0

SocketFactory.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.IO;
  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. /// <summary>
  11. /// Creates a new UDP acceptSocket and binds it to the specified local port.
  12. /// </summary>
  13. /// <param name="localPort">An integer specifying the local port to bind the acceptSocket to.</param>
  14. public ISocket CreateUdpSocket(int localPort)
  15. {
  16. if (localPort < 0)
  17. {
  18. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  19. }
  20. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  21. try
  22. {
  23. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  24. return new UdpSocket(retVal, localPort, IPAddress.Any);
  25. }
  26. catch
  27. {
  28. retVal?.Dispose();
  29. throw;
  30. }
  31. }
  32. public ISocket CreateUdpBroadcastSocket(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.Socket, SocketOptionName.Broadcast, 1);
  43. return new UdpSocket(retVal, localPort, IPAddress.Any);
  44. }
  45. catch
  46. {
  47. retVal?.Dispose();
  48. throw;
  49. }
  50. }
  51. /// <summary>
  52. /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
  53. /// </summary>
  54. /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
  55. public ISocket CreateSsdpUdpSocket(IPAddress localIpAddress, int localPort)
  56. {
  57. if (localPort < 0)
  58. {
  59. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  60. }
  61. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  62. try
  63. {
  64. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  65. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  66. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIpAddress));
  67. return new UdpSocket(retVal, localPort, localIpAddress);
  68. }
  69. catch
  70. {
  71. retVal?.Dispose();
  72. throw;
  73. }
  74. }
  75. /// <summary>
  76. /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
  77. /// </summary>
  78. /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
  79. /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
  80. /// <param name="localPort">The number of the local port to bind to.</param>
  81. /// <returns></returns>
  82. public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  83. {
  84. if (ipAddress == null)
  85. {
  86. throw new ArgumentNullException(nameof(ipAddress));
  87. }
  88. if (ipAddress.Length == 0)
  89. {
  90. throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
  91. }
  92. if (multicastTimeToLive <= 0)
  93. {
  94. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  95. }
  96. if (localPort < 0)
  97. {
  98. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  99. }
  100. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  101. try
  102. {
  103. // not supported on all platforms. throws on ubuntu with .net core 2.0
  104. retVal.ExclusiveAddressUse = false;
  105. }
  106. catch (SocketException)
  107. {
  108. }
  109. try
  110. {
  111. // seeing occasional exceptions thrown on qnap
  112. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  113. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  114. }
  115. catch (SocketException)
  116. {
  117. }
  118. try
  119. {
  120. //retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  121. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  122. var localIp = IPAddress.Any;
  123. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
  124. retVal.MulticastLoopback = true;
  125. return new UdpSocket(retVal, localPort, localIp);
  126. }
  127. catch
  128. {
  129. retVal?.Dispose();
  130. throw;
  131. }
  132. }
  133. public Stream CreateNetworkStream(ISocket socket, bool ownsSocket)
  134. => new NetworkStream(((UdpSocket)socket).Socket, ownsSocket);
  135. }
  136. }