Fix Snapcast not working with hostname

This commit is contained in:
Théo Marchal 2022-04-08 01:39:59 +02:00
parent c7a93c2d82
commit 3b59e51368
2 changed files with 23 additions and 12 deletions

View File

@ -54,6 +54,7 @@ namespace unison
bool _isUpdatingStatus = false; bool _isUpdatingStatus = false;
bool _isUpdatingSong = false; bool _isUpdatingSong = false;
public IPAddress _ipAddress;
bool _invalidIp = false; bool _invalidIp = false;
private event EventHandler ConnectionChanged; private event EventHandler ConnectionChanged;
@ -191,9 +192,11 @@ namespace unison
_connection = await ConnectInternal(token); _connection = await ConnectInternal(token);
_commandConnection = await ConnectInternal(token); _commandConnection = await ConnectInternal(token);
} }
catch(MpcNET.Exceptions.MpcConnectException) catch (MpcNET.Exceptions.MpcConnectException e)
{ {
_invalidIp = true; _invalidIp = true;
Trace.WriteLine($"Error in connect: {e.Message}");
return;
} }
if (_connection != null && _commandConnection != null) if (_connection != null && _commandConnection != null)
{ {
@ -219,16 +222,21 @@ namespace unison
private async Task<MpcConnection> ConnectInternal(CancellationToken token) 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 try
{ {
addrList = Dns.GetHostAddresses(Properties.Settings.Default.mpd_host); IPAddress[] addrList = Dns.GetHostAddresses(Properties.Settings.Default.mpd_host);
if (addrList.Length > 0) if (addrList.Length > 0)
ipAddress = addrList[0]; {
foreach (IPAddress addr in addrList)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
_ipAddress = addr;
}
}
} }
catch (Exception) catch (Exception)
{ {
@ -236,7 +244,7 @@ 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); MpcConnection connection = new MpcConnection(_mpdEndpoint);
await connection.ConnectAsync(token); await connection.ConnectAsync(token);
@ -256,10 +264,8 @@ namespace unison
private void Disconnected() private void Disconnected()
{ {
_connected = false; _connected = false;
_connection = null; _connection = null;
_commandConnection = null; _commandConnection = null;
ConnectionChanged?.Invoke(this, EventArgs.Empty); ConnectionChanged?.Invoke(this, EventArgs.Empty);
} }
@ -279,7 +285,10 @@ namespace unison
if (idleChanges.IsResponseValid) if (idleChanges.IsResponseValid)
await HandleIdleResponseAsync(idleChanges.Response.Content); await HandleIdleResponseAsync(idleChanges.Response.Content);
else else
throw new Exception(idleChanges.Response?.Content); {
Trace.WriteLine($"Error in Idle connection thread: {idleChanges.Response?.Content}");
//throw new Exception(idleChanges.Response?.Content);
}
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -14,7 +14,7 @@ namespace unison
{ {
if (Properties.Settings.Default.snapcast_startup) if (Properties.Settings.Default.snapcast_startup)
{ {
var mpd = (MPDHandler)Application.Current.Properties["mpd"]; MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
if (mpd.IsConnected()) if (mpd.IsConnected())
LaunchOrExit(); LaunchOrExit();
} }
@ -37,8 +37,10 @@ namespace unison
{ {
if (!HasStarted && !ForceExit) if (!HasStarted && !ForceExit)
{ {
MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
_snapcast.StartInfo.FileName = Properties.Settings.Default.snapcast_path + @"\snapclient.exe"; _snapcast.StartInfo.FileName = Properties.Settings.Default.snapcast_path + @"\snapclient.exe";
_snapcast.StartInfo.Arguments = $"--host {Properties.Settings.Default.mpd_host}"; _snapcast.StartInfo.Arguments = $"--host {mpd._ipAddress}";
_snapcast.StartInfo.CreateNoWindow = !Properties.Settings.Default.snapcast_window; _snapcast.StartInfo.CreateNoWindow = !Properties.Settings.Default.snapcast_window;
try try
{ {