/// <summary> /// Adds a reference to the room instance. /// </summary> /// <param name="ownerPeer"> /// The peer that holds this reference. /// </param> /// <returns> /// a new <see cref="RoomReference"/> /// </returns> public RoomReference AddReference(PeerBase ownerPeer) { var reference = new RoomReference(this.roomFactory, this.Room, ownerPeer); this.references.Add(reference.Id, reference); if (log.IsDebugEnabled) { log.DebugFormat( "Created room instance reference: roomName={0}, referenceCount={1}", this.Room.Name, this.ReferenceCount); } if (this.logQueue.Log.IsDebugEnabled) { this.logQueue.Add( new LogEntry( "AddReference", string.Format( "RoomName={0}, ReferenceCount={1}, OwnerPeer={2}", this.Room.Name, this.ReferenceCount, ownerPeer))); } return(reference); }
/// <summary> /// Releases a room reference. /// The related room instance will be removed from the cache if /// no more references to the room exists. /// </summary> /// <param name="roomReference"> /// The room reference to relaease. /// </param> public void ReleaseRoomReference(RoomReference roomReference) { Room room; lock (this.SyncRoot) { RoomInstance roomInstance; if (!this.RoomInstances.TryGetValue(roomReference.Room.Name, out roomInstance)) { return; } roomInstance.ReleaseReference(roomReference); // if there are still references to the room left // the room stays into the cache if (roomInstance.ReferenceCount > 0) { return; } if (log.IsDebugEnabled) { log.DebugFormat("Room has no references. Tring to remove it. room:{0}", roomReference.Room.Name); } // ask the room implementation if the room should be // removed automaticly from the cache var shouldRemoveRoom = roomInstance.Room.BeforeRemoveFromCache(); if (shouldRemoveRoom == false) { if (log.IsDebugEnabled) { log.DebugFormat("Room BeforeRemoveFromCache returned 'false'. Removing stopped. room:{0}", roomReference.Room.Name); } return; } this.RoomInstances.Remove(roomInstance.Room.Name); room = roomInstance.Room; } if (room == null) { // the room hast not been removed from the cache return; } if (log.IsDebugEnabled) { log.DebugFormat("Removed room instance: roomId={0}", room.Name); } this.OnRoomRemoved(room); room.Release(); }
/// <summary> /// Tries to get room reference for a room with the specified id. /// </summary> /// <param name="roomId"> /// The room id. /// </param> /// <param name="ownerPeer"> /// The peer that holds this reference. /// </param> /// <param name="roomReference"> /// When this method returns true, contains a new <see cref="RoomReference"/> for the room /// with the specified room id; otherwise, set to null. /// </param> /// <returns> /// True if the cache contains a room with the specified room id; otherwise, false. /// </returns> public bool TryGetRoomReference(string roomId, PeerBase ownerPeer, out RoomReference roomReference) { lock (this.SyncRoot) { RoomInstance roomInstance; if (!this.RoomInstances.TryGetValue(roomId, out roomInstance)) { roomReference = null; return(false); } roomReference = roomInstance.AddReference(ownerPeer); return(true); } }
/// <summary> /// Tries to create a new room. /// </summary> /// <param name="roomName"> /// The room id. /// </param> /// <param name="ownerPeer"> /// The peer that holds this reference. /// </param> /// <param name="roomReference"> /// When this method returns true, contains a new <see cref="RoomReference"/> for the room /// with the specified room id; otherwise, set to null. /// </param> /// <param name="args"> /// Optionally arguments used for room creation. /// </param> /// <returns> /// False if the cache contains a room with the specified room id; otherwise, true. /// </returns> public bool TryCreateRoom(string roomName, PeerBase ownerPeer, out RoomReference roomReference, params object[] args) { lock (this.SyncRoot) { if (this.RoomInstances.ContainsKey(roomName)) { roomReference = null; return(false); } Room room = this.CreateRoom(roomName, args); var roomInstance = new RoomInstance(this, room); this.RoomInstances.Add(roomName, roomInstance); roomReference = roomInstance.AddReference(ownerPeer); return(true); } }
/// <summary> /// Releases a reference from this instance. /// </summary> /// <param name="reference"> /// The room reference. /// </param> public void ReleaseReference(RoomReference reference) { this.references.Remove(reference.Id); if (log.IsDebugEnabled) { log.DebugFormat( "Removed room instance reference: roomName={0}, referenceCount={1}", this.Room.Name, this.ReferenceCount); } if (this.logQueue.Log.IsDebugEnabled) { this.logQueue.Add( new LogEntry( "ReleaseReference", string.Format( "RoomName={0}, ReferenceCount={1}, OwnerPeer={2}", this.Room.Name, this.ReferenceCount, reference.OwnerPeer))); } }
protected override bool TryGetRoomReference(string gameId, out RoomReference roomReference) { return this.application.GameCache.TryGetRoomReference(gameId, this, out roomReference); }
protected override bool TryCreateRoom(string gameId, out RoomReference roomReference, params object[] args) { return this.application.GameCache.TryCreateRoom(gameId, this, out roomReference, args); }
/// <summary> /// Adds a reference to the room instance. /// </summary> /// <param name="ownerPeer"> /// The peer that holds this reference. /// </param> /// <returns> /// a new <see cref="RoomReference"/> /// </returns> public RoomReference AddReference(PeerBase ownerPeer) { var reference = new RoomReference(this.roomFactory, this.Room, ownerPeer); this.references.Add(reference.Id, reference); if (log.IsDebugEnabled) { log.DebugFormat( "Created room instance reference: roomName={0}, referenceCount={1}", this.Room.Name, this.ReferenceCount); } if (this.logQueue.Log.IsDebugEnabled) { this.logQueue.Add( new LogEntry( "AddReference", string.Format( "RoomName={0}, ReferenceCount={1}, OwnerPeer={2}", this.Room.Name, this.ReferenceCount, ownerPeer))); } return reference; }
/// <summary> /// Tries to get room reference for a room with the specified id. /// </summary> /// <param name="roomId"> /// The room id. /// </param> /// <param name="ownerPeer"> /// The peer that holds this reference. /// </param> /// <param name="roomReference"> /// When this method returns true, contains a new <see cref="RoomReference"/> for the room /// with the specified room id; otherwise, set to null. /// </param> /// <returns> /// True if the cache contains a room with the specified room id; otherwise, false. /// </returns> public bool TryGetRoomReference(string roomId, PeerBase ownerPeer, out RoomReference roomReference) { lock (this.SyncRoot) { RoomInstance roomInstance; if (!this.RoomInstances.TryGetValue(roomId, out roomInstance)) { roomReference = null; return false; } roomReference = roomInstance.AddReference(ownerPeer); return true; } }
/// <summary> /// Tries to create a new room. /// </summary> /// <param name="roomName"> /// The room id. /// </param> /// <param name="ownerPeer"> /// The peer that holds this reference. /// </param> /// <param name="roomReference"> /// When this method returns true, contains a new <see cref="RoomReference"/> for the room /// with the specified room id; otherwise, set to null. /// </param> /// <param name="args"> /// Optionally arguments used for room creation. /// </param> /// <returns> /// False if the cache contains a room with the specified room id; otherwise, true. /// </returns> public bool TryCreateRoom(string roomName, PeerBase ownerPeer, out RoomReference roomReference, params object[] args) { lock (this.SyncRoot) { if (this.RoomInstances.ContainsKey(roomName)) { roomReference = null; return false; } Room room = this.CreateRoom(roomName, args); var roomInstance = new RoomInstance(this, room); this.RoomInstances.Add(roomName, roomInstance); roomReference = roomInstance.AddReference(ownerPeer); return true; } }