SocketFactory.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Socket CreateUdpBroadcastSocket(int localPort)
  12. {
  13. if (localPort < 0)
  14. {
  15. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  16. }
  17. var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  18. try
  19. {
  20. socket.EnableBroadcast = true;
  21. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  22. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  23. socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
  24. return socket;
  25. }
  26. catch
  27. {
  28. socket?.Dispose();
  29. throw;
  30. }
  31. }
  32. /// <inheritdoc />
  33. public Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort)
  34. {
  35. ArgumentNullException.ThrowIfNull(bindInterface.Address);
  36. if (localPort < 0)
  37. {
  38. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  39. }
  40. var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  41. try
  42. {
  43. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  44. socket.Bind(new IPEndPoint(bindInterface.Address, localPort));
  45. return socket;
  46. }
  47. catch
  48. {
  49. socket?.Dispose();
  50. throw;
  51. }
  52. }
  53. /// <inheritdoc />
  54. public Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort)
  55. {
  56. var bindIPAddress = bindInterface.Address;
  57. ArgumentNullException.ThrowIfNull(multicastAddress);
  58. ArgumentNullException.ThrowIfNull(bindIPAddress);
  59. if (multicastTimeToLive <= 0)
  60. {
  61. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  62. }
  63. if (localPort < 0)
  64. {
  65. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  66. }
  67. var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  68. try
  69. {
  70. var interfaceIndex = bindInterface.Index;
  71. var interfaceIndexSwapped = (int)IPAddress.HostToNetworkOrder(interfaceIndex);
  72. socket.MulticastLoopback = false;
  73. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  74. socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
  75. socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  76. socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, interfaceIndexSwapped);
  77. socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
  78. socket.Bind(new IPEndPoint(multicastAddress, localPort));
  79. return socket;
  80. }
  81. catch
  82. {
  83. socket?.Dispose();
  84. throw;
  85. }
  86. }
  87. }
  88. }