Microsoft DirectX 9.0


The SimplePeer sample illustrates how to implement a simple peer-to-peer application. After joining or creating a session, the game begins immediately. Other players can join the session at any time.


Source: (SDK root)\Samples\C++\DirectPlay\SimplePeer

Executable: (SDK root)\Samples\C++\DirectPlay\Bin

User's Guide

Enter the player's name and choose a connection type. You can choose Wait for lobby connection or choose a service provider. Use the Multiplayer Games dialog box to search for an active game to join or to start a new game. After the game has been joined or created, the game begins immediately. Other players can join the game at any time. If Host Migration is on, the host player can leave at any time because Microsoft?DirectPlay?will automatically migrate the host session to another player.

Note  If you choose the Internetwork Packet Exchange (IPX) service provider and want to have DirectPlay perform a search for the address, select the use DPNSVR check box.

Programming Notes

The InitDirectPlay function does the following:

  1. Initializes the Component Object Model (COM) with CoInitialize.
  2. Creates an IDirectPlay8Peer object with CoCreateInstance.
  3. Creates an IDirectPlay8LobbiedApplication object with CoCreateInstance.
  4. Calls IDirectPlay8Peer::Initialize and passes its message handler.
  5. Calls IDirectPlay8LobbiedApplication::Initialize and passes its message handler.
  6. Checks the return value of the IDirectPlay8LobbiedApplication::Initialize method. If it is successful, the application is launched by a lobby client.

If the application is lobby launched, the connection settings can be obtained from the lobby client by the ConnectUsingLobbySetting function of the CNetConnectWizard. The CNetConnectWizard class is a helper class. It uses dialog boxes to query the user for information. The ConnectUsingLobbySettings function does the following:

  1. Calls IDirectPlay8LobbiedApplication::GetConnectionSettings to get the connection setting from the client.
  2. Checks the dwFlags member of the DPL_CONNECTION_SETTINGS structure for the DPLCONNECTIONSETTINGS_HOST flag to see if it should host.
  3. Calls IDirectPlay8Peer::SetPeerInfo.
  4. Calls IDirectPlay8Peer::Host if hosting, otherwise IDirectPlay8Peer::Connect.
  5. Releases the objects in DPL_CONNECTION_SETTINGS.

If the connection setting from the lobby client is not provided, the application calls the DoConnectWizard function of the CNetConnectWizard. DoConnectWizard does the following:

  1. Calls IDirectPlay8Peer::EnumServiceProviders to enumerate service providers.
  2. Calls the ConnectionDlgOnOk function, which displays a dialog box where the user can either choose a service provider or choose to use a lobby connection. If Wait for lobby connection is chosen, the function calls IDirectPlay8LobbiedApplication::SetAppAvailable to tell the lobby client that the application is available for connection. If a service provider is selected, the function creates a DirectPlay host and device address objects by calling CoCreateInstance. Then it calls IDirectPlay8Address::SetSP to pass service provider's globally unique identifier (GUID) into the two DirectPlay address objects.
  3. Calls IDirectPlay8Peer::EnumHosts to enumerate all the games in progress on that service provider.
  4. Processes the DPN_MSGID_ENUM_HOSTS_RESPONSE that arrives in the callback function.

The wizard displays the list of the current sessions and allows users to choose a game from the list or create a new one. If Join is clicked, the SessionDlgJoinGame function calls IDirectPlay8Peer::SetPeerInfo to set the player name and IDirectPlay8Peer::Connect to connect to a game. If Create is clicked, the SessionDlgCreateGame function calls IDirectPlay8Peer::SetPeerInfo to set the player's name and IDirectPlay8Peer::Host to begin hosting a game. A DPN_APPLICATION_DESC structure filled with information such as the game name, max player, and the application GUID is passed in the call to Connect.

Once connected, if Wave to other players is clicked, the WaveToAllPlayers function calls IDirectPlay8Peer::SendTo with the dpnid parameter set to DPNID_ALL_PLAYERS_GROUP and the pBufferDesc parameter pointing to a DWORD containing GAME_MSGID_WAVE.

When you click Exit, all the interfaces are cleaned up.

© 2002 Microsoft Corporation. All rights reserved.