Beispiel #1
0
        /// <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 = null;

            lock (this.SyncRoot)
            {
                RoomInstance roomInstance;
                if (!this.roomInstances.TryGetValue(roomReference.Room.Name, out roomInstance))
                {
                    return;
                }

                roomInstance.ReleaseReference(roomReference);

                if (roomInstance.ReferenceCount <= 0)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Removing room instance: roomId={0}", roomReference.Room.Name);
                    }

                    this.roomInstances.Remove(roomInstance.Room.Name);
                    roomInstance.Room.Dispose();
                    room = roomInstance.Room;
                }
            }

            if (room != null)
            {
                this.OnRoomRemoved(room);
            }
        }
Beispiel #2
0
            /// <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);
            }
Beispiel #3
0
            /// <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);
                }
            }
Beispiel #4
0
            /// <summary>
            /// Adds a reference to the room instance.
            /// </summary>
            /// <returns>
            /// a new <see cref="RoomReference"/>
            /// </returns>
            public RoomReference AddReference()
            {
                var reference = new RoomReference(this.roomFactory, this.Room);

                this.references.Add(reference.Id, reference);

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Created room instance reference: roomName={0}, referenceCount={1}", this.Room.Name,
                                    this.ReferenceCount);
                }

                return(reference);
            }
Beispiel #5
0
        /// <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);
            }
        }
Beispiel #6
0
        /// <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;
                }

                // ask the room implementation if the room should be
                // removed automaticly from the cache
                var shouldRemoveRoom = roomInstance.Room.BeforeRemoveFromCache();
                if (shouldRemoveRoom == false)
                {
                    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);
            }

            room.Dispose();
            this.OnRoomRemoved(room);
        }
Beispiel #7
0
        /// <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);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Tries to create a new room.
        /// </summary>
        /// <param name="roomName">The room id.</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, 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();
                return true;
            }
        }
Beispiel #9
0
            /// <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)));
                }
            }
Beispiel #10
0
        /// <summary>
        ///   Handles the <see cref = "LeaveRequest" /> to leave a <see cref = "LiteGame" />.
        /// </summary>
        /// <param name = "operationRequest">
        ///   The operation request to handle.
        /// </param>
        /// <param name = "sendParameters">
        ///   The send Parameters.
        /// </param>
        protected virtual void HandleLeaveOperation(OperationRequest operationRequest, SendParameters sendParameters)
        {
            // check if the peer have a reference to game
            if (this.RoomReference == null)
            {
                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Received leave operation on peer without a game: peerId={0}", this.ConnectionId);
                }

                return;
            }

            // enqueue the leave operation into game queue.
            this.RoomReference.Room.EnqueueOperation(this, operationRequest, sendParameters);

            // release the reference to the game
            // the game cache will recycle the game instance if no
            // more refrences to the game are left.
            this.RoomReference.Dispose();

            // finally the peers state is set to null to indicate
            // that the peer is not attached to a room anymore.
            this.RoomReference = null;
        }
Beispiel #11
0
        /// <summary>
        ///   Handles the <see cref = "JoinRequest" /> to enter a <see cref = "LiteGame" />.
        ///   This method removes the peer from any previously joined room, finds the room intended for join
        ///   and enqueues the operation for it to handle.
        /// </summary>
        /// <param name = "operationRequest">
        ///   The operation request to handle.
        /// </param>
        /// <param name = "sendParameters">
        ///   The send Parameters.
        /// </param>
        protected virtual void HandleJoinOperation(OperationRequest operationRequest, SendParameters sendParameters)
        {
            // create join operation
            var joinRequest = new JoinRequest(this.Protocol, operationRequest);
            if (this.ValidateOperation(joinRequest, sendParameters) == false)
            {
                return;
            }

            // remove peer from current game
            this.RemovePeerFromCurrentRoom();

            // get a game reference from the game cache
            // the game will be created by the cache if it does not exists already
            RoomReference gameReference = this.GetRoomReference(joinRequest);

            // save the game reference in the peers state
            this.RoomReference = gameReference;

            // finally enqueue the operation into game queue
            gameReference.Room.EnqueueOperation(this, operationRequest, sendParameters);
        }
 /// <summary>
 ///   Initializes a new instance of the <see cref = "LiteLobbyGame" /> class.
 /// </summary>
 /// <param name = "gameName">
 ///   The name of the game.
 /// </param>
 /// <param name = "lobbyName">
 ///   The name of the lobby for the game.
 /// </param>
 public LiteLobbyGame(string gameName, string lobbyName)
     : base(gameName)
 {
     // get the reference to the lobby
     this.lobbyReference = LiteLobbyRoomCache.Instance.GetRoomReference(lobbyName, null);
 }
 protected virtual bool TryGetRoomReference(string gameId, out RoomReference roomReference)
 {
     return GameCache.Instance.TryGetRoomReference(gameId, this, out roomReference);
 }
            /// <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)));
                }
            }
            /// <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;
            }
        }
Beispiel #17
0
 protected override bool TryCreateRoom(string gameId, out RoomReference roomReference)
 {
     return GameCache.Instance.TryCreateRoom(gameId, this, out roomReference);
 }
Beispiel #18
0
        /// <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 = null;
            lock (this.syncRoot)
            {
                RoomInstance roomInstance;
                if (!this.roomInstances.TryGetValue(roomReference.Room.Name, out roomInstance))
                {
                    return;
                }

                roomInstance.ReleaseReference(roomReference);

                if (roomInstance.ReferenceCount <= 0)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Removing room instance: roomId={0}", roomReference.Room.Name);
                    }

                    this.roomInstances.Remove(roomInstance.Room.Name);
                    roomInstance.Room.Dispose();
                    room = roomInstance.Room;
                }
            }

            if (room != null)
            {
                this.OnRoomRemoved(room);
            }
        }
Beispiel #19
0
            /// <summary>
            /// Adds a reference to the room instance.
            /// </summary>
            /// <returns>
            /// a new <see cref="RoomReference"/>
            /// </returns>
            public RoomReference AddReference()
            {
                var reference = new RoomReference(this.roomFactory, this.Room);
                this.references.Add(reference.Id, reference);

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Created room instance reference: roomName={0}, referenceCount={1}", this.Room.Name,
                                    this.ReferenceCount);
                }

                return reference;
            }
Beispiel #20
0
        /// <summary>
        ///   Called when client disconnects.
        ///   Ensures that disconnected players leave the game <see cref = "Room" />.
        ///   The player is not removed immediately but a message is sent to the room. This avoids
        ///   threading issues by making sure the player remove is not done concurrently with operations.
        /// </summary>
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("OnDisconnect: conId={0}, reason={1}, reasonDetail={2}", this.ConnectionId, reasonCode, reasonDetail);
            }

            if (this.RoomReference == null)
            {
                return;
            }

            var message = new RoomMessage((byte)GameMessageCodes.RemovePeerFromGame, this);
            this.RoomReference.Room.EnqueueMessage(message);
            this.RoomReference.Dispose();
            this.RoomReference = null;
        }
        /// <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;
                }

                // ask the room implementation if the room should be 
                // removed automaticly from the cache
                var shouldRemoveRoom = roomInstance.Room.BeforeRemoveFromCache();
                if (shouldRemoveRoom == false)
                {
                    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);
            }

            room.Dispose();
            this.OnRoomRemoved(room);
        }
Beispiel #22
0
        /// <summary>
        ///   Checks if the the state of peer is set to a reference of a room.
        ///   If a room refrence is present the peer will be removed from the related room and the reference will be disposed. 
        ///   Disposing the reference allows the associated room factory to remove the room instance if no more references to the room exists.
        /// </summary>
        protected virtual void RemovePeerFromCurrentRoom()
        {
            // check if the peer already joined another game
            if (this.RoomReference != null)
            {
                // remove peer from his current game.
                var message = new RoomMessage((byte)GameMessageCodes.RemovePeerFromGame, this);
                this.RoomReference.Room.EnqueueMessage(message);

                // release room reference
                this.RoomReference.Dispose();
                this.RoomReference = null;
            }
        }
Beispiel #23
0
            /// <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);
                }
            }