Example #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }
            if (InputManager.InputManager.KeyJustPressed(Keys.Escape))
            {
                LocalClient.Disconnect();
            }

            // TODO: Add your update logic here
            InputManager.InputManager.Update(Keyboard.GetState(), Mouse.GetState(), IsActive);
            TimeManager.Update(gameTime);
            BackgroundWall.Update();

            if (Program.Hosting)
            {
                Server.Update();
            }

            LocalClient.Update();
            PlatformWorld.Update();

            base.Update(gameTime);
        }
        public void ConnectAndDisconnectWithClientAtServer()
        {
            using (var server = StartLocalServer())
            using (var client = new LocalClient())
            {
                Assert.AreEqual(0, server.ConnectedClientsCount);

                ConnectClientToServerAndCheckState(client, server);
                Assert.AreEqual(1, server.ConnectedClientsCount);

                client.Disconnect();
                Assert.IsFalse(client.IsConnected);
                Assert.AreEqual(0, server.ConnectedClientsCount);
            }
        }
Example #3
0
        public Game1()
        {
            _graphics             = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            Exiting += delegate
            {
                if (Program.Hosting)
                {
                    Server.Stop();
                }

                LocalClient.Disconnect();
            };
        }
        /// <summary>
        /// This shuts down the server and disconnects all clients.
        /// </summary>
        public void Disconnect()
        {
            if (LocalClient != null)
            {
                OnStopHost.Invoke();
                LocalClient.Disconnect();
            }

            // make a copy,  during disconnect, it is possible that connections
            // are modified, so it throws
            // System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
            var connectionscopy = new HashSet <INetworkConnection>(connections);

            foreach (INetworkConnection conn in connectionscopy)
            {
                conn.Disconnect();
            }
            if (Transport != null)
            {
                Transport.Disconnect();
            }
        }
Example #5
0
        /// <summary>
        /// This shuts down the server and disconnects all clients.
        /// <para>If In host mode, this will also stop the local client</para>
        /// </summary>
        public void Stop()
        {
            if (LocalClient != null)
            {
                _onStopHost?.Invoke();
                LocalClient.Disconnect();
            }

            // make a copy,  during disconnect, it is possible that connections
            // are modified, so it throws
            // System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
            var playersCopy = new HashSet <INetworkPlayer>(Players);

            foreach (INetworkPlayer player in playersCopy)
            {
                player.Connection?.Disconnect();
            }
            if (Transport != null)
            {
                Transport.Disconnect();
            }
        }
Example #6
0
        /// <summary>
        /// This shuts down the server and disconnects all clients.
        /// <para>If In host mode, this will also stop the local client</para>
        /// </summary>
        public void Stop()
        {
            if (!Active)
            {
                logger.LogWarning("Can't stop server because it is not active");
                return;
            }

            if (LocalClient != null)
            {
                _onStopHost?.Invoke();
                LocalClient.Disconnect();
            }

            // just clear list, connections will be disconnected when peer is closed
            connections.Clear();
            LocalPlayer = null;

            Cleanup();

            // remove listen when server is stopped so that we can cleanup correctly
            Application.quitting -= Stop;
        }
 private void CommandRecieved(object sender, CommandEventArgs e)
 {
     if (e.Command.CommandType == CommandType.UserDataInform)
     {
         client.Wins   = int.Parse(e.Command.Data.Split(':')[0]);
         client.Losses = int.Parse(e.Command.Data.Split(':')[1]);
     }
     if (e.Command.CommandType == CommandType.UsernameRequest)
     {
         if (e.Command.Data.ToLower() == "false")
         {
             client.SignOut();
             MessageBox.Show(i18n.GetText("userNameInUse"), i18n.GetText("invalidUsernameTitle"), MessageBoxButtons.OK);
             client.Disconnect();
         }
         else if (e.Command.Data.ToLower() == "true")
         {
             client.CommandRecieved -= CommandRecieved;
             if (InvokeRequired)
             {
                 BeginInvoke(new MethodInvoker(delegate
                 {
                     chat = new ChatForm(ref client, this);
                     chat.Show();
                     Hide();
                 }));
             }
             else
             {
                 chat = new ChatForm(ref client, this);
                 chat.Show();
                 Hide();
             }
         }
     }
 }
 private void ChatForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     client.SignOut();
     Thread.Sleep(1000);             //Delay ensures message is completely sent before exiting program
     client.Disconnect();
 }