SocketFactory.cs 6.7 KB

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