SocketFactory.cs 6.4 KB

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