TimeSyncService.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Controller.Session;
  5. using MediaBrowser.Model.Services;
  6. using MediaBrowser.Model.Syncplay;
  7. using Microsoft.Extensions.Logging;
  8. namespace MediaBrowser.Api.Syncplay
  9. {
  10. [Route("/GetUtcTime", "GET", Summary = "Get UtcTime")]
  11. [Authenticated]
  12. public class GetUtcTime : IReturnVoid
  13. {
  14. // Nothing
  15. }
  16. /// <summary>
  17. /// Class TimeSyncService.
  18. /// </summary>
  19. public class TimeSyncService : BaseApiService
  20. {
  21. /// <summary>
  22. /// The session manager.
  23. /// </summary>
  24. private readonly ISessionManager _sessionManager;
  25. /// <summary>
  26. /// The session context.
  27. /// </summary>
  28. private readonly ISessionContext _sessionContext;
  29. public TimeSyncService(
  30. ILogger<TimeSyncService> logger,
  31. IServerConfigurationManager serverConfigurationManager,
  32. IHttpResultFactory httpResultFactory,
  33. ISessionManager sessionManager,
  34. ISessionContext sessionContext)
  35. : base(logger, serverConfigurationManager, httpResultFactory)
  36. {
  37. _sessionManager = sessionManager;
  38. _sessionContext = sessionContext;
  39. }
  40. /// <summary>
  41. /// Handles the specified request.
  42. /// </summary>
  43. /// <param name="request">The request.</param>
  44. /// <value>The current UTC time response.</value>
  45. public UtcTimeResponse Get(GetUtcTime request)
  46. {
  47. // Important to keep the following line at the beginning
  48. var requestReceptionTime = DateTime.UtcNow.ToUniversalTime().ToString("o");
  49. var response = new UtcTimeResponse();
  50. response.RequestReceptionTime = requestReceptionTime;
  51. var currentSession = GetSession(_sessionContext);
  52. // Important to keep the following two lines at the end
  53. var responseTransmissionTime = DateTime.UtcNow.ToUniversalTime().ToString("o");
  54. response.ResponseTransmissionTime = responseTransmissionTime;
  55. // Implementing NTP on such a high level results in this useless
  56. // information being sent. On the other hand it enables future additions.
  57. return response;
  58. }
  59. }
  60. }