spShareMemoryBase.h 3.0 KB

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