예제 #1
0
        public void Host_ReceiveDataTest()
        {
            GameMessage inMessage = new GameMessage { Content = _messageContent };
            GameMessage outMessage = new GameMessage();
            Host host = new Host();
            Action<IPAddress, IConfigurable> theAction = (notImportant, receivedObject) => outMessage = (GameMessage)receivedObject;
            host.ObjectReceived += theAction;
            host.ReceiveData(_someAddress, _converter.ObjectToString(inMessage));
            Assert.That(outMessage.Content.Equals(inMessage.Content));
            host.ObjectReceived -= theAction;
            host.StopMonitoringDataFromSender(_someAddress);

            bool malformedEventNotRaised = true;
            Action<IPAddress, string> shouldNotExecute = (notImportant, whoCares) => malformedEventNotRaised = false;
            object outObject = new object();
            theAction = (notImportant, receivedObject) => outObject = receivedObject;
            host.ObjectReceived += theAction;
            host.ReceiveData(_someAddress, _validObjectString);
            Assert.That(malformedEventNotRaised);
            Assert.That(outObject == null);
            host.ObjectReceived -= theAction;
            host.StopMonitoringDataFromSender(_someAddress);

            string mixedString = _validObjectString + _validObjectString + _malformedObjectString + _otherMessageContent;
            List<object> validObjects = new List<object>();
            Action<IPAddress, IConfigurable> onValidReceived = (notImportant, receivedObject) => validObjects.Add(receivedObject);
            host.ObjectReceived += onValidReceived;
            host.ReceiveData(_someAddress, mixedString);
            Assert.That(validObjects[0] == null);
            Assert.That(validObjects[1] == null);
        }
예제 #2
0
 public void ConnectToHostAt(IPAddress address)
 {
     if (_host != null)
     {
         _host.ShutDown();
         _host.ObjectReceived -= HandleObject;
         _host = null;
     }
     if (_client != null)
     {
         _client.ShutDown();
         _client.Disconnected -= OnDisconnected;
         _client.ObjectReceived -= HandleObject;
     }
     _client = new Client { ServerIPAddress = address };
     /* Client will only have non-null values for ServerIPAddress
      * if it is connected.
      */
     if (_client.ServerIPAddress != null)
     {
         SendPlayerNameToHost();
         ChangeToScreen(Screen.MultiplayerPreGameClient);
         _client.Disconnected += OnDisconnected;
         _client.ObjectReceived += HandleObject;
     }
 }
예제 #3
0
 public void ChangeToScreen(Screen whichScreen)
 {
     if (_currentScreen == Screen.InGame && whichScreen != Screen.InGame)
     {
         //call OnGameOver
         OnGameOver();
     }
     _currentScreen = whichScreen;
     switch (whichScreen)
     {
         case Screen.MainMenu:
         {
             //Main Menu is always initialized before this point
             PlayerManager.Players.Clear();
             LevelNumber = 100;
             SetupLevel();
             _screenInterface = _mainMenu;
             // Clean up _host if we have left MultiplayerPreGameServer
             if (_host != null)
             {
                 if (!_host.IsShutDown)
                 {
                     _host.ShutDown();
                 }
                 _host.ObjectReceived -= HandleObject;
                 _host = null;
                 _clientAddressesAndMonikers.Clear();
             }
             // Clean up _client if we have left MultiplayerPreGameServer
             if (_client != null)
             {
                 if (!_client.IsShutDown)
                 {
                     _client.ShutDown();
                 }
                 _client.ObjectReceived -= HandleObject;
                 _client = null;
             }
             break;
         }
         case Screen.MultiplayerPreGameClient:
         {
             if (_multiplayerGameSetup == null)
             {
                 string reason;
                 _multiplayerGameSetup = new MultiplayerGameSetup();
                 if (!_multiplayerGameSetup.Initialize(this, out reason))
                 {
                     MessageBox.Show("Error in loading Multiplayer PreGame Screen.  Reason: " + reason);
                     ExitGame();
                 }
             }
             _screenInterface = _multiplayerGameSetup;
             break;
         }
         case Screen.MultiplayerPreGameServer:
         {
             if (_multiplayerGameSetup == null)
             {
                 string reason;
                 _multiplayerGameSetup = new MultiplayerGameSetup();
                 if (!_multiplayerGameSetup.Initialize(this, out reason))
                 {
                     MessageBox.Show("Error in loading Multiplayer PreGame Screen.  Reason: " + reason);
                     ExitGame();
                 }
             }
             if (_client != null)
             {
                 _client.ShutDown();
                 _client = null;
             }
             _host = new Host { CurrentlyAcceptingPlayers = true };
             _host.ObjectReceived += HandleObject;
             _screenInterface = _multiplayerGameSetup;
             PlayerList.Players = new [] { _mainMenu.PlayerName };
             NewPlayerListUpdate = true;
             break;
         }
         case Screen.InGame:
         {
             if (_inGame == null)
             {
                 string reason;
                 _inGame = new InGame();
                 if (!_inGame.Initialize(this, out reason))
                 {
                     MessageBox.Show("Error in loading In-Game Screen.  Reason: " + reason);
                     ExitGame();
                 }
             }
             ObjectManager.Clear();
             _screenInterface = _inGame;
             if (_host != null || _client != null)
             {
                 if (_host != null)
                 {
                     OnPushAsteroids(null, null);
                     _1000msPushDataTimer.Elapsed += OnPushAsteroids;
                     _1000msPushDataTimer.Start();
                 }
             }
             break;
         }
         case Screen.Upgrade:
         {
             if (_upgradeAndWaitScreen == null)
             {
                 string reason;
                 _upgradeAndWaitScreen = new UpgradeAndWaitScreen();
                 if (!_upgradeAndWaitScreen.Initialize(this, out reason))
                 {
                     MessageBox.Show("Error in loading Upgrade Screen.  Reason: " + reason);
                     ExitGame();
                 }
             }
             lock (_lockDrawObject)
             {
                 _upgradeAndWaitScreen.RefreshLabels();
             }
             _screenInterface = _upgradeAndWaitScreen;
             if (IsHost)
             {
                 if (ShoppingPlayers == null)
                 {
                     ShoppingPlayers = new Dictionary<IPAddress, List<Object>>();
                 }
                 foreach (var item in _clientAddressesAndMonikers)
                 {
                     if (item.Value[ID] != null && PlayerManager.Players[ID].Bank >= MIN_COST_FOR_SHIP)
                     {
                         ShoppingPlayers.Add(item.Key, item.Value);
                     }
                 }
                 if (ShoppingPlayers.Count > 0)
                 {
                     _upgradeAndWaitScreen.DisableTheReadyButton();
                 }
                 else
                 {
                     _upgradeAndWaitScreen.EnableTheReadyButton();
                 }
                 _host.SendObjectTCP(new NetworkMessage { Content = "Change to " + Screen.Upgrade.ToString() + " Screen." });
             }
             else
             {
                 _upgradeAndWaitScreen.EnableTheReadyButton();
             }
             break;
         }
     }
 }