HttpSessionController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Session;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Serialization;
  5. using MediaBrowser.Model.Session;
  6. using MediaBrowser.Model.System;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Server.Implementations.Session
  15. {
  16. public class HttpSessionController : ISessionController, IDisposable
  17. {
  18. private readonly IHttpClient _httpClient;
  19. private readonly IJsonSerializer _json;
  20. private readonly ISessionManager _sessionManager;
  21. public SessionInfo Session { get; private set; }
  22. private readonly string _postUrl;
  23. private Timer _pingTimer;
  24. public HttpSessionController(IHttpClient httpClient,
  25. IJsonSerializer json,
  26. SessionInfo session,
  27. string postUrl, ISessionManager sessionManager)
  28. {
  29. _httpClient = httpClient;
  30. _json = json;
  31. Session = session;
  32. _postUrl = postUrl;
  33. _sessionManager = sessionManager;
  34. _pingTimer = new Timer(PingTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  35. ResetPingTimer();
  36. }
  37. private string PostUrl
  38. {
  39. get
  40. {
  41. return string.Format("http://{0}{1}", Session.RemoteEndPoint, _postUrl);
  42. }
  43. }
  44. public bool IsSessionActive
  45. {
  46. get
  47. {
  48. return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 20;
  49. }
  50. }
  51. public bool SupportsMediaControl
  52. {
  53. get { return true; }
  54. }
  55. private async void PingTimerCallback(object state)
  56. {
  57. try
  58. {
  59. await SendMessage("Ping", CancellationToken.None).ConfigureAwait(false);
  60. }
  61. catch
  62. {
  63. ReportSessionEnded();
  64. }
  65. }
  66. private void ReportSessionEnded()
  67. {
  68. try
  69. {
  70. _sessionManager.ReportSessionEnded(Session.Id);
  71. }
  72. catch (Exception ex)
  73. {
  74. }
  75. }
  76. private void ResetPingTimer()
  77. {
  78. if (_pingTimer != null)
  79. {
  80. var period = TimeSpan.FromSeconds(60);
  81. _pingTimer.Change(period, period);
  82. }
  83. }
  84. private Task SendMessage(string name, CancellationToken cancellationToken)
  85. {
  86. return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
  87. }
  88. private async Task SendMessage(string name,
  89. Dictionary<string, string> args,
  90. CancellationToken cancellationToken)
  91. {
  92. var url = PostUrl + "/" + name + ToQueryString(args);
  93. await _httpClient.Post(new HttpRequestOptions
  94. {
  95. Url = url,
  96. CancellationToken = cancellationToken
  97. }).ConfigureAwait(false);
  98. ResetPingTimer();
  99. }
  100. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  101. {
  102. return Task.FromResult(true);
  103. }
  104. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  105. {
  106. return Task.FromResult(true);
  107. }
  108. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  109. {
  110. return Task.FromResult(true);
  111. }
  112. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  113. {
  114. var dict = new Dictionary<string, string>();
  115. dict["ItemIds"] = string.Join(",", command.ItemIds);
  116. if (command.StartPositionTicks.HasValue)
  117. {
  118. dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  119. }
  120. return SendMessage(command.PlayCommand.ToString(), dict, cancellationToken);
  121. }
  122. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  123. {
  124. var args = new Dictionary<string, string>();
  125. if (command.Command == PlaystateCommand.Seek)
  126. {
  127. if (!command.SeekPositionTicks.HasValue)
  128. {
  129. throw new ArgumentException("SeekPositionTicks cannot be null");
  130. }
  131. args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  132. }
  133. return SendMessage(command.Command.ToString(), args, cancellationToken);
  134. }
  135. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  136. {
  137. return Task.FromResult(true);
  138. }
  139. public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
  140. {
  141. return SendMessage("RestartRequired", cancellationToken);
  142. }
  143. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  144. {
  145. return Task.FromResult(true);
  146. }
  147. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  148. {
  149. return SendMessage("ServerShuttingDown", cancellationToken);
  150. }
  151. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  152. {
  153. return SendMessage("ServerRestarting", cancellationToken);
  154. }
  155. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  156. {
  157. return SendMessage(command.Name, command.Arguments, cancellationToken);
  158. }
  159. private string ToQueryString(Dictionary<string, string> nvc)
  160. {
  161. var array = (from item in nvc
  162. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  163. .ToArray();
  164. var args = string.Join("&", array);
  165. if (string.IsNullOrEmpty(args))
  166. {
  167. return args;
  168. }
  169. return "?" + args;
  170. }
  171. public void Dispose()
  172. {
  173. DisposePingTimer();
  174. }
  175. private void DisposePingTimer()
  176. {
  177. if (_pingTimer != null)
  178. {
  179. _pingTimer.Dispose();
  180. _pingTimer = null;
  181. }
  182. }
  183. }
  184. }