Fix playback but is not resistant to disconnection

This commit is contained in:
Théo Marchal 2021-08-29 20:50:47 +02:00
parent 80d6395c8e
commit 1b44c64ec5
2 changed files with 38 additions and 21 deletions

View File

@ -49,6 +49,9 @@ namespace unison
} }
private MpcConnection _connection; private MpcConnection _connection;
private MpcConnection _commandConnection;
private IPEndPoint _mpdEndpoint; private IPEndPoint _mpdEndpoint;
private CancellationTokenSource cancelToken; private CancellationTokenSource cancelToken;
@ -67,6 +70,7 @@ namespace unison
{ {
var token = cancelToken.Token; var token = cancelToken.Token;
_connection = await Connect(token); _connection = await Connect(token);
_commandConnection = await Connect(token);
if (_connection.IsConnected) if (_connection.IsConnected)
{ {
_connected = true; _connected = true;
@ -168,7 +172,15 @@ namespace unison
_isUpdatingStatus = false; _isUpdatingStatus = false;
} }
bool _isUpdatingSong = false;
private async Task UpdateSongAsync() private async Task UpdateSongAsync()
{
if (_connection == null) return;
if (_isUpdatingSong) return;
_isUpdatingSong = true;
try
{ {
MpcNET.Message.IMpdMessage<MpcNET.Types.IMpdFile> response = await _connection.SendAsync(new CurrentSongCommand()); MpcNET.Message.IMpdMessage<MpcNET.Types.IMpdFile> response = await _connection.SendAsync(new CurrentSongCommand());
if (response != null && response.IsResponseValid) if (response != null && response.IsResponseValid)
@ -176,13 +188,23 @@ namespace unison
CurrentSong = response.Response.Content; CurrentSong = response.Response.Content;
UpdateSong(); UpdateSong();
} }
else
{
throw new Exception();
}
}
catch
{
await Connect(cancelToken.Token);
}
_isUpdatingSong = false;
} }
public async Task<T> SafelySendCommandAsync<T>(IMpcCommand<T> command) public async Task<T> SafelySendCommandAsync<T>(IMpcCommand<T> command)
{ {
try try
{ {
var response = await _connection.SendAsync(command); var response = await _commandConnection.SendAsync(command);
if (!response.IsResponseValid) if (!response.IsResponseValid)
{ {
// If we have an MpdError string, only show that as the error to avoid extra noise // 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() public async void Consume()
{ {
await SafelySendCommandAsync(new ConsumeCommand(!_currentConsume)); var response2 = await SafelySendCommandAsync(new ConsumeCommand(!_currentConsume));
} }
public async void SetVolume(int value) public async void SetVolume(int value)

View File

@ -4,10 +4,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 16.0.31515.178 VisualStudioVersion = 16.0.31515.178
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "unison", "unison.csproj", "{489048C4-3FCA-4573-B34C-943D03F94D04}" 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 EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MpcNET", "..\MpcNET\MpcNET.csproj", "{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MpcNET", "..\MpcNET\MpcNET.csproj", "{230556C6-5AC3-4FD8-8947-C9ABF1416D19}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MpcNET.Test", "..\MpcNET.Test\MpcNET.Test.csproj", "{6789E959-2DB2-412E-8CC1-C1564068F03D}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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|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.ActiveCfg = Release|Any CPU
{489048C4-3FCA-4573-B34C-943D03F94D04}.Release-Stable|Any CPU.Build.0 = 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 {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Debug|Any CPU.Build.0 = Debug|Any CPU {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release|Any CPU.ActiveCfg = Release|Any CPU {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release|Any CPU.Build.0 = Release|Any CPU {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release|Any CPU.Build.0 = Release|Any CPU
{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release-Stable|Any CPU.ActiveCfg = Release-Stable|Any CPU {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.Release-Stable|Any CPU.ActiveCfg = Release-Stable|Any CPU
{AD425CBC-9C18-4B4A-AB51-4DE8F50FB6A9}.Release-Stable|Any CPU.Build.0 = Release-Stable|Any CPU {230556C6-5AC3-4FD8-8947-C9ABF1416D19}.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
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE