mirror of
https://github.com/ppy/osu.git
synced 2025-03-28 01:37:46 +08:00
Fix spectator not being thread-safe
This commit is contained in:
parent
297283491a
commit
d5dccbc3d7
@ -36,6 +36,8 @@ namespace osu.Game.Online.Spectator
|
|||||||
|
|
||||||
private readonly List<int> watchingUsers = new List<int>();
|
private readonly List<int> watchingUsers = new List<int>();
|
||||||
|
|
||||||
|
private readonly object userLock = new object();
|
||||||
|
|
||||||
public IBindableList<int> PlayingUsers => playingUsers;
|
public IBindableList<int> PlayingUsers => playingUsers;
|
||||||
|
|
||||||
private readonly BindableList<int> playingUsers = new BindableList<int>();
|
private readonly BindableList<int> playingUsers = new BindableList<int>();
|
||||||
@ -144,12 +146,19 @@ namespace osu.Game.Online.Spectator
|
|||||||
await connection.StartAsync();
|
await connection.StartAsync();
|
||||||
Logger.Log("Spectator client connected!", LoggingTarget.Network);
|
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
|
// success
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
|
|
||||||
// resubscribe to watched users
|
// resubscribe to watched users
|
||||||
var users = watchingUsers.ToArray();
|
|
||||||
watchingUsers.Clear();
|
|
||||||
foreach (var userId in users)
|
foreach (var userId in users)
|
||||||
WatchUser(userId);
|
WatchUser(userId);
|
||||||
|
|
||||||
@ -238,21 +247,29 @@ namespace osu.Game.Online.Spectator
|
|||||||
|
|
||||||
public virtual void WatchUser(int userId)
|
public virtual void WatchUser(int userId)
|
||||||
{
|
{
|
||||||
if (watchingUsers.Contains(userId))
|
lock (userLock)
|
||||||
return;
|
{
|
||||||
|
if (watchingUsers.Contains(userId))
|
||||||
|
return;
|
||||||
|
|
||||||
watchingUsers.Add(userId);
|
watchingUsers.Add(userId);
|
||||||
|
|
||||||
if (!isConnected) return;
|
if (!isConnected)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
connection.SendAsync(nameof(ISpectatorServer.StartWatchingUser), userId);
|
connection.SendAsync(nameof(ISpectatorServer.StartWatchingUser), userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopWatchingUser(int 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);
|
connection.SendAsync(nameof(ISpectatorServer.EndWatchingUser), userId);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user