SocketFactory.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 readonly ILogger _logger;
  23. public SocketFactory(ILogger logger)
  24. {
  25. if (logger == null)
  26. {
  27. throw new ArgumentNullException("logger");
  28. }
  29. _logger = logger;
  30. _LocalIP = IPAddress.Any;
  31. }
  32. public ISocket CreateSocket(IpAddressFamily family, MediaBrowser.Model.Net.SocketType socketType, MediaBrowser.Model.Net.ProtocolType protocolType, bool dualMode)
  33. {
  34. var addressFamily = family == IpAddressFamily.InterNetwork
  35. ? AddressFamily.InterNetwork
  36. : AddressFamily.InterNetworkV6;
  37. var socket = new Socket(addressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
  38. if (dualMode)
  39. {
  40. socket.DualMode = true;
  41. }
  42. return new NetSocket(socket, _logger);
  43. }
  44. #region ISocketFactory Members
  45. /// <summary>
  46. /// Creates a new UDP socket and binds it to the specified local port.
  47. /// </summary>
  48. /// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
  49. public IUdpSocket CreateUdpSocket(int localPort)
  50. {
  51. if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
  52. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  53. try
  54. {
  55. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  56. return new UdpSocket(retVal, localPort, _LocalIP);
  57. }
  58. catch
  59. {
  60. if (retVal != null)
  61. retVal.Dispose();
  62. throw;
  63. }
  64. }
  65. /// <summary>
  66. /// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
  67. /// </summary>
  68. /// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
  69. /// <returns>An implementation of the <see cref="IUdpSocket"/> interface used by RSSDP components to perform socket operations.</returns>
  70. public IUdpSocket CreateSsdpUdpSocket(int localPort)
  71. {
  72. if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
  73. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  74. try
  75. {
  76. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  77. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  78. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), _LocalIP));
  79. return new UdpSocket(retVal, localPort, _LocalIP);
  80. }
  81. catch
  82. {
  83. if (retVal != null)
  84. retVal.Dispose();
  85. throw;
  86. }
  87. }
  88. /// <summary>
  89. /// Creates a new UDP socket that is a member of the specified multicast IP address, and binds it to the specified local port.
  90. /// </summary>
  91. /// <param name="ipAddress">The multicast IP address to make the socket a member of.</param>
  92. /// <param name="multicastTimeToLive">The multicast time to live value for the socket.</param>
  93. /// <param name="localPort">The number of the local port to bind to.</param>
  94. /// <returns></returns>
  95. public IUdpSocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  96. {
  97. if (ipAddress == null) throw new ArgumentNullException("ipAddress");
  98. if (ipAddress.Length == 0) throw new ArgumentException("ipAddress cannot be an empty string.", "ipAddress");
  99. if (multicastTimeToLive <= 0) throw new ArgumentException("multicastTimeToLive cannot be zero or less.", "multicastTimeToLive");
  100. if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
  101. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  102. try
  103. {
  104. #if NETSTANDARD1_3
  105. // 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
  106. // See https://github.com/dotnet/corefx/pull/11509 for more details
  107. if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
  108. {
  109. retVal.ExclusiveAddressUse = false;
  110. }
  111. #else
  112. retVal.ExclusiveAddressUse = false;
  113. #endif
  114. //retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  115. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  116. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  117. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), _LocalIP));
  118. retVal.MulticastLoopback = true;
  119. return new UdpSocket(retVal, localPort, _LocalIP);
  120. }
  121. catch
  122. {
  123. if (retVal != null)
  124. retVal.Dispose();
  125. throw;
  126. }
  127. }
  128. #endregion
  129. }
  130. }