Creating a Network Session

The Complete Sample

The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download NetworkingSample.zip.

A network session is made up of players in a game and an optional set of attributes to aid in describing the type of session that is being created. Attributes used to describe an available multiplayer session are known as session properties. These session properties, which you create, may contain such details as identifying a game as either a timed session or a capture-the-flag session.

Note

Creating a NetworkSessionProperties collection to describe your session is optional. You can also pass in null when creating a new network session.

The NetworkSessionProperties supports up to eight integer values that describe the session. Because these values are integers, we create a set of enums in this example to describe them and their placement in NetworkSessionProperties.

enum SessionProperty{ GameMode, SkillLevel, ScoreToWin }

enum GameMode{ Practice, Timed, CaptureTheFlag }

enum SkillLevel{ Beginner, Intermediate, Advanced }

Conducting a Network Session

To describe session properties

  1. Create a new NetworkSessionProperties.

  2. Set the values to the custom session properties.

    NetworkSessionProperties sessionProperties = 
        new NetworkSessionProperties();
    
    sessionProperties[(int)SessionProperty.GameMode] 
        = (int)GameMode.Practice; 
    sessionProperties[(int)SessionProperty.SkillLevel] 
        = (int)SkillLevel.Beginner; 
    sessionProperties[(int)SessionProperty.ScoreToWin]
        = 100;
    

You can create many different types of sessions, including a local session that is used for split-screen gaming and requires no network traffic. Another is a system link session, which connects multiple gaming machines over a local subnet. Finally, there is an Xbox LIVE multiplayer session that occurs on the Internet. In this example, we will create a system link game.

Note

The gaming machine that creates the session is called the host. It owns the multiplayer session. The host does not imply a game server or authority. There are no implied network topologies such as client-server, peer-to-peer, or hybrid. The network topology is determined by your implementation of your game.

NetworkSession session;

    int maximumGamers = 8;  
    int privateGamerSlots = 2;
    int maximumLocalPlayers = 1;

    // Create the session
    session = NetworkSession.Create( 
        NetworkSessionType.SystemLink,
        maximumLocalPlayers, maximumGamers, privateGamerSlots, 
        sessionProperties );

To enable host migration and join-in-progress functionality

To subscribe to session events

  • Subscribe to any session events in which your game has an interest.

    Multiplayer sessions have events that occur when the game transitions from lobby to gameplay (GameStarted and GameEnded, respectively), when players join and leave (GamerJoined and GamerLeft, respectively), when the host changes (HostChanged), and when the session ends (SessionEnded).

    session.GamerJoined += 
        new EventHandler<GamerJoinedEventArgs>(session_GamerJoined);
    session.GamerLeft += 
        new EventHandler<GamerLeftEventArgs>(session_GamerLeft);
    session.GameStarted += 
        new EventHandler<GameStartedEventArgs>(session_GameStarted);
    session.GameEnded += 
        new EventHandler<GameEndedEventArgs>(session_GameEnded);
    session.SessionEnded += 
        new EventHandler<NetworkSessionEndedEventArgs>(
            session_SessionEnded);
    

To end the session

  • If you are a host purposely ending the session , and the conditions for ending the session have been met, call NetworkSession.EndGame.

    In this example, when the host presses the ESC key or Back button indicating that the player wants to exit the game, the host checks to see if players remain in the game. If not, the host ends the session.

    // Check for exit.
    if ( IsButtonPressed(GamePadButton.B ) || 
        IsButtonPressed( GamePadButton.Back ) )
    {
        if (session != null)
        {
            if (session.AllGamers.Count == 1)
            {
                session.EndGame();
                session.Update();
            }
        }
    }
    

    The end of a game is controlled by the NetworkSession.EndGame method. At the end of a game, you might choose to move back to the lobby or to end the session. Players in the game can subscribe to the NetworkSession.GameEnded and NetworkSession.SessionEnded events.

See Also

Concepts

Network Session Management
Setting up your Firewall

Tasks

Finding and Joining a Network Session