1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 13:23:22 +08:00

Merge pull request #11056 from smoogipoo/fix-spectator-test-failures

Fix spectator connecting not being thread-safe
This commit is contained in:
Dean Herbert 2020-12-02 19:50:15 +09:00 committed by GitHub
commit 33b88e09cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,6 +36,8 @@ namespace osu.Game.Online.Spectator
private readonly List<int> watchingUsers = new List<int>();
private readonly object userLock = new object();
public IBindableList<int> PlayingUsers => playingUsers;
private readonly BindableList<int> playingUsers = new BindableList<int>();
@ -144,12 +146,19 @@ namespace osu.Game.Online.Spectator
await connection.StartAsync();
Logger.Log("Spectator client connected!", LoggingTarget.Network);
// get all the users that were previously being watched
int[] users;
lock (userLock)
{
users = watchingUsers.ToArray();
watchingUsers.Clear();
}
// success
isConnected = true;
// resubscribe to watched users
var users = watchingUsers.ToArray();
watchingUsers.Clear();
foreach (var userId in users)
WatchUser(userId);
@ -238,21 +247,29 @@ namespace osu.Game.Online.Spectator
public virtual void WatchUser(int userId)
{
if (watchingUsers.Contains(userId))
return;
lock (userLock)
{
if (watchingUsers.Contains(userId))
return;
watchingUsers.Add(userId);
watchingUsers.Add(userId);
if (!isConnected) return;
if (!isConnected)
return;
}
connection.SendAsync(nameof(ISpectatorServer.StartWatchingUser), userId);
}
public void StopWatchingUser(int userId)
{
watchingUsers.Remove(userId);
lock (userLock)
{
watchingUsers.Remove(userId);
if (!isConnected) return;
if (!isConnected)
return;
}
connection.SendAsync(nameof(ISpectatorServer.EndWatchingUser), userId);
}