// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable enable using System; using System.Collections.Generic; using System.Threading; using Newtonsoft.Json; using osu.Framework.Allocation; namespace osu.Game.Online.RealtimeMultiplayer { /// /// A multiplayer room. /// [Serializable] public class MultiplayerRoom { /// /// The ID of the room, used for database persistence. /// public readonly long RoomID; /// /// The current state of the room (ie. whether it is in progress or otherwise). /// public MultiplayerRoomState State { get; set; } /// /// All currently enforced game settings for this room. /// public MultiplayerRoomSettings Settings { get; set; } = new MultiplayerRoomSettings(); /// /// All users currently in this room. /// public List Users { get; set; } = new List(); /// /// The host of this room, in control of changing room settings. /// public MultiplayerRoomUser? Host { get; set; } private object writeLock = new object(); [JsonConstructor] public MultiplayerRoom(in long roomId) { RoomID = roomId; } private object updateLock = new object(); private ManualResetEventSlim freeForWrite = new ManualResetEventSlim(true); /// /// Request a lock on this room to perform a thread-safe update. /// public IDisposable LockForUpdate() { // ReSharper disable once InconsistentlySynchronizedField freeForWrite.Wait(); lock (updateLock) { freeForWrite.Wait(); freeForWrite.Reset(); return new ValueInvokeOnDisposal(this, r => freeForWrite.Set()); } } public override string ToString() => $"RoomID:{RoomID} Host:{Host?.UserID} Users:{Users.Count} State:{State} Settings: [{Settings}]"; } }