HttpSessionController.cs 5.9 KB

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