Implement NTP like time sync

This commit is contained in:
gion 2020-04-16 16:02:52 +02:00
parent 84d92ba9ce
commit 6519eebabb
3 changed files with 93 additions and 18 deletions

View File

@ -111,14 +111,6 @@ namespace MediaBrowser.Api.Syncplay
public double Ping { get; set; }
}
[Route("/Syncplay/{SessionId}/GetUtcTime", "POST", Summary = "Get UtcTime")]
[Authenticated]
public class SyncplayGetUtcTime : IReturnVoid
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
/// <summary>
/// Class SyncplayService.
/// </summary>
@ -129,6 +121,9 @@ namespace MediaBrowser.Api.Syncplay
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
/// The session context.
/// </summary>
private readonly ISessionContext _sessionContext;
/// <summary>
@ -268,15 +263,5 @@ namespace MediaBrowser.Api.Syncplay
syncplayRequest.Ping = Convert.ToInt64(request.Ping);
_syncplayManager.HandleRequest(currentSession, syncplayRequest);
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <value>The current UTC time.</value>
public string Post(SyncplayGetUtcTime request)
{
return DateTime.UtcNow.ToUniversalTime().ToString("o");
}
}
}

View File

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.Syncplay;
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Syncplay;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.Syncplay
{
[Route("/GetUtcTime", "GET", Summary = "Get UtcTime")]
[Authenticated]
public class GetUtcTime : IReturnVoid
{
// Nothing
}
/// <summary>
/// Class TimeSyncService.
/// </summary>
public class TimeSyncService : BaseApiService
{
/// <summary>
/// The session manager.
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
/// The session context.
/// </summary>
private readonly ISessionContext _sessionContext;
public TimeSyncService(
ILogger<TimeSyncService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
ISessionManager sessionManager,
ISessionContext sessionContext)
: base(logger, serverConfigurationManager, httpResultFactory)
{
_sessionManager = sessionManager;
_sessionContext = sessionContext;
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <value>The current UTC time response.</value>
public UtcTimeResponse Get(GetUtcTime request)
{
// Important to keep the following line at the beginning
var requestReceptionTime = DateTime.UtcNow.ToUniversalTime().ToString("o");
var response = new UtcTimeResponse();
response.RequestReceptionTime = requestReceptionTime;
var currentSession = GetSession(_sessionContext);
// Important to keep the following two lines at the end
var responseTransmissionTime = DateTime.UtcNow.ToUniversalTime().ToString("o");
response.ResponseTransmissionTime = responseTransmissionTime;
// Implementing NTP on such a high level results in this useless
// information being sent. On the other hand it enables future additions.
return response;
}
}
}

View File

@ -0,0 +1,20 @@
namespace MediaBrowser.Model.Syncplay
{
/// <summary>
/// Class UtcTimeResponse.
/// </summary>
public class UtcTimeResponse
{
/// <summary>
/// Gets or sets the UTC time when request has been received.
/// </summary>
/// <value>The UTC time when request has been received.</value>
public string RequestReceptionTime { get; set; }
/// <summary>
/// Gets or sets the UTC time when response has been sent.
/// </summary>
/// <value>The UTC time when response has been sent.</value>
public string ResponseTransmissionTime { get; set; }
}
}