Skip to content

Commit

Permalink
fix - Fixed .NET Framework "cancel" exceptions
Browse files Browse the repository at this point in the history
---

We've fixed the .NET Framework version of BB Basolia causing hangs.

---

Type: fix
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed May 31, 2024
1 parent aa282b1 commit de3803a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
9 changes: 6 additions & 3 deletions BassBoom.Basolia/File/FileTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void OpenFile(string path)
/// Opens a remote radio station
/// </summary>
public static void OpenUrl(string path) =>
OpenUrlAsync(path).Wait();
Task.Run(() => OpenUrlAsync(path)).Wait();

/// <summary>
/// Opens a remote radio station
Expand All @@ -123,8 +123,11 @@ public static async Task OpenUrlAsync(string path)
throw new BasoliaException("Provide a path to a music file or a radio station", mpg123_errors.MPG123_BAD_FILE);

// Check to see if the radio station exists
#if NET48
RadioTools.client = new();
#endif
RadioTools.client.DefaultRequestHeaders.Add("Icy-MetaData", "1");
var reply = await RadioTools.client.GetAsync(path, HttpCompletionOption.ResponseHeadersRead);
var reply = await RadioTools.client.GetAsync(path, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
RadioTools.client.DefaultRequestHeaders.Remove("Icy-MetaData");
if (!reply.IsSuccessStatusCode)
throw new BasoliaException($"This radio station doesn't exist. Error code: {(int)reply.StatusCode} ({reply.StatusCode}).", mpg123_errors.MPG123_BAD_FILE);
Expand All @@ -147,7 +150,7 @@ public static async Task OpenUrlAsync(string path)
isOpened = true;
isRadioStation = true;
}
currentFile = new(true, path, reply.Content.ReadAsStreamAsync().Result, reply.Headers, reply.Headers.GetValues("icy-name").First());
currentFile = new(true, path, await reply.Content.ReadAsStreamAsync().ConfigureAwait(false), reply.Headers, reply.Headers.GetValues("icy-name").First());

// If necessary, feed.
PlaybackTools.FeedRadio();
Expand Down
5 changes: 2 additions & 3 deletions BassBoom.Basolia/Radio/IcecastServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class IcecastServer : IRadioServer
private readonly List<StreamInfo> streams = [];
internal JToken streamToken;
internal HtmlDocument streamHtmlToken = new();
internal static HttpClient client = new();

/// <inheritdoc/>
public string ServerHost { get; }
Expand Down Expand Up @@ -131,7 +130,7 @@ public async Task RefreshAsync()
{
try
{
await InitializeStatsAsync();
await InitializeStatsAsync().ConfigureAwait(false);
FinalizeIcecast();
}
catch (Exception ex)
Expand All @@ -144,7 +143,7 @@ internal async Task InitializeStatsAsync()
{
// Use the full address to download the statistics.
Uri statisticsUri = new(ServerHostFull + "/status-json.xsl");
string serverResponse = await client.GetStringAsync(statisticsUri);
string serverResponse = await RadioTools.client.GetStringAsync(statisticsUri).ConfigureAwait(false);
streamToken = JToken.Parse(serverResponse)["icestats"];
}

Expand Down
9 changes: 6 additions & 3 deletions BassBoom.Basolia/Radio/RadioTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class RadioTools
/// <param name="radioUrl">Radio station URL</param>
/// <returns>An instance of <see cref="IRadioServer"/>. <see langword="null"/> if type can't be determined. <see cref="ShoutcastServer"/> if this radio station server uses Shoutcast, and <see cref="IcecastServer"/> if it uses Icecast.</returns>
public static IRadioServer GetRadioInfo(string radioUrl) =>
GetRadioInfoAsync(radioUrl).Result;
Task.Run(() => GetRadioInfoAsync(radioUrl)).GetAwaiter().GetResult();

/// <summary>
/// Gets extended radio station information asynchronously
Expand All @@ -52,8 +52,11 @@ public static async Task<IRadioServer> GetRadioInfoAsync(string radioUrl)
var uri = new Uri(radioUrl);

// Check to see if the radio station exists
#if NET48
client = new();
#endif
client.DefaultRequestHeaders.Add("Icy-MetaData", "1");
var reply = await client.GetAsync(radioUrl, HttpCompletionOption.ResponseHeadersRead);
var reply = await client.GetAsync(radioUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
client.DefaultRequestHeaders.Remove("Icy-MetaData");
if (!reply.IsSuccessStatusCode)
throw new BasoliaMiscException($"This radio station doesn't exist. Error code: {(int)reply.StatusCode} ({reply.StatusCode}).");
Expand Down Expand Up @@ -82,7 +85,7 @@ public static async Task<IRadioServer> GetRadioInfoAsync(string radioUrl)
};
if (stats is null)
return null;
await stats.RefreshAsync();
await stats.RefreshAsync().ConfigureAwait(false);
return stats;
}
}
Expand Down
6 changes: 3 additions & 3 deletions BassBoom.Basolia/Radio/ShoutcastServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public async Task RefreshAsync()
{
try
{
await InitializeStatsAsync();
await InitializeStatsAsync().ConfigureAwait(false);

// Determine version of Shoutcast
if (serverVersion == ShoutcastVersion.v1)
Expand All @@ -189,14 +189,14 @@ internal async Task InitializeStatsAsync()
// /7.html
Uri statisticsUri = new(ServerHostFull + "/statistics?json=1");
Uri fallbackUri = new(ServerHostFull + "/7.html");
string serverResponse = await RadioTools.client.GetStringAsync(statisticsUri);
string serverResponse = await RadioTools.client.GetStringAsync(statisticsUri).ConfigureAwait(false);

// Shoutcast v1.x doesn't have /statistics...
if (serverResponse.Contains("Invalid resource"))
{
// Detected v1. Fallback to /7.html
serverVersion = ShoutcastVersion.v1;
serverResponse = await RadioTools.client.GetStringAsync(fallbackUri);
serverResponse = await RadioTools.client.GetStringAsync(fallbackUri).ConfigureAwait(false);
streamHtmlToken.LoadHtml(serverResponse);
}
else
Expand Down

0 comments on commit de3803a

Please sign in to comment.