From cabfb165da7c552c4aecc00da78b6f31de7be084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Sat, 29 Oct 2022 22:44:41 +0200 Subject: [PATCH 1/6] Update .NET version to 6.0 --- unison.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unison.csproj b/unison.csproj index bad23d8..bb20b3f 100644 --- a/unison.csproj +++ b/unison.csproj @@ -2,7 +2,7 @@ WinExe - net5.0-windows + net6.0-windows true Resources\icon-full.ico From cb741349fabdb3c11d38c7e0e43ae85c394276dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Sat, 29 Oct 2022 23:46:43 +0200 Subject: [PATCH 2/6] Ugly patch to avoid exception in GetAlbumCover --- Handlers/MPDHandler.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index e3db9a4..f29ef5a 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -390,7 +390,7 @@ namespace unison if (_connection == null) return; - IMpdMessage albumReq = await _connection.SendAsync(new ReadPictureCommand(path, currentSize)); + IMpdMessage albumReq = await _connection.SendAsync(new AlbumArtCommand(path, currentSize)); if (!albumReq.IsResponseValid) break; @@ -412,7 +412,7 @@ namespace unison if (_connection == null) return; - IMpdMessage albumReq = await _connection.SendAsync(new AlbumArtCommand(path, currentSize)); + IMpdMessage albumReq = await _connection.SendAsync(new ReadPictureCommand(path, currentSize)); if (!albumReq.IsResponseValid) break; @@ -441,7 +441,7 @@ namespace unison { _cover = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); } - catch (System.NotSupportedException) + catch { _cover = null; } From 0898d5cf119889355d0229a5c8a181a2cda46412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Thu, 3 Nov 2022 00:19:35 +0100 Subject: [PATCH 3/6] Fix connection change not working --- Handlers/MPDHandler.cs | 116 +++++++++++++++++++++++++++-------------- Views/Settings.xaml.cs | 7 +-- 2 files changed, 78 insertions(+), 45 deletions(-) diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index f29ef5a..b741219 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -8,7 +8,6 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; -using System.Windows.Threading; using MpcNET; using MpcNET.Commands.Database; using MpcNET.Commands.Playback; @@ -49,7 +48,7 @@ namespace unison private BitmapFrame _cover; public Statistics _stats; private readonly System.Timers.Timer _elapsedTimer; - private DispatcherTimer _retryTimer; + //private DispatcherTimer _retryTimer; bool _isUpdatingStatus = false; bool _isUpdatingSong = false; @@ -64,17 +63,19 @@ namespace unison private MpcConnection _connection; private MpcConnection _commandConnection; private IPEndPoint _mpdEndpoint; - private CancellationTokenSource _cancelToken; + + private CancellationTokenSource _cancelCommand; + private CancellationTokenSource _cancelConnect; public MPDHandler() { - Initialize(null, null); + Startup(); _stats = new Statistics(); - _retryTimer = new DispatcherTimer(); - _retryTimer.Interval = TimeSpan.FromSeconds(5); - _retryTimer.Tick += Initialize; + //_retryTimer = new DispatcherTimer(); + //_retryTimer.Interval = TimeSpan.FromSeconds(5); + //_retryTimer.Tick += Initialize; _elapsedTimer = new System.Timers.Timer(500); _elapsedTimer.Elapsed += new System.Timers.ElapsedEventHandler(ElapsedTimer); @@ -95,10 +96,10 @@ namespace unison void OnConnectionChanged(object sender, EventArgs e) { - if (!_connected) - _retryTimer.Start(); - else - _retryTimer.Stop(); + //if (!_connected) + // _retryTimer.Start(); + //else + // _retryTimer.Stop(); Application.Current.Dispatcher.Invoke(() => { @@ -147,7 +148,7 @@ namespace unison public async Task SafelySendCommandAsync(IMpcCommand command) { - if (_commandConnection == null) + if (_commandConnection == null || !IsConnected()) { Trace.WriteLine("[SafelySendCommandAsync] no command connection"); return default(T); @@ -175,21 +176,52 @@ namespace unison return default(T); } - private void Initialize(object sender, EventArgs e) + public async void Startup() { - if (!_connected) - Connect(); + await Initialize(); } - public async void Connect() + public async Task Initialize() { - _cancelToken = new CancellationTokenSource(); - CancellationToken token = _cancelToken.Token; + Trace.WriteLine("Initializing"); + + Disconnected(); + + if (!_connected) + await Connect(); + } + + public void Disconnected() + { + _connected = false; + ConnectionChanged?.Invoke(this, EventArgs.Empty); + + _commandConnection?.DisconnectAsync(); + _connection?.DisconnectAsync(); + + _cancelConnect?.Cancel(); + _cancelConnect = new CancellationTokenSource(); + + _cancelCommand?.Cancel(); + _cancelCommand = new CancellationTokenSource(); + + _connection = null; + _commandConnection = null; + + Trace.WriteLine("Disconnected"); + } + + public async Task Connect() + { + Trace.WriteLine("Connecting"); + + if (_cancelCommand.IsCancellationRequested || _cancelConnect.IsCancellationRequested) + return; try { - _connection = await ConnectInternal(token); - _commandConnection = await ConnectInternal(token); + _connection = await ConnectInternal(_cancelConnect.Token); + _commandConnection = await ConnectInternal(_cancelCommand.Token); } catch (MpcNET.Exceptions.MpcConnectException e) { @@ -217,11 +249,14 @@ namespace unison await UpdateStatusAsync(); await UpdateSongAsync(); - Loop(token); + Loop(_cancelCommand.Token); } private async Task ConnectInternal(CancellationToken token) { + if (token.IsCancellationRequested) + return null; + IPAddress.TryParse(Properties.Settings.Default.mpd_host, out _ipAddress); if (_ipAddress == null) @@ -261,18 +296,6 @@ namespace unison return connection; } - public void Disconnected() - { - _connected = false; - ConnectionChanged?.Invoke(this, EventArgs.Empty); - - if (_connection != null) - _connection = null; - - if (_commandConnection != null) - _commandConnection = null; - } - private void Loop(CancellationToken token) { Task.Run(async () => @@ -281,8 +304,7 @@ namespace unison { try { - token.ThrowIfCancellationRequested(); - if (token.IsCancellationRequested || _connection == null) + if (token.IsCancellationRequested || _connection == null || !IsConnected()) break; IMpdMessage idleChanges = await _connection.SendAsync(new IdleCommand("stored_playlist playlist player mixer output options update")); @@ -291,14 +313,17 @@ namespace unison await HandleIdleResponseAsync(idleChanges.Response.Content); else { - Trace.WriteLine($"Error in Idle connection thread: {idleChanges.Response?.Content}"); + Trace.WriteLine($"Error in Idle connection thread (1): {idleChanges.Response?.Content}"); throw new Exception(idleChanges.Response?.Content); } } catch (Exception e) { - Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); - Disconnected(); + if (token.IsCancellationRequested) + Trace.WriteLine($"Idle connection cancelled."); + else + Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); + await Initialize(); break; } } @@ -321,6 +346,7 @@ namespace unison catch (Exception e) { Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); + await Initialize(); } } @@ -345,6 +371,7 @@ namespace unison catch (Exception e) { Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); + await Initialize(); } _isUpdatingStatus = false; @@ -352,6 +379,8 @@ namespace unison private async Task UpdateSongAsync() { + Trace.WriteLine("Updating song"); + if (_connection == null || _isUpdatingSong) return; @@ -371,13 +400,17 @@ namespace unison catch (Exception e) { Trace.WriteLine($"Error in Idle connection thread: {e.Message}"); + await Initialize(); } _isUpdatingSong = false; } - private async void GetAlbumCover(string path, CancellationToken token = default) + private async void GetAlbumCover(string path, CancellationToken token) { + if (token.IsCancellationRequested) + return; + List data = new List(); try { @@ -428,6 +461,9 @@ namespace unison } catch (Exception e) { + if (token.IsCancellationRequested) + return; + Trace.WriteLine("Exception caught while getting albumart: " + e); return; } @@ -476,7 +512,7 @@ namespace unison SongChanged?.Invoke(this, EventArgs.Empty); string uri = Regex.Escape(_currentSong.Path); - GetAlbumCover(uri); + GetAlbumCover(uri, _cancelCommand.Token); } public void UpdateCover() diff --git a/Views/Settings.xaml.cs b/Views/Settings.xaml.cs index 530a43c..b384b3d 100644 --- a/Views/Settings.xaml.cs +++ b/Views/Settings.xaml.cs @@ -111,14 +111,11 @@ namespace unison SaveSettings(); - MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"]; - if (mpd.IsConnected()) - mpd = new MPDHandler(); - ConnectButton.IsEnabled = false; ConnectionStatus.Text = unison.Resources.Resources.Settings_ConnectionStatusConnecting; - System.Threading.Tasks.Task.Run(() => { mpd.Connect(); }); + MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"]; + System.Threading.Tasks.Task.Run(async () => { await mpd.Initialize(); }); } private void SnapcastReset_Clicked(object sender, RoutedEventArgs e) From 8033aebb2345d44d314c59c4b7342749ae97b558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Thu, 3 Nov 2022 00:39:03 +0100 Subject: [PATCH 4/6] Restore auto-reconnect --- Handlers/MPDHandler.cs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index b741219..744e055 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; +using System.Windows.Threading; using MpcNET; using MpcNET.Commands.Database; using MpcNET.Commands.Playback; @@ -48,7 +49,7 @@ namespace unison private BitmapFrame _cover; public Statistics _stats; private readonly System.Timers.Timer _elapsedTimer; - //private DispatcherTimer _retryTimer; + private DispatcherTimer _retryTimer; bool _isUpdatingStatus = false; bool _isUpdatingSong = false; @@ -69,13 +70,13 @@ namespace unison public MPDHandler() { - Startup(); + Startup(null, null); _stats = new Statistics(); - //_retryTimer = new DispatcherTimer(); - //_retryTimer.Interval = TimeSpan.FromSeconds(5); - //_retryTimer.Tick += Initialize; + _retryTimer = new DispatcherTimer(); + _retryTimer.Interval = TimeSpan.FromSeconds(5); + _retryTimer.Tick += Startup; _elapsedTimer = new System.Timers.Timer(500); _elapsedTimer.Elapsed += new System.Timers.ElapsedEventHandler(ElapsedTimer); @@ -96,10 +97,10 @@ namespace unison void OnConnectionChanged(object sender, EventArgs e) { - //if (!_connected) - // _retryTimer.Start(); - //else - // _retryTimer.Stop(); + if (!_connected) + _retryTimer.Start(); + else + _retryTimer.Stop(); Application.Current.Dispatcher.Invoke(() => { @@ -176,7 +177,7 @@ namespace unison return default(T); } - public async void Startup() + public async void Startup(object sender, EventArgs e) { await Initialize(); } From 67667c74da40b661ddfdd3997dea513149fa2b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Thu, 3 Nov 2022 02:03:37 +0100 Subject: [PATCH 5/6] Update CHANGELOG to 1.3.1 --- CHANGELOG.md | 8 ++++++++ unison.csproj | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89abf11..4b9429c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## v1.3.1 + +*Released: 03/11/2022* + +* Update .NET version from 5.0 to 6.0 +* Fix: simple patch to avoid a crash concerning GetAlbumCover +* Fix: connection change now working + ## v1.3 *Released: 18/04/2022* diff --git a/unison.csproj b/unison.csproj index bb20b3f..d4ea26b 100644 --- a/unison.csproj +++ b/unison.csproj @@ -7,7 +7,7 @@ Resources\icon-full.ico unison.App - 1.3 + 1.3.1 Théo Marchal LICENSE From b33ae3b9beaf620bb72684e0d7bd2685dc011e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Thu, 3 Nov 2022 22:20:21 +0100 Subject: [PATCH 6/6] Update publish profile --- Properties/PublishProfiles/FolderProfile.pubxml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Properties/PublishProfiles/FolderProfile.pubxml b/Properties/PublishProfiles/FolderProfile.pubxml index 430d9d7..0784803 100644 --- a/Properties/PublishProfiles/FolderProfile.pubxml +++ b/Properties/PublishProfiles/FolderProfile.pubxml @@ -4,14 +4,14 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - Release + Release-Stable Any CPU publish\ FileSystem - net5.0-windows + net6.0-windows false win-x64 - True - False + true + false \ No newline at end of file