NetworkHelper.FindSession C# (CSharp) Method

FindSession() public method

public FindSession ( ) : void
return void
    public void FindSession()
    {
        // All sessions found
        AvailableNetworkSessionCollection availableSessions;
        // The session we'll join
        AvailableNetworkSession availableSession = null;

        availableSessions = NetworkSession.Find(NetworkSessionType.SystemLink, maximumLocalPlayers, null);

        // Get a session with available gamer slots
        foreach (AvailableNetworkSession curSession in availableSessions)
        {
            Console.WriteLine("FindSession() foreach");
            int TotalSessionSlots = curSession.OpenPublicGamerSlots + curSession.OpenPrivateGamerSlots;

            if (TotalSessionSlots > curSession.CurrentGamerCount)
            {
                Console.WriteLine("FindSession() totalsessionslots");
                availableSession = curSession;
            }
        }

        // If session was found, connect to it
        if (availableSession != null)
        {
            Console.WriteLine("findsession() availablesession");
            message = "Found an available session at host " + availableSession.HostGamertag;
            session = NetworkSession.Join(availableSession);
            gameStart = true;
        }
        else
            message = "No sessions found.";
    }

Usage Example

Example #1
0
    protected override void Update(GameTime gameTime)
    {
        networkHelper.Update();
        KeyboardState keyboardState = Keyboard.GetState();

        if (keyboardState.IsKeyDown(Keys.Escape)) // If ESC is pressed
        {
            this.Exit();                          // Exit the game
        }
        if (!networkHelper.gameStart)
        {
            if (keyboardState.IsKeyDown(Keys.F1))
            {
                networkHelper.SignInGamer();
            }

            if (keyboardState.IsKeyDown(Keys.F2))
            {
                networkHelper.CreateSession();
                Host   = true;
                camera = new Camera(this, hostPos);
                Components.Add(camera);         // Add camera to components
                Player2     = new Vector3(100, 100, 100);
                playerModel = new BasicModel(Content.Load <Model>("spaceship"));
            }

            if (keyboardState.IsKeyDown(Keys.F3))
            {
                networkHelper.FindSession();
                Guest  = true;
                camera = new Camera(this, guestPos);
                Components.Add(camera);         // Add camera to components
                Player2     = new Vector3(100, 100, 100);
                playerModel = new BasicModel(Content.Load <Model>("spaceship"));
            }

            if (keyboardState.IsKeyDown(Keys.F4))
            {
                networkHelper.AsyncFindSession();
                Guest  = true;
                camera = new Camera(this, guestPos);
                Components.Add(camera);         // Add camera to components
                Player2     = new Vector3(100, 100, 100);
                playerModel = new BasicModel(Content.Load <Model>("spaceship"));
            }

            if (Mouse.GetState().LeftButton == ButtonState.Pressed && (gameTime.TotalGameTime.TotalSeconds - shotTimer) >= 1)
            {
                shotTimer = gameTime.TotalGameTime.TotalSeconds;
                bulletPos.Add(camera.cameraPosition);
                bulletVel.Add(camera.forward);
                x = true;
            }
            if (bulletPos.Count > 0)
            {
                bulletModel.Update(bulletPos[0]);
            }
            playerModel.Update(Player2);
            jumpPadModel1.Update(jumpPadLocation1);

            if (camera.angle != pAngle)
            {
                Vector3 newgunPosition = gunPosition - camera.cameraPosition;
                newgunPosition = Vector3.Transform(newgunPosition, Matrix.CreateRotationY(pAngle.Y - camera.angle.Y));
                gunPosition    = newgunPosition + camera.cameraPosition;
            }
            if (pAngle.X != camera.angle.X)
            {
                float   dotprod, angle, degree;
                Vector3 tempAngle = gunPosition - camera.cameraPosition;
                tempAngle = Vector3.Transform(tempAngle, Matrix.CreateRotationY(0 - camera.angle.Y));

                dotprod = Vector3.Dot(camera.angle, tempAngle);
                angle   = (float)Math.Acos(dotprod);
                degree  = angle * 180 / MathHelper.Pi;
                Console.WriteLine(degree);

                tempAngle = Vector3.Transform(tempAngle, Matrix.CreateRotationX(pAngle.X - camera.angle.X));
                tempAngle = Vector3.Transform(tempAngle, Matrix.CreateRotationY(camera.angle.Y - 0));
                tempAngle = tempAngle + camera.cameraPosition;
            }

            /*gunPosition = camera.cameraPosition;
             * gunPosition.Z -= 11;
             * gunPosition.X += 6;
             * gunPosition.Y -= 2;*/
            gunPosition.Y = camera.cameraPosition.Y - 2;
            //gunPosition.X = camera.cameraPosition.X + 6;
            //gunPosition.Z = camera.cameraPosition.Z - 11;
            gun.Update(gunPosition);

            pAngle = camera.angle;
        }
        else
        {
            if (Mouse.GetState().LeftButton == ButtonState.Pressed && (gameTime.TotalGameTime.TotalSeconds - shotTimer) >= 1)
            {
                shotTimer = gameTime.TotalGameTime.TotalSeconds;
                bulletPos.Add(camera.cameraPosition);
                bulletVel.Add(camera.forward);
                x = true;
            }

            if (bulletPos.Count > 0)
            {
                bulletModel.Update(bulletPos[0]);
            }

            jumpPadModel1.Update(jumpPadLocation1);

            if (networkHelper.p2hit)
            {
                ++p2kills;
                ++p1deaths;
                if (Host)
                {
                    camera.cameraPosition = hostPos;
                }
                else
                {
                    camera.cameraPosition = guestPos;
                }
            }
        }

        if (networkHelper.SessionState == NetworkSessionState.Playing)
        {
            // Send any key pressed to the remote player
            networkHelper.SendMessage(camera.cameraPosition);

            // Recieve the keys from the remote player
            networkHelper.ReceiveMessage();
            Player2         = networkHelper.Player2;
            playerModel.rot = networkHelper.rot;
            playerModel.Update(Player2);
        }
        bulletMove(gameTime);
        BulletCollision();                                        //check collision
        PlayerCollision();
        if (camera.gameStart != networkHelper.gameStart)
        {
            camera.gameStart = networkHelper.gameStart;
        }
        base.Update(gameTime);
    }