HttpSessionController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. var period = TimeSpan.FromSeconds(60);
  71. _pingTimer.Change(period, period);
  72. }
  73. private Task SendMessage(string name, CancellationToken cancellationToken)
  74. {
  75. return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
  76. }
  77. private async Task SendMessage(string name,
  78. Dictionary<string, string> args,
  79. CancellationToken cancellationToken)
  80. {
  81. var url = _postUrl + "/" + name + ToQueryString(args);
  82. await _httpClient.Post(new HttpRequestOptions
  83. {
  84. Url = url,
  85. CancellationToken = cancellationToken
  86. }).ConfigureAwait(false);
  87. ResetPingTimer();
  88. }
  89. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  90. {
  91. return Task.FromResult(true);
  92. }
  93. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  94. {
  95. return Task.FromResult(true);
  96. }
  97. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  98. {
  99. return Task.FromResult(true);
  100. }
  101. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  102. {
  103. return Task.FromResult(true);
  104. //return SendMessage(new WebSocketMessage<PlayRequest>
  105. //{
  106. // MessageType = "Play",
  107. // Data = command
  108. //}, cancellationToken);
  109. }
  110. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  111. {
  112. var args = new Dictionary<string, string>();
  113. if (command.Command == PlaystateCommand.Seek)
  114. {
  115. }
  116. return SendMessage(command.Command.ToString(), cancellationToken);
  117. }
  118. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  119. {
  120. return Task.FromResult(true);
  121. }
  122. public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
  123. {
  124. return SendMessage("RestartRequired", cancellationToken);
  125. }
  126. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  127. {
  128. return Task.FromResult(true);
  129. }
  130. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  131. {
  132. return SendMessage("ServerShuttingDown", cancellationToken);
  133. }
  134. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  135. {
  136. return SendMessage("ServerRestarting", cancellationToken);
  137. }
  138. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  139. {
  140. return SendMessage(command.Name, command.Arguments, cancellationToken);
  141. }
  142. private string ToQueryString(Dictionary<string, string> nvc)
  143. {
  144. var array = (from item in nvc
  145. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  146. .ToArray();
  147. var args = string.Join("&", array);
  148. if (string.IsNullOrEmpty(args))
  149. {
  150. return args;
  151. }
  152. return "?" + args;
  153. }
  154. public void Dispose()
  155. {
  156. DisposePingTimer();
  157. }
  158. private void DisposePingTimer()
  159. {
  160. if (_pingTimer != null)
  161. {
  162. _pingTimer.Dispose();
  163. _pingTimer = null;
  164. }
  165. }
  166. }
  167. }