From 1b44c64ec5177f41c4a6c6f877db47fb0d0b8fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Sun, 29 Aug 2021 20:50:47 +0200 Subject: [PATCH] Fix playback but is not resistant to disconnection --- Handlers/MPDHandler.cs | 34 ++++++++++++++++++++++++++++------ unison.sln | 25 ++++++++++--------------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index ea6f73d..66cb9e2 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -49,6 +49,9 @@ namespace unison } private MpcConnection _connection; + + private MpcConnection _commandConnection; + private IPEndPoint _mpdEndpoint; private CancellationTokenSource cancelToken; @@ -67,6 +70,7 @@ namespace unison { var token = cancelToken.Token; _connection = await Connect(token); + _commandConnection = await Connect(token); if (_connection.IsConnected) { _connected = true; @@ -168,21 +172,39 @@ namespace unison _isUpdatingStatus = false; } + bool _isUpdatingSong = false; private async Task UpdateSongAsync() { - MpcNET.Message.IMpdMessage response = await _connection.SendAsync(new CurrentSongCommand()); - if (response != null && response.IsResponseValid) + if (_connection == null) return; + + if (_isUpdatingSong) return; + _isUpdatingSong = true; + + try { - CurrentSong = response.Response.Content; - UpdateSong(); + MpcNET.Message.IMpdMessage response = await _connection.SendAsync(new CurrentSongCommand()); + if (response != null && response.IsResponseValid) + { + CurrentSong = response.Response.Content; + UpdateSong(); + } + else + { + throw new Exception(); + } } + catch + { + await Connect(cancelToken.Token); + } + _isUpdatingSong = false; } public async Task SafelySendCommandAsync(IMpcCommand command) { try { - var response = await _connection.SendAsync(command); + var response = await _commandConnection.SendAsync(command); if (!response.IsResponseValid) { // If we have an MpdError string, only show that as the error to avoid extra noise @@ -323,7 +345,7 @@ namespace unison public async void Consume() { - await SafelySendCommandAsync(new ConsumeCommand(!_currentConsume)); + var response2 = await SafelySendCommandAsync(new ConsumeCommand(!_currentConsume)); } public async void SetVolume(int value) diff --git a/unison.sln b/unison.sln index 8c12821..feb8d8b 100644 --- a/unison.sln +++ b/unison.sln @@ -4,10 +4,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 16.0.31515.178 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "unison", "unison.csproj", "{489048C4-3FCA-4573-B34C-943D03F94D04}" + ProjectSection(ProjectDependencies) = postProject + {230556C6-5AC3-4FD8-8947-C9ABF1416D19} = {230556C6-5AC3-4FD8-8947-C9ABF1416D19} + EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MpcNET", "..\MpcNET\MpcNET.csproj", "{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MpcNET.Test", "..\MpcNET.Test\MpcNET.Test.csproj", "{6789E959-2DB2-412E-8CC1-C1564068F03D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MpcNET", "..\MpcNET\MpcNET.csproj", "{230556C6-5AC3-4FD8-8947-C9ABF1416D19}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -22,18 +23,12 @@ Global {489048C4-3FCA-4573-B34C-943D03F94D04}.Release|Any CPU.Build.0 = Release|Any CPU {489048C4-3FCA-4573-B34C-943D03F94D04}.Release-Stable|Any CPU.ActiveCfg = Release|Any CPU {489048C4-3FCA-4573-B34C-943D03F94D04}.Release-Stable|Any CPU.Build.0 = Release|Any CPU - {AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release|Any CPU.Build.0 = Release|Any CPU - {AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release-Stable|Any CPU.ActiveCfg = Release-Stable|Any CPU - {AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release-Stable|Any CPU.Build.0 = Release-Stable|Any CPU - {6789E959-2DB2-412E-8CC1-C1564068F03D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6789E959-2DB2-412E-8CC1-C1564068F03D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6789E959-2DB2-412E-8CC1-C1564068F03D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6789E959-2DB2-412E-8CC1-C1564068F03D}.Release|Any CPU.Build.0 = Release|Any CPU - {6789E959-2DB2-412E-8CC1-C1564068F03D}.Release-Stable|Any CPU.ActiveCfg = Release|Any CPU - {6789E959-2DB2-412E-8CC1-C1564068F03D}.Release-Stable|Any CPU.Build.0 = Release|Any CPU + {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Debug|Any CPU.Build.0 = Debug|Any CPU + {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release|Any CPU.ActiveCfg = Release|Any CPU + {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release|Any CPU.Build.0 = Release|Any CPU + {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release-Stable|Any CPU.ActiveCfg = Release-Stable|Any CPU + {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release-Stable|Any CPU.Build.0 = Release-Stable|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE