示例#1
0
        public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e)
        {
            if (e is NetworkBackgammonGameRoomEvent)
            {
                switch (((NetworkBackgammonGameRoomEvent)e).EventType)
                {
                case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerDisconnected:
                case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected:
                {
                    if (InvokeRequired)
                    {
                        // In case the caller has called this routine on a different thread
                        BeginInvoke(new OnNotificationDelegate(UpdateGameRoomList));
                    }
                    else
                    {
                        UpdateGameRoomList();
                    }
                }
                break;
                }
            }

            //else if ( ((NetworkBackgammonPlayer)sender).PlayerName.CompareTo(NetworkBackgammonClient.Instance.Player.PlayerName) == 0 )
            {
                if (e is NetworkBackgammonChallengeResponseEvent)
                {
                    NetworkBackgammonChallengeResponseEvent challengeRespEvent = ((NetworkBackgammonChallengeResponseEvent)e);

                    bool challengeAccepted = challengeRespEvent.ChallengeAccepted;

                    if (NetworkBackgammonClient.Instance.Player.PlayerName.CompareTo(challengeRespEvent.ChallengingPlayer) == 0)
                    {
                        lock (gameChallengeMutex)
                        {
                            if (gameChallengeThreadExchangeData != null)
                            {
                                gameChallengeThreadExchangeData.ChallengeResult =
                                    challengeAccepted ?
                                    GameChallengeDataContainer.ChallengeResultType.CHALLENGE_ACCEPTED :
                                    GameChallengeDataContainer.ChallengeResultType.CHALLENGE_REJECTED;
                            }
                        }

                        if (InvokeRequired)
                        {
                            BeginInvoke(new OnChallengeResponseDelegate(OnChallengeResponse));
                        }
                        else
                        {
                            OnChallengeResponse();
                        }
                    }
                }
            }
        }
        public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e)
        {
            if (sender is NetworkBackgammonPlayer)
            {
                NetworkBackgammonPlayer player = (NetworkBackgammonPlayer)sender;

                if (e is NetworkBackgammonChallengeResponseEvent)
                {
                    NetworkBackgammonChallengeResponseEvent challengeResponseEvent = (NetworkBackgammonChallengeResponseEvent)e;

                    if (challengeSyncList.Keys.Contains(player))
                    {
                        if (challengeSyncList[player] != null)
                        {
                            challengeSyncList[player].ChallengeAccepted = challengeResponseEvent.ChallengeAccepted;

                            try
                            {
                                challengeSyncList[player].ChallengeSemaphore.Release();
                            }
                            catch (SemaphoreFullException ex)
                            {
                                // TODO: If this exception occurs calling Release too many times...
                            }
                        }
                    }
                }
                else if (e is NetworkBackgammonChatEvent)
                {
                    // Pass the message through to the listeners
                    Broadcast((NetworkBackgammonChatEvent)e);
                }
            }
            else if (sender is NetworkBackgammonGameSession)
            {
                NetworkBackgammonGameSession gameSession = (NetworkBackgammonGameSession)sender;

                if (e is NetworkBackgammonGameSessionEvent)
                {
                    NetworkBackgammonGameSessionEvent gameSessionEvent = (NetworkBackgammonGameSessionEvent)e;

                    if (gameSessionEvent.EventType == NetworkBackgammonGameSessionEvent.GameSessionEventType.GameFinished)
                    {
                        bool newQueueItemAdd = true;

                        NetworkBackgammonEventQueueElement newQueueItem = new NetworkBackgammonEventQueueElement(e, sender);

                        lock (newQueueItem)
                        {
                            /*
                             * if (gameSessionsEventQueue.Count > 0)
                             * {
                             *  NetworkBackgammonEventQueueElement lastQueueItem = gameSessionsEventQueue.Last();
                             *  // Avoid adding the same event (from the same sender) twice
                             *  // Reason: Game Room listens to events from all players, i.e. also
                             *  // both players that are in one Game Session. Thus, all events broadcasted
                             *  // by the Game Session arrive here (at the Game Room) twice
                             *  if (gameSessionsEventQueue.Last().Notifier == sender &&
                             *      gameSessionsEventQueue.Last().Event == e)
                             *  {
                             *      newQueueItemAdd = false;
                             *  }
                             * }
                             */

                            if (newQueueItemAdd)
                            {
                                gameSessionsEventQueue.Enqueue(new NetworkBackgammonEventQueueElement(e, sender));

                                gameSessionsSemaphore.Release();
                            }
                        }
                    }
                }
            }
        }