SocketFactory.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma warning disable CS1591
  2. using System;
  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. /// <inheritdoc />
  11. public ISocket CreateUdpBroadcastSocket(int localPort)
  12. {
  13. if (localPort < 0)
  14. {
  15. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  16. }
  17. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  18. try
  19. {
  20. retVal.EnableBroadcast = true;
  21. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  22. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  23. return new UdpSocket(retVal, localPort, IPAddress.Any);
  24. }
  25. catch
  26. {
  27. retVal?.Dispose();
  28. throw;
  29. }
  30. }
  31. /// <inheritdoc />
  32. public ISocket CreateSsdpUdpSocket(IPAddress localIp, 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, SocketType.Dgram, ProtocolType.Udp);
  39. try
  40. {
  41. retVal.EnableBroadcast = true;
  42. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  43. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  44. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIp));
  45. return new UdpSocket(retVal, localPort, localIp);
  46. }
  47. catch
  48. {
  49. retVal?.Dispose();
  50. throw;
  51. }
  52. }
  53. /// <inheritdoc />
  54. public ISocket CreateUdpMulticastSocket(IPAddress ipAddress, IPAddress bindIpAddress, int multicastTimeToLive, int localPort)
  55. {
  56. if (ipAddress == null)
  57. {
  58. throw new ArgumentNullException(nameof(ipAddress));
  59. }
  60. if (bindIpAddress == null)
  61. {
  62. bindIpAddress = IPAddress.Any;
  63. }
  64. if (multicastTimeToLive <= 0)
  65. {
  66. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  67. }
  68. if (localPort < 0)
  69. {
  70. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  71. }
  72. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  73. retVal.ExclusiveAddressUse = false;
  74. try
  75. {
  76. // seeing occasional exceptions thrown on qnap
  77. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  78. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  79. }
  80. catch (SocketException)
  81. {
  82. }
  83. try
  84. {
  85. retVal.EnableBroadcast = true;
  86. // retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  87. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  88. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipAddress, bindIpAddress));
  89. retVal.MulticastLoopback = true;
  90. return new UdpSocket(retVal, localPort, bindIpAddress);
  91. }
  92. catch
  93. {
  94. retVal?.Dispose();
  95. throw;
  96. }
  97. }
  98. }
  99. }