#ifdef _WIN32 #include #include #include #else #include #endif //_WIN32 #include #ifndef _SHARE_MEMORY_BASE_ #include using namespace std; #endif #define ENTITY_NAME_LEN 100 #define IP_LEN 512 //extend the len, can storage ip or address class CMyLogFile { public: void PrintCurTime(); explicit CMyLogFile(); ~CMyLogFile(); void LOGERROR(TCHAR* formatString, ...); void ExtendLOGERROR(TCHAR* formatString, ...); private: ofstream *m_cOutFile; void Output(const TCHAR* data); bool GetRootDir(char *dirPath); void Init(); void Release(); BOOL Lock(DWORD dwTime); void Unlock(); HANDLE m_hLock; private: CMyLogFile& operator <<(long lVal); CMyLogFile& operator <<(const TCHAR* str); CMyLogFile& operator <<(TCHAR tch); CMyLogFile& operator <<(int nVal); CMyLogFile& operator <<(unsigned long ulVal); CMyLogFile& operator <<(double dVal); CMyLogFile& operator <<(unsigned int unVal); #ifdef _WIN32 CMyLogFile& operator <<(u__int64_t unllVal); #endif }; extern CMyLogFile m_log; struct ENTITY_CONNECT_INFO { public: char m_EntityName[ENTITY_NAME_LEN]; char m_ServerIP[IP_LEN]; int m_ServerPort; char m_Server_BackupIP[IP_LEN]; int m_Server_BackupPort; int m_DualActive; //-1, unInit; 0, close DualActive;1, open DualActive int m_currentLink; //-1, unLink; 0, Link to serverIP and serverPort; 1, Link to server_backupIP and server_backupPort int m_lastLink; explicit ENTITY_CONNECT_INFO() {clear();} explicit ENTITY_CONNECT_INFO(const char *entityName, const char *serverIP, int serverPort, const char *serverBackIP, int serverBackPort, int dualActive, int currentLink) { clear(); setParam(entityName, serverIP, serverPort, serverBackIP, serverBackPort, dualActive, currentLink); } void setParam(const std::string entityName, const std::string serverIP, int serverPort, const std::string serverBackIP, int serverBackPort, int dualActive = -1, int currentLink = -1) { //static int i = 0; memset(m_EntityName, 0, ENTITY_NAME_LEN); memset(m_ServerIP, 0, IP_LEN); memset(m_Server_BackupIP, 0, IP_LEN); memcpy(m_EntityName, entityName.c_str(), entityName.length()); memcpy(m_ServerIP, serverIP.c_str(), serverIP.length()); memcpy(m_Server_BackupIP, serverBackIP.c_str(), serverBackIP.length()); m_ServerPort = serverPort; m_Server_BackupPort = serverBackPort; m_DualActive = dualActive; m_currentLink = currentLink; if (-1 == m_lastLink) { m_lastLink = m_currentLink; } else { m_lastLink = (-1 == m_currentLink ? m_lastLink : m_currentLink); } //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).withLogProducer(logProducer)("i : %d, Entity:%s,m_currentLink : %d, last : %d", i++, m_EntityName, m_currentLink, m_lastLink); } void clear() { //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).withLogProducer(logProducer)("clear"); m_lastLink = -1; setParam("", "", -1, "", -1); } }; class connectControl { public: static connectControl* getInstance(); int getEntityNum(); bool getEntityInfo(const char *entityName, ENTITY_CONNECT_INFO *entityInfo); bool setEntityInfo(const ENTITY_CONNECT_INFO *entityInfo); bool getSuccessDual(ENTITY_CONNECT_INFO *entityInfo); int getPriorLink(int lastLink); void setLastLink(int link); private: connectControl(void); ~connectControl(void); connectControl(const connectControl&); connectControl& operator=(const connectControl&); HANDLE m_hLock; BOOL Lock(DWORD dwTime); void Unlock(); };