CommManager.h
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:7k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   CommManager.h
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Interface for the communication manager class.
  11. *                       
  12. *                                                                             
  13. *   Authors: Eran Yariv - 28484475                                           
  14. *            Moshe Zur  - 24070856                                           
  15. *                                                                            
  16. *                                                                            
  17. *   Date: 23/09/98                                                           
  18. *                                                                            
  19. ******************************************************************************/
  20. #ifndef _COMM_MANAGER_H_
  21. #define _COMM_MANAGER_H_
  22. #include ".\dplay.h"
  23. #include "CommSettingDlg.h"
  24. #include "WorkerThread.h"
  25. struct CCommMessage
  26. {
  27.     // Consts:
  28.     enum { MAX_BUFFER_LENGTH = 2048 };
  29.     BYTE            m_aBuffer[MAX_BUFFER_LENGTH];
  30.     DWORD           m_dwLength;
  31. };
  32. // Forward decl.
  33. class CClient;
  34. class CHost;
  35. class CCommManager : public CWorkerThread
  36. {
  37. public:
  38.     
  39.     CCommManager();             // Alloc and initialize DPlay
  40.     ~CCommManager ();           // Free DPlay
  41.     void SetCommParams();       // Present the Comm. pref. dialog
  42.     BOOL OnNewGame();           // Player : connects to server - presents a progress dlg
  43.                                 // Host   : start the server - game starts w/o delay
  44.     BOOL OnStopGame();          // Player : disconnect from server
  45.                                 // Host   : end session
  46.     BOOL IsConnected();         // Player : participate in a session
  47.                                 // Host   : session started
  48.     BOOL IsHost();
  49.     UINT ThreadEntry (LPVOID pParam = 0);
  50.     void NotifyBonusEaten (UINT uTankID);               // Tell all the players a tank ate the current bonus
  51.     void NotifyNewBonus (BonusType, DWORD, CPoint &);   // Tell all the game managers a new bonus is born
  52.     void NotifyExplodingTank(UINT uTankID);             // Tell all the players a tank is exploding
  53.     void NotifyCheckSum(CMessage::MessageData &);       // Tell the host about a new checksum
  54.     void NotifyChatMsg(LPCSTR szMsg);                   // Tell the host to dispatch the chat msg
  55.                                                         // Called by the ChatDlg on new msg.
  56.     CCommMessage& ExposeMessage();
  57.     void SetHostID();
  58.     void SendAsHost(DPID idTo);   
  59.     // Consts
  60.     static const BYTE ACK;
  61.     static const DPID INVALID_PLAYER_ID;
  62.     DPID GetHostID ()           { return m_idHost;   }
  63.     BOOL GetPlayerInfo (UINT ind, BOOL &, CString &, DWORD &, DWORD &);
  64.     void GetPlayerName (DPID id, CString &);    // Get the player name for tank (i)
  65.     BOOL KillPlayer (UINT uPlayerID);
  66. private:
  67.     // methods:
  68.     void CreateDPInterface ();
  69.     void OpenSession();
  70.     void CloseSession();
  71.     void CreatePlayer();
  72.     void DestroyPlayer();
  73.     void EndThread(BOOL bWait);
  74.     void DisplayMessageAndEndGame (UINT u=0);
  75.     void HandleClientMessage();
  76.     void HandleHostMessage();
  77.     // members:
  78.     LPDIRECTPLAY2   m_pIDP;
  79.     DPID            m_idPlayer;
  80.     DPID            m_idHost;
  81.     DPCAPS          m_SessionCaps;
  82.     CClient*        m_pClient;
  83.     CHost*          m_pHost;
  84.     BOOL            m_fIsConnected;             // Session is on
  85.     BOOL            m_fIsHost;                  // Session was opened by us
  86.     BOOL            m_fIsPlaying;               // Player was created
  87.     CMsgQueue&      m_IncomingMsgQ;
  88.     CMsgQueue&      m_OutgoingMsgQ;
  89.     HANDLE          m_hRecvEvent;
  90.     HANDLE          m_hQuitEvent;
  91.     CCommMessage    m_Message;
  92.     DPID            m_idFrom;
  93.     DPID            m_idTo;
  94.     CCriticalSection  m_CSHost;
  95.     BOOL            m_bLostServerConnection;    // Has client lost connection to host?
  96. #ifdef GATHER_NETWORK_STATS
  97.     // Times and totals:
  98.     DWORD           m_dwSessionStartTime;                   // Store DPlay session start time
  99.     DWORD           m_dwTotalBytesSent;                     // Keep total bytes sent
  100.     DWORD           m_dwTotalBytesReceived;                 // Keep total bytes received
  101.     DWORD           m_dwTotalClientMsgsSent;                // Total number of client messages sent
  102.     DWORD           m_dwTotalClientMsgsReceived;            // Total number of client messages received
  103.     DWORD           m_dwTotalHostMsgsSent;                  // Total number of host messages sent
  104.     DWORD           m_dwTotalHostMsgsReceived;              // Total number of host messages received
  105.     // Packet size statistics:
  106.     DWORD           m_dwBiggestSentPacketSize;              // Keep biggest sent packet size
  107.     DWORD           m_dwSmallestSentPacketSize;             // Keep smallest sent packet size
  108.     DWORD           m_dwBiggestReceivedPacketSize;          // Keep biggest received packet size
  109.     DWORD           m_dwSmallestReceivedPacketSize;         // Keep smallest received packet size
  110.     // Time window statistics:
  111.     DWORD           m_dwLastSecTimeRecv;                    // Used to trace one second of time (approx.)
  112.     DWORD           m_dwLastSecTimeSend;                    // Used to trace one second of time (approx.)
  113.     DWORD           m_dwLastRecvTotal;
  114.     DWORD           m_dwLastSentTotal;
  115.     DWORD           m_dwMaxBytesSentInLastSecond;           // Max sent packet size in last second
  116.     DWORD           m_dwMaxBytesReceivedInLastSecond;       // Max received packet size in last second
  117.     // Errors:
  118.     DWORD           m_dwClientSendErrors;                   // Total number of client message sending errors
  119.     DWORD           m_dwHostSendErrors;                     // Total number of host message sending errors
  120.     DWORD           m_dwReceiveErrors;                      // Total number of message receive errors
  121.     void UpdateReceive ();
  122.     void UpdateSend ();
  123.     void TraceNetworkStats();
  124. #endif // GATHER_NETWORK_STATS
  125. };
  126. // {E8660CE0-0DA6-11d2-91DB-444553540000}
  127. DEFINE_GUID(TANKS_GUID, 
  128.     0xe8660ce0, 0xda6, 0x11d2, 0x91, 0xdb, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0);
  129. #include "CommManager.inl"
  130. #endif