SocketFactory.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Net;
  8. namespace Emby.Common.Implementations.Net
  9. {
  10. public class SocketFactory : ISocketFactory
  11. {
  12. // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS
  13. // Be careful to check any changes compile and work for all platform projects it is shared in.
  14. // Not entirely happy with this. Would have liked to have done something more generic/reusable,
  15. // but that wasn't really the point so kept to YAGNI principal for now, even if the
  16. // interfaces are a bit ugly, specific and make assumptions.
  17. /// <summary>
  18. /// Used by RSSDP components to create implementations of the <see cref="IUdpSocket"/> interface, to perform platform agnostic socket communications.
  19. /// </summary>
  20. private IPAddress _LocalIP;
  21. /// <summary>
  22. /// Default constructor.
  23. /// </summary>
  24. /// <param name="localIP">A string containing the IP address of the local network adapter to bind sockets to. Null or empty string will use <see cref="IPAddress.Any"/>.</param>
  25. public SocketFactory(string localIP)
  26. {
  27. if (String.IsNullOrEmpty(localIP))
  28. _LocalIP = IPAddress.Any;
  29. else
  30. _LocalIP = IPAddress.Parse(localIP);
  31. }
  32. #region ISocketFactory Members
  33. /// <summary>
  34. /// Creates a new UDP socket and binds it to the specified local port.
  35. /// </summary>
  36. /// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
  37. public IUdpSocket CreateUdpSocket(int localPort)
  38. {
  39. if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
  40. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  41. try
  42. {
  43. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  44. return new UdpSocket(retVal, localPort, _LocalIP);
  45. }
  46. catch
  47. {
  48. if (retVal != null)
  49. retVal.Dispose();
  50. throw;
  51. }
  52. }
  53. /// <summary>
  54. /// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
  55. /// </summary>
  56. /// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
  57. /// <returns>An implementation of the <see cref="IUdpSocket"/> interface used by RSSDP components to perform socket operations.</returns>
  58. public IUdpSocket CreateSsdpUdpSocket(int localPort)
  59. {
  60. if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
  61. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 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"), _LocalIP));
  67. return new UdpSocket(retVal, localPort, _LocalIP);
  68. }
  69. catch
  70. {
  71. if (retVal != null)
  72. retVal.Dispose();
  73. throw;
  74. }
  75. }
  76. /// <summary>
  77. /// Creates a new UDP socket that is a member of the specified multicast IP address, and binds it to the specified local port.
  78. /// </summary>
  79. /// <param name="ipAddress">The multicast IP address to make the socket a member of.</param>
  80. /// <param name="multicastTimeToLive">The multicast time to live value for the socket.</param>
  81. /// <param name="localPort">The number of the local port to bind to.</param>
  82. /// <returns></returns>
  83. public IUdpSocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  84. {
  85. if (ipAddress == null) throw new ArgumentNullException("ipAddress");
  86. if (ipAddress.Length == 0) throw new ArgumentException("ipAddress cannot be an empty string.", "ipAddress");
  87. if (multicastTimeToLive <= 0) throw new ArgumentException("multicastTimeToLive cannot be zero or less.", "multicastTimeToLive");
  88. if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
  89. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  90. try
  91. {
  92. #if NETSTANDARD1_3
  93. // The ExclusiveAddressUse socket option is a Windows-specific option that, when set to "true," tells Windows not to allow another socket to use the same local address as this socket
  94. // See https://github.com/dotnet/corefx/pull/11509 for more details
  95. if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
  96. {
  97. retVal.ExclusiveAddressUse = false;
  98. }
  99. #else
  100. retVal.ExclusiveAddressUse = false;
  101. #endif
  102. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  103. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  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. if (retVal != null)
  111. retVal.Dispose();
  112. throw;
  113. }
  114. }
  115. #endregion
  116. }
  117. }