home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 9 / Engine / Network.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  4.7 KB  |  130 lines

  1. //-----------------------------------------------------------------------------
  2. // A wrapper class for DirectPlay. Network support is provided through a
  3. // peer-to-peer architecture.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #ifndef NETWORK_H
  9. #define NETWORK_H
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Standard Message Defines
  13. //-----------------------------------------------------------------------------
  14. #define MSGID_CREATE_PLAYER 0x12001
  15. #define MSGID_DESTROY_PLAYER 0x12002
  16. #define MSGID_TERMINATE_SESSION 0x12003
  17.  
  18. //-----------------------------------------------------------------------------
  19. // Network Message Structure
  20. //-----------------------------------------------------------------------------
  21. struct NetworkMessage
  22. {
  23.     unsigned long msgid; // Message ID.
  24.     DPNID dpnid; // DirectPlay player ID.
  25. };
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Received Message Structure
  29. //-----------------------------------------------------------------------------
  30. struct ReceivedMessage : public NetworkMessage
  31. {
  32.     char data[32]; // Message data.
  33. };
  34.  
  35. //-----------------------------------------------------------------------------
  36. // Enumerated Session Information Structure
  37. //-----------------------------------------------------------------------------
  38. struct SessionInfo
  39. {
  40.     IDirectPlay8Address *address; // Session network address.
  41.     DPN_APPLICATION_DESC description; // Application description.
  42. };
  43.  
  44. //-----------------------------------------------------------------------------
  45. // Player Information Structure
  46. //-----------------------------------------------------------------------------
  47. struct PlayerInfo
  48. {
  49.     DPNID dpnid; // DirectPlay player ID.
  50.     char *name; // Name of the player.
  51.     void *data; // Application specific player data.
  52.     unsigned long size; // Data size.
  53.  
  54.     //-------------------------------------------------------------------------
  55.     // The player information structure constructor.
  56.     //-------------------------------------------------------------------------
  57.     PlayerInfo()
  58.     {
  59.         dpnid = 0;
  60.         name = NULL;
  61.         data = NULL;
  62.         size = 0;
  63.     }
  64.  
  65.     //-------------------------------------------------------------------------
  66.     // The player information structure destructor.
  67.     //-------------------------------------------------------------------------
  68.     ~PlayerInfo()
  69.     {
  70.         SAFE_DELETE( name );
  71.         SAFE_DELETE( data );
  72.     }
  73. };
  74.  
  75. //-----------------------------------------------------------------------------
  76. // Network Class
  77. //-----------------------------------------------------------------------------
  78. class Network
  79. {
  80. public:
  81.     Network( GUID guid, void (*HandleNetworkMessageFunction)( ReceivedMessage *msg ) );
  82.     virtual ~Network();
  83.  
  84.     void Update();
  85.  
  86.     void EnumerateSessions();
  87.  
  88.     bool Host( char *name, char *session, int players = 0, void *playerData = NULL, unsigned long dataSize = 0 );
  89.     bool Join( char *name, int session = 0, void *playerData = NULL, unsigned long dataSize = 0 );
  90.     void Terminate();
  91.  
  92.     void SetReceiveAllowed( bool allowed );
  93.  
  94.     SessionInfo *GetNextSession( bool restart = false );
  95.     PlayerInfo *GetPlayer( DPNID dpnid );
  96.  
  97.     DPNID GetLocalID();
  98.     DPNID GetHostID();
  99.     bool IsHost();
  100.  
  101.     void Send( void *data, long size, DPNID dpnid = DPNID_ALL_PLAYERS_GROUP, long flags = 0 );
  102.  
  103. private:
  104.     static HRESULT WINAPI NetworkMessageHandler( PVOID context, DWORD msgid, PVOID data );
  105.  
  106. private:
  107.     GUID m_guid; // Application specific GUID.
  108.     IDirectPlay8Peer *m_dpp; // DirectPlay peer interface.
  109.     IDirectPlay8Address *m_device; // DirectPlay device address.
  110.  
  111.     unsigned long m_port; // Port for network communication.
  112.     unsigned long m_sendTimeOut; // Timeout for sent network messages.
  113.     unsigned long m_processingTime; // Allowed time period for processing received network messages.
  114.  
  115.     DPNID m_dpnidLocal; // DirectPlay ID of the local player.
  116.     DPNID m_dpnidHost; // DirectPlay ID of the host player.
  117.  
  118.     CRITICAL_SECTION m_sessionCS; // Enumerated session list critical section.
  119.     LinkedList< SessionInfo > *m_sessions; // Linked list of enumerated sessions.
  120.  
  121.     CRITICAL_SECTION m_playerCS; // Player list critical section.
  122.     LinkedList< PlayerInfo > *m_players; // Linked list of players.
  123.  
  124.     bool m_receiveAllowed; // Inidcates if the network is allowed to receive application specific messages.
  125.     CRITICAL_SECTION m_messageCS; // Network message list critical section.
  126.     LinkedList< ReceivedMessage > *m_messages; // Linked list of network messages.
  127.     void (*HandleNetworkMessage)( ReceivedMessage *msg ); // Pointer to an application specific network message handler.
  128. };
  129.  
  130. #endif