CSCommon: MClient.h 소스 파일

MAIET

MClient.h

00001 #ifndef MCLIENT_H
00002 #define MCLIENT_H
00003 
00004 #include "MCommandCommunicator.h"
00005 
00006 class MClientCommandProcessor;
00007 class MCommand;
00008 
00009 #define MAX_PACKETBUFFER_SIZE 65535
00010 #define RINGBUFSIZE 16384
00011 
00012 // 나중에 패킷 버퍼 대체로 사용할 목적으로 우선 만들어둔다.
00013 class MRingBuffer {
00014 private:
00015     int         m_iBufSize;
00016     char*       m_Buf;
00017     char*       m_cpBegin, m_cpEnd;
00018 protected:
00019 public:
00020     MRingBuffer();
00021     MRingBuffer(int iBufSize);
00022     virtual ~MRingBuffer();
00023     void Reserve(int iBufSize); 
00024     bool Enqueue(char* cp, int iDataSize);
00025     bool Dequeue(char* cpOut, int iDataSize);
00026     int GetSize()       { return m_iBufSize; }
00027 };
00028 
00030 enum MZMOMType{
00031     MZMDM_GENERAL,          
00032     MZMOM_USERCOMMAND,      
00033     MZMOM_ERROR,            
00034     MZMOM_LOCALREPLY,       
00035     MZMOM_SERVERREPLY,      
00036 };
00037 
00038 
00040 class MClient : public MCommandCommunicator{
00041 private:
00042     static MClient* m_pInstance;            
00043 protected:
00044     MUID                m_Server;       
00045     char                m_PacketBuffer[MAX_PACKETBUFFER_SIZE];
00046     int                 m_iPBufferTop;
00047 
00048     MClientSocket       m_ClientSocket; 
00049     CRITICAL_SECTION    m_csRecvLock;   
00050 
00051     MCommandBuilder*    m_pCommandBuilder;
00052 
00053 protected:
00054     void LockRecv() { EnterCriticalSection(&m_csRecvLock); }
00055     void UnlockRecv() { LeaveCriticalSection(&m_csRecvLock); }
00056 
00057     virtual void OnRegisterCommand(MCommandManager* pCommandManager);
00058     virtual bool OnCommand(MCommand* pCommand);
00059 
00061     virtual void OutputLocalInfo(void) = 0;
00063     virtual void OutputMessage(const char* szMessage, MZMOMType nType=MZMDM_GENERAL) = 0;
00064 
00065     MCommandBuilder* GetCommandBuilder()    { return m_pCommandBuilder; }
00066     virtual void SendCommand(MCommand* pCommand);
00067     virtual MCommand* GetCommandSafe();
00068 
00069     virtual int OnConnected(MUID* pTargetUID, MUID* pAllocUID, MCommObject* pCommObj);
00070     virtual int OnConnected(SOCKET sock, MUID* pTargetUID, MUID* pAllocUID);
00071     int MakeCmdPacket(char* pOutPacket, int iMaxPacketSize, MCommand* pCommand);
00072     
00073 
00074     // Socket Event
00075     virtual bool OnSockConnect(SOCKET sock);
00076     virtual bool OnSockDisconnect(SOCKET sock);
00077     virtual bool OnSockRecv(SOCKET sock, char* pPacket, DWORD dwSize);
00078     virtual void OnSockError(SOCKET sock, SOCKET_ERROR_EVENT ErrorEvent, int &ErrorCode);
00079 
00080 public:
00081     MClient();
00082     virtual ~MClient();
00083 
00085     static MClient* GetInstance(void);
00086 
00087     MUID GetServerUID(void){ return m_Server; }
00088     virtual MUID GetSenderUIDBySocket(SOCKET socket);
00089     MClientSocket* GetClientSocket()                        { return &m_ClientSocket; }
00090     void GetTraffic(int* nSendTraffic, int* nRecvTraffic)   { return m_ClientSocket.GetTraffic(nSendTraffic, nRecvTraffic); }
00091 
00092     virtual bool Post(MCommand* pCommand);
00093     virtual bool Post(char* szErrMsg, int nErrMsgCount, const char* szCommand);
00094 
00095     virtual int Connect(MCommObject* pCommObj);
00096     virtual int Connect(SOCKET* pSocket, char* szIP, int nPort);
00097     virtual void Disconnect(MUID uid);
00098     virtual void Log(const char* szLog){}
00099 
00101     void OutputMessage(MZMOMType nType, const char *pFormat,...);
00102 
00103     static bool SocketRecvEvent(void* pCallbackContext, SOCKET sock, char* pPacket, DWORD dwSize);
00104     static bool SocketConnectEvent(void* pCallbackContext, SOCKET sock);
00105     static bool SocketDisconnectEvent(void* pCallbackContext, SOCKET sock);
00106     static void SocketErrorEvent(void* pCallbackContext, SOCKET sock, SOCKET_ERROR_EVENT ErrorEvent, int &ErrorCode);
00107 
00108     bool IsConnected() { return m_ClientSocket.IsActive(); }
00109 };
00110 
00111 void SplitIAddress(char* szIP, int* pPort, const char* szAddress);
00112 
00113 #endif


MAIET entertainment