SsdpCommunicationsServer.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Net;
  9. namespace Rssdp.Infrastructure
  10. {
  11. /// <summary>
  12. /// Provides the platform independent logic for publishing device existence and responding to search requests.
  13. /// </summary>
  14. public sealed class SsdpCommunicationsServer : DisposableManagedObjectBase, ISsdpCommunicationsServer
  15. {
  16. #region Fields
  17. /*
  18. We could technically use one socket listening on port 1900 for everything.
  19. This should get both multicast (notifications) and unicast (search response) messages, however
  20. this often doesn't work under Windows because the MS SSDP service is running. If that service
  21. is running then it will steal the unicast messages and we will never see search responses.
  22. Since stopping the service would be a bad idea (might not be allowed security wise and might
  23. break other apps running on the system) the only other work around is to use two sockets.
  24. We use one socket to listen for/receive notifications and search requests (_BroadcastListenSocket).
  25. We use a second socket, bound to a different local port, to send search requests and listen for
  26. responses (_SendSocket). The responses are sent to the local port this socket is bound to,
  27. which isn't port 1900 so the MS service doesn't steal them. While the caller can specify a local
  28. port to use, we will default to 0 which allows the underlying system to auto-assign a free port.
  29. */
  30. private object _BroadcastListenSocketSynchroniser = new object();
  31. private IUdpSocket _BroadcastListenSocket;
  32. private object _SendSocketSynchroniser = new object();
  33. private IUdpSocket _SendSocket;
  34. private HttpRequestParser _RequestParser;
  35. private HttpResponseParser _ResponseParser;
  36. private ISocketFactory _SocketFactory;
  37. private int _LocalPort;
  38. private int _MulticastTtl;
  39. private bool _IsShared;
  40. #endregion
  41. #region Events
  42. /// <summary>
  43. /// Raised when a HTTPU request message is received by a socket (unicast or multicast).
  44. /// </summary>
  45. public event EventHandler<RequestReceivedEventArgs> RequestReceived;
  46. /// <summary>
  47. /// Raised when an HTTPU response message is received by a socket (unicast or multicast).
  48. /// </summary>
  49. public event EventHandler<ResponseReceivedEventArgs> ResponseReceived;
  50. #endregion
  51. #region Constructors
  52. /// <summary>
  53. /// Minimum constructor.
  54. /// </summary>
  55. /// <param name="socketFactory">An implementation of the <see cref="ISocketFactory"/> interface that can be used to make new unicast and multicast sockets. Cannot be null.</param>
  56. /// <exception cref="System.ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
  57. public SsdpCommunicationsServer(ISocketFactory socketFactory)
  58. : this(socketFactory, 0, SsdpConstants.SsdpDefaultMulticastTimeToLive)
  59. {
  60. }
  61. /// <summary>
  62. /// Partial constructor.
  63. /// </summary>
  64. /// <param name="socketFactory">An implementation of the <see cref="ISocketFactory"/> interface that can be used to make new unicast and multicast sockets. Cannot be null.</param>
  65. /// <param name="localPort">The specific local port to use for all sockets created by this instance. Specify zero to indicate the system should choose a free port itself.</param>
  66. /// <exception cref="System.ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
  67. public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort)
  68. : this(socketFactory, localPort, SsdpConstants.SsdpDefaultMulticastTimeToLive)
  69. {
  70. }
  71. /// <summary>
  72. /// Full constructor.
  73. /// </summary>
  74. /// <param name="socketFactory">An implementation of the <see cref="ISocketFactory"/> interface that can be used to make new unicast and multicast sockets. Cannot be null.</param>
  75. /// <param name="localPort">The specific local port to use for all sockets created by this instance. Specify zero to indicate the system should choose a free port itself.</param>
  76. /// <param name="multicastTimeToLive">The multicast time to live value for multicast sockets. Technically this is a number of router hops, not a 'Time'. Must be greater than zero.</param>
  77. /// <exception cref="System.ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
  78. /// <exception cref="System.ArgumentOutOfRangeException">The <paramref name="multicastTimeToLive"/> argument is less than or equal to zero.</exception>
  79. public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort, int multicastTimeToLive)
  80. {
  81. if (socketFactory == null) throw new ArgumentNullException("socketFactory");
  82. if (multicastTimeToLive <= 0) throw new ArgumentOutOfRangeException("multicastTimeToLive", "multicastTimeToLive must be greater than zero.");
  83. _BroadcastListenSocketSynchroniser = new object();
  84. _SendSocketSynchroniser = new object();
  85. _LocalPort = localPort;
  86. _SocketFactory = socketFactory;
  87. _RequestParser = new HttpRequestParser();
  88. _ResponseParser = new HttpResponseParser();
  89. _MulticastTtl = multicastTimeToLive;
  90. }
  91. #endregion
  92. #region Public Methods
  93. /// <summary>
  94. /// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications.
  95. /// </summary>
  96. /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  97. public void BeginListeningForBroadcasts()
  98. {
  99. ThrowIfDisposed();
  100. if (_BroadcastListenSocket == null)
  101. {
  102. lock (_BroadcastListenSocketSynchroniser)
  103. {
  104. if (_BroadcastListenSocket == null)
  105. _BroadcastListenSocket = ListenForBroadcastsAsync();
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications.
  111. /// </summary>
  112. /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  113. public void StopListeningForBroadcasts()
  114. {
  115. ThrowIfDisposed();
  116. lock (_BroadcastListenSocketSynchroniser)
  117. {
  118. if (_BroadcastListenSocket != null)
  119. {
  120. _BroadcastListenSocket.Dispose();
  121. _BroadcastListenSocket = null;
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Sends a message to a particular address (uni or multicast) and port.
  127. /// </summary>
  128. /// <param name="messageData">A byte array containing the data to send.</param>
  129. /// <param name="destination">A <see cref="IpEndPointInfo"/> representing the destination address for the data. Can be either a multicast or unicast destination.</param>
  130. /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="messageData"/> argument is null.</exception>
  131. /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  132. public async Task SendMessage(byte[] messageData, IpEndPointInfo destination)
  133. {
  134. if (messageData == null) throw new ArgumentNullException("messageData");
  135. ThrowIfDisposed();
  136. EnsureSendSocketCreated();
  137. // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP.
  138. await Repeat(SsdpConstants.UdpResendCount, TimeSpan.FromMilliseconds(100), () => SendMessageIfSocketNotDisposed(messageData, destination)).ConfigureAwait(false);
  139. }
  140. /// <summary>
  141. /// Sends a message to the SSDP multicast address and port.
  142. /// </summary>
  143. /// <param name="messageData">A byte array containing the data to send.</param>
  144. /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="messageData"/> argument is null.</exception>
  145. /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  146. public async Task SendMulticastMessage(byte[] messageData)
  147. {
  148. if (messageData == null) throw new ArgumentNullException("messageData");
  149. ThrowIfDisposed();
  150. EnsureSendSocketCreated();
  151. // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP.
  152. await Repeat(SsdpConstants.UdpResendCount, TimeSpan.FromMilliseconds(100),
  153. () => SendMessageIfSocketNotDisposed(messageData, new IpEndPointInfo() { IpAddress = new IpAddressInfo { Address = SsdpConstants.MulticastLocalAdminAddress }, Port = SsdpConstants.MulticastPort })).ConfigureAwait(false);
  154. }
  155. /// <summary>
  156. /// Stops listening for search responses on the local, unicast socket.
  157. /// </summary>
  158. /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  159. public void StopListeningForResponses()
  160. {
  161. ThrowIfDisposed();
  162. lock (_SendSocketSynchroniser)
  163. {
  164. var socket = _SendSocket;
  165. _SendSocket = null;
  166. if (socket != null)
  167. socket.Dispose();
  168. }
  169. }
  170. #endregion
  171. #region Public Properties
  172. /// <summary>
  173. /// Gets or sets a boolean value indicating whether or not this instance is shared amongst multiple <see cref="SsdpDeviceLocatorBase"/> and/or <see cref="ISsdpDevicePublisher"/> instances.
  174. /// </summary>
  175. /// <remarks>
  176. /// <para>If true, disposing an instance of a <see cref="SsdpDeviceLocatorBase"/>or a <see cref="ISsdpDevicePublisher"/> will not dispose this comms server instance. The calling code is responsible for managing the lifetime of the server.</para>
  177. /// </remarks>
  178. public bool IsShared
  179. {
  180. get { return _IsShared; }
  181. set { _IsShared = value; }
  182. }
  183. #endregion
  184. #region Overrides
  185. /// <summary>
  186. /// Stops listening for requests, disposes this instance and all internal resources.
  187. /// </summary>
  188. /// <param name="disposing"></param>
  189. protected override void Dispose(bool disposing)
  190. {
  191. if (disposing)
  192. {
  193. lock (_BroadcastListenSocketSynchroniser)
  194. {
  195. if (_BroadcastListenSocket != null)
  196. _BroadcastListenSocket.Dispose();
  197. }
  198. lock (_SendSocketSynchroniser)
  199. {
  200. if (_SendSocket != null)
  201. _SendSocket.Dispose();
  202. }
  203. }
  204. }
  205. #endregion
  206. #region Private Methods
  207. private async Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination)
  208. {
  209. var socket = _SendSocket;
  210. if (socket != null)
  211. {
  212. await _SendSocket.SendTo(messageData, destination).ConfigureAwait(false);
  213. }
  214. else
  215. {
  216. ThrowIfDisposed();
  217. }
  218. }
  219. private static async Task Repeat(int repetitions, TimeSpan delay, Func<Task> work)
  220. {
  221. for (int cnt = 0; cnt < repetitions; cnt++)
  222. {
  223. await work().ConfigureAwait(false);
  224. if (delay != TimeSpan.Zero)
  225. await Task.Delay(delay).ConfigureAwait(false);
  226. }
  227. }
  228. private IUdpSocket ListenForBroadcastsAsync()
  229. {
  230. var socket = _SocketFactory.CreateUdpMulticastSocket(SsdpConstants.MulticastLocalAdminAddress, _MulticastTtl, SsdpConstants.MulticastPort);
  231. ListenToSocket(socket);
  232. return socket;
  233. }
  234. private IUdpSocket CreateSocketAndListenForResponsesAsync()
  235. {
  236. _SendSocket = _SocketFactory.CreateUdpSocket(_LocalPort);
  237. ListenToSocket(_SendSocket);
  238. return _SendSocket;
  239. }
  240. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capturing task to local variable removes compiler warning, task is not otherwise required.")]
  241. private void ListenToSocket(IUdpSocket socket)
  242. {
  243. // Tasks are captured to local variables even if we don't use them just to avoid compiler warnings.
  244. var t = Task.Run(async () =>
  245. {
  246. var cancelled = false;
  247. while (!cancelled)
  248. {
  249. try
  250. {
  251. var result = await socket.ReceiveAsync();
  252. if (result.ReceivedBytes > 0)
  253. {
  254. // Strange cannot convert compiler error here if I don't explicitly
  255. // assign or cast to Action first. Assignment is easier to read,
  256. // so went with that.
  257. Action processWork = () => ProcessMessage(System.Text.UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.ReceivedFrom);
  258. var processTask = Task.Run(processWork);
  259. }
  260. }
  261. catch (ObjectDisposedException)
  262. {
  263. cancelled = true;
  264. }
  265. catch (TaskCanceledException)
  266. {
  267. cancelled = true;
  268. }
  269. }
  270. });
  271. }
  272. private void EnsureSendSocketCreated()
  273. {
  274. if (_SendSocket == null)
  275. {
  276. lock (_SendSocketSynchroniser)
  277. {
  278. if (_SendSocket == null)
  279. _SendSocket = CreateSocketAndListenForResponsesAsync();
  280. }
  281. }
  282. }
  283. private void ProcessMessage(string data, IpEndPointInfo endPoint)
  284. {
  285. //Responses start with the HTTP version, prefixed with HTTP/ while
  286. //requests start with a method which can vary and might be one we haven't
  287. //seen/don't know. We'll check if this message is a request or a response
  288. //by checking for the static HTTP/ prefix on the start of the message.
  289. if (data.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase))
  290. {
  291. HttpResponseMessage responseMessage = null;
  292. try
  293. {
  294. responseMessage = _ResponseParser.Parse(data);
  295. }
  296. catch (ArgumentException) { } // Ignore invalid packets.
  297. if (responseMessage != null)
  298. OnResponseReceived(responseMessage, endPoint);
  299. }
  300. else
  301. {
  302. HttpRequestMessage requestMessage = null;
  303. try
  304. {
  305. requestMessage = _RequestParser.Parse(data);
  306. }
  307. catch (ArgumentException) { } // Ignore invalid packets.
  308. if (requestMessage != null)
  309. OnRequestReceived(requestMessage, endPoint);
  310. }
  311. }
  312. private void OnRequestReceived(HttpRequestMessage data, IpEndPointInfo endPoint)
  313. {
  314. //SSDP specification says only * is currently used but other uri's might
  315. //be implemented in the future and should be ignored unless understood.
  316. //Section 4.2 - http://tools.ietf.org/html/draft-cai-ssdp-v1-03#page-11
  317. if (data.RequestUri.ToString() != "*") return;
  318. var handlers = this.RequestReceived;
  319. if (handlers != null)
  320. handlers(this, new RequestReceivedEventArgs(data, endPoint));
  321. }
  322. private void OnResponseReceived(HttpResponseMessage data, IpEndPointInfo endPoint)
  323. {
  324. var handlers = this.ResponseReceived;
  325. if (handlers != null)
  326. handlers(this, new ResponseReceivedEventArgs(data, endPoint));
  327. }
  328. #endregion
  329. }
  330. }