Merge branch 'main' into shuffle

This commit is contained in:
2022-04-14 01:08:49 +02:00
14 changed files with 631 additions and 167 deletions

View File

@ -54,7 +54,7 @@ namespace unison
bool _isUpdatingStatus = false;
bool _isUpdatingSong = false;
bool _invalidIp = false;
public IPAddress _ipAddress;
private event EventHandler ConnectionChanged;
private event EventHandler StatusChanged;
@ -64,12 +64,10 @@ namespace unison
private MpcConnection _connection;
private MpcConnection _commandConnection;
private IPEndPoint _mpdEndpoint;
private CancellationTokenSource cancelToken;
private CancellationTokenSource _cancelToken;
public MPDHandler()
{
cancelToken = new CancellationTokenSource();
Initialize(null, null);
_stats = new Statistics();
@ -97,7 +95,7 @@ namespace unison
void OnConnectionChanged(object sender, EventArgs e)
{
if (!_connected && !_invalidIp)
if (!_connected)
_retryTimer.Start();
else
_retryTimer.Stop();
@ -160,7 +158,7 @@ namespace unison
IMpdMessage<T> response = await _commandConnection.SendAsync(command);
if (!response.IsResponseValid)
{
var mpdError = response.Response?.Result?.MpdError;
string mpdError = response.Response?.Result?.MpdError;
if (mpdError != null && mpdError != "")
throw new Exception(mpdError);
else
@ -185,19 +183,23 @@ namespace unison
public async void Connect()
{
CancellationToken token = cancelToken.Token;
_cancelToken = new CancellationTokenSource();
CancellationToken token = _cancelToken.Token;
try
{
_connection = await ConnectInternal(token);
_commandConnection = await ConnectInternal(token);
}
catch(MpcNET.Exceptions.MpcConnectException)
catch (MpcNET.Exceptions.MpcConnectException e)
{
_invalidIp = true;
_connected = false;
Trace.WriteLine($"Error in connect: {e.Message}");
ConnectionChanged?.Invoke(this, EventArgs.Empty);
return;
}
if (_connection != null && _commandConnection != null)
{
_invalidIp = false;
if (_connection.IsConnected && _commandConnection.IsConnected)
{
_connected = true;
@ -207,6 +209,7 @@ namespace unison
}
else
{
_connected = false;
ConnectionChanged?.Invoke(this, EventArgs.Empty);
return;
}
@ -219,16 +222,21 @@ namespace unison
private async Task<MpcConnection> ConnectInternal(CancellationToken token)
{
IPAddress.TryParse(Properties.Settings.Default.mpd_host, out IPAddress ipAddress);
IPAddress.TryParse(Properties.Settings.Default.mpd_host, out _ipAddress);
if (ipAddress == null)
if (_ipAddress == null)
{
IPAddress[] addrList;
try
{
addrList = Dns.GetHostAddresses(Properties.Settings.Default.mpd_host);
IPAddress[] addrList = Dns.GetHostAddresses(Properties.Settings.Default.mpd_host);
if (addrList.Length > 0)
ipAddress = addrList[0];
{
foreach (IPAddress addr in addrList)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
_ipAddress = addr;
}
}
}
catch (Exception)
{
@ -236,11 +244,11 @@ namespace unison
}
}
_mpdEndpoint = new IPEndPoint(ipAddress, Properties.Settings.Default.mpd_port);
_mpdEndpoint = new IPEndPoint(_ipAddress, Properties.Settings.Default.mpd_port);
MpcConnection connection = new MpcConnection(_mpdEndpoint);
await connection.ConnectAsync(token);
/*if (!string.IsNullOrEmpty(Properties.Settings.Default.mpd_password))
if (!string.IsNullOrEmpty(Properties.Settings.Default.mpd_password))
{
IMpdMessage<string> result = await connection.SendAsync(new PasswordCommand(Properties.Settings.Default.mpd_password));
if (!result.IsResponseValid)
@ -248,19 +256,21 @@ namespace unison
string mpdError = result.Response?.Result?.MpdError;
Trace.WriteLine(mpdError);
}
}*/
}
return connection;
}
private void Disconnected()
{
public void Disconnected()
{
_connected = false;
_connection = null;
_commandConnection = null;
ConnectionChanged?.Invoke(this, EventArgs.Empty);
if (_connection != null)
_connection = null;
if (_commandConnection != null)
_commandConnection = null;
}
private void Loop(CancellationToken token)
@ -271,24 +281,29 @@ namespace unison
{
try
{
token.ThrowIfCancellationRequested();
if (token.IsCancellationRequested || _connection == null)
break;
var idleChanges = await _connection.SendAsync(new IdleCommand("stored_playlist playlist player mixer output options"));
IMpdMessage<string> idleChanges = await _connection.SendAsync(new IdleCommand("stored_playlist playlist player mixer output options update"));
if (idleChanges.IsResponseValid)
await HandleIdleResponseAsync(idleChanges.Response.Content);
else
{
Trace.WriteLine($"Error in Idle connection thread: {idleChanges.Response?.Content}");
throw new Exception(idleChanges.Response?.Content);
}
}
catch (Exception e)
{
Trace.WriteLine($"Error in Idle connection thread: {e.Message}");
Disconnected();
break;
}
}
}).ConfigureAwait(false);
}, token).ConfigureAwait(false);
}
private async Task HandleIdleResponseAsync(string subsystems)
@ -366,6 +381,7 @@ namespace unison
List<byte> data = new List<byte>();
try
{
bool ReadPictureFailed = true;
long totalBinarySize = 9999;
long currentSize = 0;
@ -374,18 +390,41 @@ namespace unison
if (_connection == null)
return;
var albumReq = await _connection.SendAsync(new AlbumArtCommand(path, currentSize));
IMpdMessage<MpdBinaryData> albumReq = await _connection.SendAsync(new ReadPictureCommand(path, currentSize));
if (!albumReq.IsResponseValid)
break;
var response = albumReq.Response.Content;
if (response.Binary == 0)
MpdBinaryData response = albumReq.Response.Content;
if (response == null || response.Binary == 0)
break;
ReadPictureFailed = false;
totalBinarySize = response.Size;
currentSize += response.Binary;
data.AddRange(response.Data);
}
while (currentSize < totalBinarySize && !token.IsCancellationRequested);
do
{
if (!ReadPictureFailed)
break;
if (_connection == null)
return;
IMpdMessage<MpdBinaryData> albumReq = await _connection.SendAsync(new AlbumArtCommand(path, currentSize));
if (!albumReq.IsResponseValid)
break;
MpdBinaryData response = albumReq.Response.Content;
if (response == null || response.Binary == 0)
break;
totalBinarySize = response.Size;
currentSize += response.Binary;
data.AddRange(response.Data);
} while (currentSize < totalBinarySize && !token.IsCancellationRequested);
}
while (currentSize < totalBinarySize && !token.IsCancellationRequested);
}
catch (Exception e)
{
@ -503,7 +542,6 @@ namespace unison
public void AddSong(string Uri)
{
Debug.WriteLine("AddCommand path: " + Uri);
SendCommand(new AddCommand(Uri));
}