CSCommon: MMatchClient.h 소스 파일

MAIET

MMatchClient.h

00001 #ifndef MMATCHCLIENT_H
00002 #define MMATCHCLIENT_H
00003 
00004 #include <list>
00005 using namespace std;
00006 #include "MMatchGlobal.h"
00007 #include "MCommandCommunicator.h"
00008 #include "MClient.h"
00009 #include "MSafeUDP.h"
00010 #include "MMatchObjCache.h"
00011 #include "MMatchObject.h"
00012 #include "MMatchTransDataType.h"
00013 #include "MMatchStage.h"
00014 #include "MMatchGlobal.h"
00015 
00016 #define MATCHCLIENT_DEFAULT_UDP_PORT    10000
00017 #define MAX_PING                        999
00018 
00019 class MMatchPeerInfo
00020 {
00021 public:
00022     MUID    uidChar;
00023     char    szIP[64];
00024     int     nPort;
00025     MTD_CharInfo        CharInfo;
00026     MTD_ExtendInfo      ExtendInfo;
00027 protected:
00028     bool            m_bUDPTestResult;
00029     bool            m_bUDPTestProcess;
00030     int             m_nUDPTestCount;
00031 
00032     bool            m_bOpened;
00033     int             m_nPing;
00034     unsigned int    m_nLastPingTime;
00035     unsigned int    m_nLastPongTime;
00036 
00037 public:
00038     MMatchPeerInfo() {
00039         uidChar = MUID(0,0);
00040         szIP[0] = NULL;
00041         nPort = 0;
00042         memset(&CharInfo, 0, sizeof(MTD_CharInfo));
00043 
00044         m_bUDPTestResult = false;
00045         m_bUDPTestProcess = false;
00046         m_nUDPTestCount = 0;
00047 
00048         m_bOpened = false;
00049         m_nPing = 0;
00050         m_nLastPingTime = 0;
00051         m_nLastPongTime = 0;
00052     }
00053     virtual ~MMatchPeerInfo()           {}
00054     bool GetUDPTestResult()             { return m_bUDPTestResult; }
00055     void SetUDPTestResult(bool bResult) { m_bUDPTestResult = bResult; }
00056     void StartUDPTest()                 { m_bUDPTestProcess = true; m_nUDPTestCount = 10; }
00057     void StopUDPTest()                  { m_bUDPTestProcess = false; m_nUDPTestCount = 0; }
00058     bool GetProcess()                   { return m_bUDPTestProcess; }
00059     int GetTestCount()                  { return m_nUDPTestCount; }
00060     void UseTestCount()                 { m_nUDPTestCount--; }
00061 
00062     bool IsOpened()                     { return m_bOpened; }
00063     void SetOpened(bool bVal)           { m_bOpened = bVal; }
00064     int GetPing(unsigned int nCurrTime) { 
00065         if ((int)m_nLastPongTime - (int)m_nLastPingTime >= 0) {
00066             return m_nPing;
00067         } else {
00068             int nDelay = nCurrTime - m_nLastPingTime;
00069             if (nDelay < MAX_PING) {
00070                 return m_nPing;
00071             } else {
00072                 return min(MAX_PING, nDelay);
00073             }
00074         }
00075     }
00076     void UpdatePing(unsigned int nTime, int nPing) { 
00077         m_nLastPongTime = nTime;
00078         m_nPing = nPing; 
00079     }
00080     void SetLastPingTime(unsigned int nTime) { 
00081         if ((int)m_nLastPongTime - (int)m_nLastPingTime >= 0)
00082             m_nLastPingTime = nTime; 
00083     }
00084     unsigned int GetLastPingTime()      { return m_nLastPingTime; }
00085     unsigned int GetLastPongTime()      { return m_nLastPongTime; }
00086 };
00087 class MMatchPeerInfoList : public list<MMatchPeerInfo*>
00088 {
00089 public:
00090     ~MMatchPeerInfoList() { Clear(); }
00091     void Clear() { while(empty()==false) { delete *begin(); erase(begin()); } }
00092     bool Delete(const char* szIP);
00093     bool Delete(MMatchPeerInfo* pPeerInfo);
00094 };
00095 
00097 class MMatchClient : public MClient
00098 {
00099 protected:
00100     MUID                m_uidServer;
00101     MUID                m_uidPlayer;
00102     MUID                m_uidChannel;
00103     MUID                m_uidStage;
00104 
00105     char                m_szServerName[64];
00106     char                m_szServerIP[32];
00107     int                 m_nServerPort;
00108     int                 m_nServerPeerPort;
00109     MMatchServerMode    m_nServerMode;              
00110 protected:
00111     MMatchObjCacheMap   m_ObjCacheMap;
00112     MSafeUDP            m_SafeUDP;
00113     MMatchPeerInfoList  m_Peers;
00114     bool                m_bBridgePeerFlag;
00115     bool                m_bUDPTestProcess;
00116     
00117 protected:
00118     MClientSocket       m_AgentSocket;
00119 
00120     MUID                m_uidAgentServer;
00121     MUID                m_uidAgentClient;
00122 
00123     char                m_szAgentIP[32];
00124     int                 m_nAgentPort;
00125     int                 m_nAgentPeerPort;
00126 
00127     bool                m_bAgentPeerFlag;   // Agent와 UDP연결여부
00128     int                 m_nAgentPeerCount;  // Agent와 UDP연결시 Retry를 위한 정보
00129 
00130     bool                m_bAllowTunneling;
00131 
00132 protected:
00133     bool GetAgentPeerFlag()             { return m_bAgentPeerFlag; }
00134     void SetAgentPeerFlag(bool bVal)    { m_bAgentPeerFlag = bVal; }
00135     int GetAgentPeerCount()             { return m_nAgentPeerCount; }
00136     void SetAgentPeerCount(int nCount)  { m_nAgentPeerCount = nCount; }
00137     void StartAgentPeerConnect();
00138     void CastAgentPeerConnect();
00139     void StartUDPTest(const MUID& uidChar);
00140 
00141 protected:
00142     // tcp socket event
00143     virtual bool OnSockConnect(SOCKET sock);
00144     virtual bool OnSockDisconnect(SOCKET sock);
00145     virtual bool OnSockRecv(SOCKET sock, char* pPacket, DWORD dwSize);
00146     virtual void OnSockError(SOCKET sock, SOCKET_ERROR_EVENT ErrorEvent, int &ErrorCode);
00147 
00148     virtual int OnConnected(SOCKET sock, MUID* pTargetUID, MUID* pAllocUID);
00149     virtual void OnRegisterCommand(MCommandManager* pCommandManager);
00150     virtual bool OnCommand(MCommand* pCommand);
00151     virtual int OnResponseMatchLogin(const MUID& uidServer, int nResult, const char* szServerName, 
00152                                      const MMatchServerMode nServerMode, const MUID& uidPlayer);
00153     virtual void OnObjectCache(unsigned int nType, void* pBlob, int nCount);
00154     virtual void OnUDPTest(const MUID& uidChar);
00155     virtual void OnUDPTestReply(const MUID& uidChar);
00156     virtual void OnResponseAgentLogin();
00157     virtual void OnLocateAgentToClient(const MUID& uidAgent, char* szIP, int nPort, int nUDPPort);
00158     virtual void OnTunnelingTCP(const MUID& uidSender, void* pBlob, int nCount);
00159     virtual void OnTunnelingUDP(const MUID& uidSender, void* pBlob, int nCount);    
00160     virtual void OnAllowTunnelingTCP();
00161     virtual void OnAllowTunnelingUDP(); 
00162     virtual void OnAgentConnected(const MUID& uidAgentServer, const MUID& uidAlloc);
00163     virtual void OnAgentError(int nError);
00164 
00165     void OutputLocalInfo(void);
00166 
00167     virtual void SendCommand(MCommand* pCommand);
00168     bool SendCommandToAgent(MCommand* pCommand);
00169     void SendCommandByUDP(MCommand* pCommand, char* szIP, int nPort);
00170     void SendCommandByTunneling(MCommand* pCommand);
00171     void ParseUDPPacket(char* pData,MPacketHeader* pPacketHeader,char* szIP,unsigned int nPort);
00172 
00173 public:
00174     MMatchClient();
00175     virtual ~MMatchClient();
00176 
00177     bool Create(unsigned short nUDPPort);
00178     
00179     bool GetBridgePeerFlag()            { return m_bBridgePeerFlag; }
00180     void SetBridgePeerFlag(bool bFlag)  { m_bBridgePeerFlag = bFlag; }
00181     void AddPeer(MMatchPeerInfo* pPeerInfo);
00182     bool DeletePeer(const char* szIP);
00183     bool DeletePeer(const MUID uid);
00184     MMatchPeerInfo* FindPeer(const char* szIP, const int nPort);
00185     MMatchPeerInfo* FindPeer(const MUID& uidChar);
00186     void ClearPeers();
00187     void CastStageBridgePeer(const MUID& uidChar, const MUID& uidStage);    // UDP lost 대비해 Stage입장시 여러번 호출 필요
00188 
00189     bool GetUDPTestProcess()            { return m_bUDPTestProcess; }
00190     void SetUDPTestProcess(bool bVal)   { m_bUDPTestProcess = bVal; }
00191     void UpdateUDPTestProcess();
00192     void GetUDPTraffic(int* nSendTraffic, int* nRecvTraffic)    { return m_SafeUDP.GetTraffic(nSendTraffic, nRecvTraffic); }
00193 
00194     void SetUDPPort(int nPort);
00195     MUID GetServerUID() { return m_uidServer; }
00196     MUID GetPlayerUID() { return m_uidPlayer; }
00197     MUID GetChannelUID() { return m_uidChannel; }
00198     MUID GetStageUID() { return m_uidStage; }
00199     virtual MUID GetSenderUIDBySocket(SOCKET socket);
00200 
00201 
00202     void SetServerAddr(const char* szIP, int nPort) { 
00203         strcpy(m_szServerIP,szIP), m_nServerPort = nPort; 
00204     }
00205     char* GetServerIP() { return m_szServerIP; }
00206     int GetServerPort() { return m_nServerPort; }
00207     void SetServerPeerPort(int nPeerPort) { m_nServerPeerPort = nPeerPort; }
00208     int GetServerPeerPort() { return m_nServerPeerPort; }
00209 
00210     MMatchPeerInfoList* GetPeers() { return &m_Peers; } 
00211     MSafeUDP* GetSafeUDP() { return &m_SafeUDP; }
00212     string GetObjName(const MUID& uid);
00213     MMatchObjCache* FindObjCache(const MUID& uid);
00214     void ReplaceObjCache(MMatchObjCache* pCache);
00215     void UpdateObjCache(MMatchObjCache* pCache);
00216     void RemoveObjCache(const MUID& uid);
00217     void ClearObjCaches();
00218 
00219     static bool UDPSocketRecvEvent(DWORD dwIP, WORD wRawPort, char* pPacket, DWORD dwSize);
00220 
00221 public:
00222     void SetAgentAddr(const char* szIP, int nPort)  { 
00223         strcpy(m_szAgentIP,szIP), m_nAgentPort = nPort; 
00224     }
00225     char* GetAgentIP() { return m_szAgentIP; }
00226     int GetAgentPort() { return m_nAgentPort; }
00227     void SetAgentPeerPort(int nPeerPort) { m_nAgentPeerPort = nPeerPort; }
00228     int GetAgentPeerPort() { return m_nAgentPeerPort; }
00229     const MUID& GetAgentServerUID() { return m_uidAgentServer; }
00230     const MUID& GetAgentClientUID() { return m_uidAgentClient; }
00231     bool GetAllowTunneling() { return m_bAllowTunneling; }
00232     void SetAllowTunneling(bool bAllow) { m_bAllowTunneling = bAllow; }
00233 
00234     int AgentConnect(SOCKET* pSocket, char* szIP, int nPort);
00235     void AgentDisconnect();
00236 
00237     MMatchObjCacheMap* GetObjCacheMap() { return &m_ObjCacheMap; }
00238     MMatchServerMode GetServerMode()    { return m_nServerMode; }
00239     const char* GetServerName()         { return m_szServerName; }
00240 };
00241 
00242 /* 
00243 로컬에서 게임 2개 띄워놓고 테스트 하는 법
00244 우선 ZoneServer를 띄운다.
00245 
00246 Chat창에서 각각의 게임창마다
00247 /con                             ,      /con
00248 /peer.setport 10000              ,      /peer.setport 10001 
00249 /peer.addpeer 127.0.0.1 10001    ,      /peer.addpeer 127.0.0.1 10000 
00250 이렇게 입력하고서 테스트 해야함.
00251 
00252 왜냐하면 각 게임창마다 포트를 다르게 설정해야하기 때문..
00253 
00254 */
00255 
00256 #endif


MAIET entertainment