From 24b925cb56c171a6d88159344f25bc910af5cd29 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 29 Jan 2016 14:37:20 -0500 Subject: [PATCH] remove timer from PlayToController --- MediaBrowser.Dlna/PlayTo/PlayToController.cs | 49 +++++++------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/MediaBrowser.Dlna/PlayTo/PlayToController.cs b/MediaBrowser.Dlna/PlayTo/PlayToController.cs index cb3629678f..7e021b8771 100644 --- a/MediaBrowser.Dlna/PlayTo/PlayToController.cs +++ b/MediaBrowser.Dlna/PlayTo/PlayToController.cs @@ -37,11 +37,28 @@ namespace MediaBrowser.Dlna.PlayTo private readonly IDeviceDiscovery _deviceDiscovery; private readonly string _serverAddress; private readonly string _accessToken; + private readonly DateTime _creationTime; public bool IsSessionActive { get { + var lastDateKnownActivity = new[] { _creationTime, _device.DateLastActivity }.Max(); + + if (DateTime.UtcNow >= lastDateKnownActivity.AddSeconds(120)) + { + try + { + // Session is inactive, mark it for Disposal and don't start the elapsed timer. + _sessionManager.ReportSessionEnded(_session.Id); + } + catch (Exception ex) + { + _logger.ErrorException("Error in ReportSessionEnded", ex); + } + return false; + } + return _device != null; } } @@ -55,8 +72,6 @@ namespace MediaBrowser.Dlna.PlayTo get { return IsSessionActive; } } - private Timer _updateTimer; - public PlayToController(SessionInfo session, ISessionManager sessionManager, ILibraryManager libraryManager, ILogger logger, IDlnaManager dlnaManager, IUserManager userManager, IImageProcessor imageProcessor, string serverAddress, string accessToken, IDeviceDiscovery deviceDiscovery, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager) { _session = session; @@ -72,6 +87,7 @@ namespace MediaBrowser.Dlna.PlayTo _mediaSourceManager = mediaSourceManager; _accessToken = accessToken; _logger = logger; + _creationTime = DateTime.UtcNow; } public void Init(Device device) @@ -84,8 +100,6 @@ namespace MediaBrowser.Dlna.PlayTo _device.Start(); _deviceDiscovery.DeviceLeft += _deviceDiscovery_DeviceLeft; - - _updateTimer = new Timer(updateTimer_Elapsed, null, 60000, 60000); } void _deviceDiscovery_DeviceLeft(object sender, SsdpMessageEventArgs e) @@ -117,22 +131,6 @@ namespace MediaBrowser.Dlna.PlayTo } } - private void updateTimer_Elapsed(object state) - { - if (DateTime.UtcNow >= _device.DateLastActivity.AddSeconds(120)) - { - try - { - // Session is inactive, mark it for Disposal and don't start the elapsed timer. - _sessionManager.ReportSessionEnded(_session.Id); - } - catch (Exception ex) - { - _logger.ErrorException("Error in ReportSessionEnded", ex); - } - } - } - async void _device_MediaChanged(object sender, MediaChangedEventArgs e) { try @@ -634,21 +632,10 @@ namespace MediaBrowser.Dlna.PlayTo _device.MediaChanged -= _device_MediaChanged; _deviceDiscovery.DeviceLeft -= _deviceDiscovery_DeviceLeft; - DisposeUpdateTimer(); - _device.Dispose(); } } - private void DisposeUpdateTimer() - { - if (_updateTimer != null) - { - _updateTimer.Dispose(); - _updateTimer = null; - } - } - private readonly CultureInfo _usCulture = new CultureInfo("en-US"); public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)