SsdpDevicePublisher.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Rssdp.Infrastructure;
  6. namespace Rssdp
  7. {
  8. /// <summary>
  9. /// Allows publishing devices both as notification and responses to search requests.
  10. /// </summary>
  11. /// <remarks>
  12. /// This is the 'server' part of the system. You add your devices to an instance of this class so clients can find them.
  13. /// </remarks>
  14. public class SsdpDevicePublisher : SsdpDevicePublisherBase
  15. {
  16. #region Constructors
  17. /// <summary>
  18. /// Default constructor.
  19. /// </summary>
  20. /// <remarks>
  21. /// <para>Uses the default <see cref="ISsdpCommunicationsServer"/> implementation and network settings for Windows and the SSDP specification.</para>
  22. /// </remarks>
  23. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")]
  24. public SsdpDevicePublisher()
  25. : this(new SsdpCommunicationsServer(new SocketFactory(null)))
  26. {
  27. }
  28. /// <summary>
  29. /// Full constructor.
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>Allows the caller to specify their own <see cref="ISsdpCommunicationsServer"/> implementation for full control over the networking, or for mocking/testing purposes..</para>
  33. /// </remarks>
  34. public SsdpDevicePublisher(ISsdpCommunicationsServer communicationsServer)
  35. : base(communicationsServer, GetOSName(), GetOSVersion())
  36. {
  37. }
  38. /// <summary>
  39. /// Partial constructor.
  40. /// </summary>
  41. /// <param name="localPort">The local port to use for socket communications, specify 0 to have the system choose it's own.</param>
  42. /// <remarks>
  43. /// <para>Uses the default <see cref="ISsdpCommunicationsServer"/> implementation and network settings for Windows and the SSDP specification, but specifies the local port to use for socket communications. Specify 0 to indicate the system should choose it's own port.</para>
  44. /// </remarks>
  45. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")]
  46. public SsdpDevicePublisher(int localPort)
  47. : this(new SsdpCommunicationsServer(new SocketFactory(null), localPort))
  48. {
  49. }
  50. /// <summary>
  51. /// Partial constructor.
  52. /// </summary>
  53. /// <param name="localPort">The local port to use for socket communications, specify 0 to have the system choose it's own.</param>
  54. /// <param name="multicastTimeToLive">The number of hops a multicast packet can make before it expires. Must be 1 or greater.</param>
  55. /// <remarks>
  56. /// <para>Uses the default <see cref="ISsdpCommunicationsServer"/> implementation and network settings for Windows and the SSDP specification, but specifies the local port to use and multicast time to live setting for socket communications.</para>
  57. /// <para>Specify 0 for the <paramref name="localPort"/> argument to indicate the system should choose it's own port.</para>
  58. /// <para>The <paramref name="multicastTimeToLive"/> is actually a number of 'hops' on the network and not a time based argument.</para>
  59. /// </remarks>
  60. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")]
  61. public SsdpDevicePublisher(int localPort, int multicastTimeToLive)
  62. : this(new SsdpCommunicationsServer(new SocketFactory(null), localPort, multicastTimeToLive))
  63. {
  64. }
  65. #endregion
  66. #region Private Methods
  67. private static string GetOSName()
  68. {
  69. #if NET46
  70. return Environment.OSVersion.Platform.ToString();
  71. #elif NETSTANDARD1_6
  72. return System.Runtime.InteropServices.RuntimeInformation.OSDescription;
  73. #endif
  74. return "Operating System";
  75. }
  76. private static string GetOSVersion()
  77. {
  78. #if NET46
  79. return Environment.OSVersion.Version.ToString() + " " + Environment.OSVersion.ServicePack.ToString();
  80. #elif NETSTANDARD1_6
  81. return System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
  82. #endif
  83. return "1.0";
  84. }
  85. #endregion
  86. }
  87. }