SocketFactory.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ArgumentNullException.ThrowIfNull(ipAddress);
  57. ArgumentNullException.ThrowIfNull(bindIpAddress);
  58. if (multicastTimeToLive <= 0)
  59. {
  60. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  61. }
  62. if (localPort < 0)
  63. {
  64. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  65. }
  66. var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  67. retVal.ExclusiveAddressUse = false;
  68. try
  69. {
  70. // seeing occasional exceptions thrown on qnap
  71. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  72. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  73. }
  74. catch (SocketException)
  75. {
  76. }
  77. try
  78. {
  79. retVal.EnableBroadcast = true;
  80. // retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  81. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  82. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipAddress, bindIpAddress));
  83. retVal.MulticastLoopback = true;
  84. return new UdpSocket(retVal, localPort, bindIpAddress);
  85. }
  86. catch
  87. {
  88. retVal?.Dispose();
  89. throw;
  90. }
  91. }
  92. }
  93. }