HttpSessionController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 Emby.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. 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. }
  34. public void OnActivity()
  35. {
  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 <= 10;
  49. }
  50. }
  51. public bool SupportsMediaControl
  52. {
  53. get { return true; }
  54. }
  55. private Task SendMessage(string name, CancellationToken cancellationToken)
  56. {
  57. return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
  58. }
  59. private Task SendMessage(string name,
  60. Dictionary<string, string> args,
  61. CancellationToken cancellationToken)
  62. {
  63. var url = PostUrl + "/" + name + ToQueryString(args);
  64. return _httpClient.Post(new HttpRequestOptions
  65. {
  66. Url = url,
  67. CancellationToken = cancellationToken,
  68. BufferContent = false
  69. });
  70. }
  71. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  72. {
  73. return Task.FromResult(true);
  74. }
  75. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  76. {
  77. return Task.FromResult(true);
  78. }
  79. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  80. {
  81. return Task.FromResult(true);
  82. }
  83. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  84. {
  85. var dict = new Dictionary<string, string>();
  86. dict["ItemIds"] = string.Join(",", command.ItemIds);
  87. if (command.StartPositionTicks.HasValue)
  88. {
  89. dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  90. }
  91. return SendMessage(command.PlayCommand.ToString(), dict, cancellationToken);
  92. }
  93. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  94. {
  95. var args = new Dictionary<string, string>();
  96. if (command.Command == PlaystateCommand.Seek)
  97. {
  98. if (!command.SeekPositionTicks.HasValue)
  99. {
  100. throw new ArgumentException("SeekPositionTicks cannot be null");
  101. }
  102. args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  103. }
  104. return SendMessage(command.Command.ToString(), args, cancellationToken);
  105. }
  106. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  107. {
  108. return SendMessage("LibraryChanged", info, cancellationToken);
  109. }
  110. public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
  111. {
  112. return SendMessage("RestartRequired", cancellationToken);
  113. }
  114. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  115. {
  116. return Task.FromResult(true);
  117. }
  118. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  119. {
  120. return SendMessage("ServerShuttingDown", cancellationToken);
  121. }
  122. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  123. {
  124. return SendMessage("ServerRestarting", cancellationToken);
  125. }
  126. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  127. {
  128. return SendMessage(command.Name, command.Arguments, cancellationToken);
  129. }
  130. public Task SendMessage<T>(string name, T data, CancellationToken cancellationToken)
  131. {
  132. var url = PostUrl + "/" + name;
  133. var options = new HttpRequestOptions
  134. {
  135. Url = url,
  136. CancellationToken = cancellationToken,
  137. BufferContent = false
  138. };
  139. options.RequestContent = _json.SerializeToString(data);
  140. options.RequestContentType = "application/json";
  141. return _httpClient.Post(new HttpRequestOptions
  142. {
  143. Url = url,
  144. CancellationToken = cancellationToken,
  145. BufferContent = false
  146. });
  147. }
  148. private string ToQueryString(Dictionary<string, string> nvc)
  149. {
  150. var array = (from item in nvc
  151. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  152. .ToArray();
  153. var args = string.Join("&", array);
  154. if (string.IsNullOrEmpty(args))
  155. {
  156. return args;
  157. }
  158. return "?" + args;
  159. }
  160. public void Dispose()
  161. {
  162. }
  163. }
  164. }