ITCPServer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. //#include <fmt/core.h>
  3. #include <winsock2.h>
  4. class ITCPServer;
  5. //连接客户端信息
  6. struct ClientInfo
  7. {
  8. SOCKET sock;
  9. SOCKADDR_IN clientAddr;////定义地址族
  10. TCHAR strIP[128]; //ipv6地址长度
  11. int iPort;
  12. bool bIsTransmit;
  13. int iSendSucCount;
  14. int iSendFailCount;
  15. int iRecvCount;
  16. ITCPServer * pTcpServer1; //创建该 clientInfo的TcpServer 的指针
  17. void* pAppLayerClt; //应用层客户端信息对象
  18. //用于绑定一些用户自定义数据,默认无用
  19. void* pData1;
  20. void* pData2;
  21. void* pData3;
  22. int iData1;
  23. bool isValid;
  24. SYSTEMTIME stLastActive;
  25. SYSTEMTIME stCreateTime;
  26. ClientInfo()
  27. {
  28. GetLocalTime(&stCreateTime);
  29. GetLocalTime(&stLastActive);
  30. sock = NULL;
  31. pAppLayerClt = NULL;
  32. pData1 = NULL;
  33. pData2 = NULL;
  34. pData3 = NULL;
  35. pTcpServer1 = NULL;
  36. iData1 = 0;
  37. bIsTransmit = false;
  38. iSendSucCount = 0;
  39. iSendFailCount = 0;
  40. iRecvCount = 0;
  41. memset(strIP, 0, 16);
  42. isValid = true;
  43. }
  44. inline std::string GetAddressStr()
  45. {
  46. return fmt::format("{}:{}", strIP, iPort);
  47. }
  48. ClientInfo* GenerateClienInfo(){
  49. ClientInfo *ptr;
  50. ptr = new ClientInfo();
  51. if (ptr) {
  52. *ptr = *this;
  53. ptr->pTcpServer1 = NULL;
  54. ptr->pData1 = NULL;
  55. ptr->pData2 = NULL;
  56. ptr->pData3 = NULL;
  57. }
  58. return ptr;
  59. }
  60. };
  61. //该结构体只适用于指针调用
  62. typedef struct connHistoryInfo {
  63. CString strIp;
  64. SOCKET socket;
  65. HANDLE m_mutex;
  66. int iSendSucCount;
  67. int iSendFailCount;
  68. int iRecvCount;
  69. int iReconnnectCount;
  70. bool bOnline;
  71. ClientInfo *pCltInfo;
  72. SYSTEMTIME lastConnectTime;
  73. bool bProxy; //代理标志
  74. bool bRecving; //开始接收数据
  75. //ConnHistoryData historyData;//历史数据预留 暂时不用
  76. connHistoryInfo() {
  77. pCltInfo = NULL;
  78. strIp = "";
  79. socket = 0;
  80. iSendSucCount = 0;
  81. iSendFailCount = 0;
  82. iRecvCount = 0;
  83. iReconnnectCount = 0;
  84. bOnline = false;
  85. bProxy = false;
  86. bRecving = false;
  87. memset(&lastConnectTime, 0, sizeof(SYSTEMTIME));
  88. m_mutex = CreateMutex(NULL, FALSE, NULL);
  89. }
  90. ~connHistoryInfo() {
  91. if (m_mutex) {
  92. CloseHandle(m_mutex);
  93. m_mutex = NULL;
  94. }
  95. }
  96. bool Lock() {
  97. if (m_mutex) {
  98. WaitForSingleObject(m_mutex, INFINITE);
  99. return true;
  100. }
  101. return false;
  102. }
  103. void Unlock() {
  104. if (m_mutex) ReleaseMutex(m_mutex);
  105. }
  106. }ConnHistoryInfo;
  107. interface ITcpServerCallBack {
  108. public:
  109. virtual void ConnStatusChange(ClientInfo* pCltInfo, BOOL bIsConn) = 0;
  110. virtual void OnRecvData_TCPServer(BYTE* pData,int iLen, ClientInfo* pCltInfo) = 0;
  111. };
  112. class ITCPServer
  113. {
  114. public:
  115. //服务器控制 strLocalIP传入为空= "" ,则绑定所有本地ip
  116. virtual bool StartServer(ITcpServerCallBack* pUser, int iPort, CString strLocalIP = _T("")) =0;
  117. virtual void StopServer() = 0;
  118. //自动关闭长期未活动连接 0不自动关闭 单位秒
  119. int iAutoClearDeadConnectionTime;
  120. //数据发送,发往某个客户端
  121. virtual BOOL SendData(const char* pData, int iLen, ClientInfo* pCltInfo) = 0;
  122. virtual BOOL SendData(const char* pData, int iLen, CString strIP) = 0;
  123. ITCPServer()
  124. :m_strName("TCPIOCP")
  125. , m_strMonitoringIP("")
  126. ,m_bStopRecv(FALSE)
  127. {
  128. iAutoClearDeadConnectionTime = 0;
  129. }
  130. virtual CString GetIOCPName() {
  131. return m_strName;
  132. }
  133. virtual void SettIOCPName(CString strName) {
  134. m_strName = strName;
  135. }
  136. virtual void SetMonitoring(CString value) {
  137. m_strMonitoringIP = value;
  138. }
  139. virtual CString GetMonitoring() const {
  140. return m_strMonitoringIP;
  141. }
  142. virtual BOOL GetStopRecvStatus() const {
  143. return m_bStopRecv;
  144. }
  145. virtual void SetStopRecvStatus(BOOL b) {
  146. m_bStopRecv = b;
  147. }
  148. virtual ITCPServer *GetChildren() { return NULL; }
  149. //获取IP列表
  150. virtual void GetIPList(std::vector<CString> &vec) = NULL;
  151. virtual void GetConnectInfo(std::vector<ClientInfo *> &vec) = NULL;
  152. virtual void GetHistoryConnectInfo(std::map<CString, ConnHistoryInfo *> &mapHis) = NULL;
  153. virtual ConnHistoryInfo * GetHistoryConnectInfo(const CString &strIP) = NULL;
  154. virtual void ClearConnHistoryInfo() = NULL;
  155. private:
  156. CString m_strName;
  157. CString m_strMonitoringIP;
  158. BOOL m_bStopRecv;//该功能用于通信调试分析诊断问题,本地停止从Tcp缓冲接收数据,导致对端Tcp缓冲区满,测试对端程序处理缓冲区满时候的健壮性
  159. };