Microsoft DirectX 9.0 SDK Update (Summer 2003) |
Connecting to a Peer-to-Peer Session
Unless you are the session host, you will need to connect your player to the session. To do so, you must have the address of the session host. If your application was connected by a lobby client, you can obtain the host's address by calling IDirectPlay8LobbiedApplication::GetConnectionSettings. You can also obtain the address by enumerating the available hosts. The information returned by the enumeration includes each host's addresses, and a DPN_APPLICATION_DESC structure that describes the associated session.
To ask to join a session, call IDirectPlay8Peer::SetPeerInfo to set your player's name, and then call IDirectPlay8Peer::Connect with the selected host's address to connect to the session.
When a player attempts to join a session, the host receives a DPN_MSGID_INDICATE_CONNECT message. To accept the player into the session, return S_OK. Returning any other value rejects the request. In either case, the player will receive a DPN_MSGID_CONNECT_COMPLETE message that contains your response. If the host accepted the connection, the hResultCode member of the associated structure will be set to S_OK. If not, hResultCode will be set to DPNERR_HOSTREJECTEDCONNECTION.
The host can define a player context value when it receives the DPN_MSGID_INDICATE_CONNECT message, however the player identifier (ID) will not yet be defined. The host can also wait to define a player context value until it receives a DPN_MSGID_CREATE_PLAYER message, which includes the player ID. Ordinary players to not receive a DPN_MSGID_INDICATE_CONNECT message.
Once the new player is connected, each member of the session, including the host, receives a DPN_MSGID_CREATE_PLAYER message announcing the new player. The structure associated with the message contains the player ID that you will use to send messages to that player. Peers that are not hosts must define the player context value when they handle this message. When a peer or host has returned from handling this message, that player context value is set for the session, and cannot be changed. See Using Player Context Values for more discussion of player context values.