From 8ac2075c61727961b1fe43554bc077abdc7ee7b5 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 2 Nov 2022 10:04:24 +0900 Subject: [PATCH] Fix possible threading issues Not really sure of the best way to handle this in general. It could be argued that this should be a `Component` type and the bindable bound in `LoadComplete()`... --- osu.Game/Online/Notifications/NotificationsClient.cs | 6 +++--- .../Notifications/Polling/PollingNotificationsClient.cs | 5 +---- .../Polling/PollingNotificationsClientConnector.cs | 5 +---- osu.Game/Online/SocketClientConnector.cs | 7 +++++-- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/osu.Game/Online/Notifications/NotificationsClient.cs b/osu.Game/Online/Notifications/NotificationsClient.cs index 6d9226ca17..8c41ae10d7 100644 --- a/osu.Game/Online/Notifications/NotificationsClient.cs +++ b/osu.Game/Online/Notifications/NotificationsClient.cs @@ -21,14 +21,14 @@ namespace osu.Game.Online.Notifications public Action>? NewMessages; public Action? PresenceReceived; - private readonly IAPIProvider api; + protected readonly IAPIProvider API; private bool enableChat; private long lastMessageId; protected NotificationsClient(IAPIProvider api) { - this.api = api; + API = api; } public bool EnableChat @@ -54,7 +54,7 @@ namespace osu.Game.Online.Notifications protected virtual Task StartChatAsync() { - api.Queue(CreateFetchMessagesRequest(0)); + API.Queue(CreateFetchMessagesRequest(0)); return Task.CompletedTask; } diff --git a/osu.Game/Online/Notifications/Polling/PollingNotificationsClient.cs b/osu.Game/Online/Notifications/Polling/PollingNotificationsClient.cs index 1c5559fcb4..2f343abf86 100644 --- a/osu.Game/Online/Notifications/Polling/PollingNotificationsClient.cs +++ b/osu.Game/Online/Notifications/Polling/PollingNotificationsClient.cs @@ -12,12 +12,9 @@ namespace osu.Game.Online.Notifications.Polling /// public class PollingNotificationsClient : NotificationsClient { - private readonly IAPIProvider api; - public PollingNotificationsClient(IAPIProvider api) : base(api) { - this.api = api; } public override Task ConnectAsync(CancellationToken cancellationToken) @@ -26,7 +23,7 @@ namespace osu.Game.Online.Notifications.Polling { while (!cancellationToken.IsCancellationRequested) { - await api.PerformAsync(CreateFetchMessagesRequest()); + await API.PerformAsync(CreateFetchMessagesRequest()); await Task.Delay(1000, cancellationToken); } }, cancellationToken); diff --git a/osu.Game/Online/Notifications/Polling/PollingNotificationsClientConnector.cs b/osu.Game/Online/Notifications/Polling/PollingNotificationsClientConnector.cs index 18a31ff061..ff3f30edcd 100644 --- a/osu.Game/Online/Notifications/Polling/PollingNotificationsClientConnector.cs +++ b/osu.Game/Online/Notifications/Polling/PollingNotificationsClientConnector.cs @@ -12,15 +12,12 @@ namespace osu.Game.Online.Notifications.Polling /// public class PollingNotificationsClientConnector : NotificationsClientConnector { - private readonly IAPIProvider api; - public PollingNotificationsClientConnector(IAPIProvider api) : base(api) { - this.api = api; } protected override Task BuildNotificationClientAsync(CancellationToken cancellationToken) - => Task.FromResult((NotificationsClient)new PollingNotificationsClient(api)); + => Task.FromResult((NotificationsClient)new PollingNotificationsClient(API)); } } diff --git a/osu.Game/Online/SocketClientConnector.cs b/osu.Game/Online/SocketClientConnector.cs index c6d5601c1f..abf19fbba3 100644 --- a/osu.Game/Online/SocketClientConnector.cs +++ b/osu.Game/Online/SocketClientConnector.cs @@ -23,18 +23,21 @@ namespace osu.Game.Online /// public SocketClient? CurrentConnection { get; private set; } + protected readonly IAPIProvider API; + + private readonly IBindable apiState = new Bindable(); private readonly Bindable isConnected = new Bindable(); private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1); private CancellationTokenSource connectCancelSource = new CancellationTokenSource(); - private readonly IBindable apiState = new Bindable(); - /// /// Constructs a new . /// /// An API provider used to react to connection state changes. protected SocketClientConnector(IAPIProvider api) { + API = api; + apiState.BindTo(api.State); apiState.BindValueChanged(_ => Task.Run(connectIfPossible), true); }