spShareMemoryBase.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifdef _WIN32
  2. #include <Windows.h>
  3. #include <string>
  4. #include <process.h>
  5. #else
  6. #include <winpr/tchar.h>
  7. #endif //_WIN32
  8. #include <string>
  9. #ifndef _SHARE_MEMORY_BASE_
  10. #include <fstream>
  11. using namespace std;
  12. #endif
  13. #define ENTITY_NAME_LEN 100
  14. #define IP_LEN 512 //extend the len, can storage ip or address
  15. class CMyLogFile
  16. {
  17. public:
  18. void PrintCurTime();
  19. explicit CMyLogFile();
  20. ~CMyLogFile();
  21. void LOGERROR(TCHAR* formatString, ...);
  22. void ExtendLOGERROR(TCHAR* formatString, ...);
  23. private:
  24. ofstream *m_cOutFile;
  25. void Output(const TCHAR* data);
  26. bool GetRootDir(char *dirPath);
  27. void Init();
  28. void Release();
  29. BOOL Lock(DWORD dwTime);
  30. void Unlock();
  31. HANDLE m_hLock;
  32. private:
  33. CMyLogFile& operator <<(long lVal);
  34. CMyLogFile& operator <<(const TCHAR* str);
  35. CMyLogFile& operator <<(TCHAR tch);
  36. CMyLogFile& operator <<(int nVal);
  37. CMyLogFile& operator <<(unsigned long ulVal);
  38. CMyLogFile& operator <<(double dVal);
  39. CMyLogFile& operator <<(unsigned int unVal);
  40. #ifdef _WIN32
  41. CMyLogFile& operator <<(u__int64_t unllVal);
  42. #endif
  43. };
  44. extern CMyLogFile m_log;
  45. struct ENTITY_CONNECT_INFO
  46. {
  47. public:
  48. char m_EntityName[ENTITY_NAME_LEN];
  49. char m_ServerIP[IP_LEN];
  50. int m_ServerPort;
  51. char m_Server_BackupIP[IP_LEN];
  52. int m_Server_BackupPort;
  53. int m_DualActive; //-1, unInit; 0, close DualActive;1, open DualActive
  54. int m_currentLink; //-1, unLink; 0, Link to serverIP and serverPort; 1, Link to server_backupIP and server_backupPort
  55. int m_lastLink;
  56. explicit ENTITY_CONNECT_INFO() {clear();}
  57. explicit ENTITY_CONNECT_INFO(const char *entityName, const char *serverIP, int serverPort, const char *serverBackIP, int serverBackPort, int dualActive, int currentLink)
  58. {
  59. clear();
  60. setParam(entityName, serverIP, serverPort, serverBackIP, serverBackPort, dualActive, currentLink);
  61. }
  62. void setParam(const std::string entityName, const std::string serverIP, int serverPort, const std::string serverBackIP, int serverBackPort, int dualActive = -1, int currentLink = -1)
  63. {
  64. //static int i = 0;
  65. memset(m_EntityName, 0, ENTITY_NAME_LEN);
  66. memset(m_ServerIP, 0, IP_LEN);
  67. memset(m_Server_BackupIP, 0, IP_LEN);
  68. memcpy(m_EntityName, entityName.c_str(), entityName.length());
  69. memcpy(m_ServerIP, serverIP.c_str(), serverIP.length());
  70. memcpy(m_Server_BackupIP, serverBackIP.c_str(), serverBackIP.length());
  71. m_ServerPort = serverPort;
  72. m_Server_BackupPort = serverBackPort;
  73. m_DualActive = dualActive;
  74. m_currentLink = currentLink;
  75. if (-1 == m_lastLink) {
  76. m_lastLink = m_currentLink;
  77. }
  78. else {
  79. m_lastLink = (-1 == m_currentLink ? m_lastLink : m_currentLink);
  80. }
  81. //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);
  82. }
  83. void clear()
  84. {
  85. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).withLogProducer(logProducer)("clear");
  86. m_lastLink = -1; setParam("", "", -1, "", -1);
  87. }
  88. };
  89. class connectControl
  90. {
  91. public:
  92. static connectControl* getInstance();
  93. int getEntityNum();
  94. bool getEntityInfo(const char *entityName, ENTITY_CONNECT_INFO *entityInfo);
  95. bool setEntityInfo(const ENTITY_CONNECT_INFO *entityInfo);
  96. bool getSuccessDual(ENTITY_CONNECT_INFO *entityInfo);
  97. int getPriorLink(int lastLink);
  98. void setLastLink(int link);
  99. private:
  100. connectControl(void);
  101. ~connectControl(void);
  102. connectControl(const connectControl&);
  103. connectControl& operator=(const connectControl&);
  104. HANDLE m_hLock;
  105. BOOL Lock(DWORD dwTime);
  106. void Unlock();
  107. };