| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #pragma once
- //#include <fmt/core.h>
- #include <winsock2.h>
- class ITCPServer;
- //连接客户端信息
- struct ClientInfo
- {
- SOCKET sock;
- SOCKADDR_IN clientAddr;////定义地址族
- TCHAR strIP[128]; //ipv6地址长度
- int iPort;
- bool bIsTransmit;
- int iSendSucCount;
- int iSendFailCount;
- int iRecvCount;
- ITCPServer * pTcpServer1; //创建该 clientInfo的TcpServer 的指针
- void* pAppLayerClt; //应用层客户端信息对象
- //用于绑定一些用户自定义数据,默认无用
- void* pData1;
- void* pData2;
- void* pData3;
- int iData1;
- bool isValid;
- SYSTEMTIME stLastActive;
- SYSTEMTIME stCreateTime;
- ClientInfo()
- {
- GetLocalTime(&stCreateTime);
- GetLocalTime(&stLastActive);
- sock = NULL;
- pAppLayerClt = NULL;
- pData1 = NULL;
- pData2 = NULL;
- pData3 = NULL;
- pTcpServer1 = NULL;
- iData1 = 0;
- bIsTransmit = false;
- iSendSucCount = 0;
- iSendFailCount = 0;
- iRecvCount = 0;
- memset(strIP, 0, 16);
- isValid = true;
- }
- inline std::string GetAddressStr()
- {
- return fmt::format("{}:{}", strIP, iPort);
- }
- ClientInfo* GenerateClienInfo(){
- ClientInfo *ptr;
- ptr = new ClientInfo();
- if (ptr) {
- *ptr = *this;
- ptr->pTcpServer1 = NULL;
- ptr->pData1 = NULL;
- ptr->pData2 = NULL;
- ptr->pData3 = NULL;
- }
- return ptr;
- }
- };
- //该结构体只适用于指针调用
- typedef struct connHistoryInfo {
- CString strIp;
- SOCKET socket;
- HANDLE m_mutex;
- int iSendSucCount;
- int iSendFailCount;
- int iRecvCount;
- int iReconnnectCount;
- bool bOnline;
- ClientInfo *pCltInfo;
- SYSTEMTIME lastConnectTime;
- bool bProxy; //代理标志
- bool bRecving; //开始接收数据
- //ConnHistoryData historyData;//历史数据预留 暂时不用
- connHistoryInfo() {
- pCltInfo = NULL;
- strIp = "";
- socket = 0;
- iSendSucCount = 0;
- iSendFailCount = 0;
- iRecvCount = 0;
- iReconnnectCount = 0;
- bOnline = false;
- bProxy = false;
- bRecving = false;
- memset(&lastConnectTime, 0, sizeof(SYSTEMTIME));
- m_mutex = CreateMutex(NULL, FALSE, NULL);
- }
- ~connHistoryInfo() {
- if (m_mutex) {
- CloseHandle(m_mutex);
- m_mutex = NULL;
- }
- }
- bool Lock() {
- if (m_mutex) {
- WaitForSingleObject(m_mutex, INFINITE);
- return true;
- }
- return false;
- }
- void Unlock() {
- if (m_mutex) ReleaseMutex(m_mutex);
- }
- }ConnHistoryInfo;
- interface ITcpServerCallBack {
- public:
- virtual void ConnStatusChange(ClientInfo* pCltInfo, BOOL bIsConn) = 0;
- virtual void OnRecvData_TCPServer(BYTE* pData,int iLen, ClientInfo* pCltInfo) = 0;
- };
- class ITCPServer
- {
- public:
- //服务器控制 strLocalIP传入为空= "" ,则绑定所有本地ip
- virtual bool StartServer(ITcpServerCallBack* pUser, int iPort, CString strLocalIP = _T("")) =0;
- virtual void StopServer() = 0;
- //自动关闭长期未活动连接 0不自动关闭 单位秒
- int iAutoClearDeadConnectionTime;
- //数据发送,发往某个客户端
- virtual BOOL SendData(const char* pData, int iLen, ClientInfo* pCltInfo) = 0;
- virtual BOOL SendData(const char* pData, int iLen, CString strIP) = 0;
- ITCPServer()
- :m_strName("TCPIOCP")
- , m_strMonitoringIP("")
- ,m_bStopRecv(FALSE)
- {
- iAutoClearDeadConnectionTime = 0;
- }
- virtual CString GetIOCPName() {
- return m_strName;
- }
- virtual void SettIOCPName(CString strName) {
- m_strName = strName;
- }
- virtual void SetMonitoring(CString value) {
- m_strMonitoringIP = value;
- }
- virtual CString GetMonitoring() const {
- return m_strMonitoringIP;
- }
- virtual BOOL GetStopRecvStatus() const {
- return m_bStopRecv;
- }
- virtual void SetStopRecvStatus(BOOL b) {
- m_bStopRecv = b;
- }
- virtual ITCPServer *GetChildren() { return NULL; }
- //获取IP列表
- virtual void GetIPList(std::vector<CString> &vec) = NULL;
- virtual void GetConnectInfo(std::vector<ClientInfo *> &vec) = NULL;
- virtual void GetHistoryConnectInfo(std::map<CString, ConnHistoryInfo *> &mapHis) = NULL;
- virtual ConnHistoryInfo * GetHistoryConnectInfo(const CString &strIP) = NULL;
- virtual void ClearConnHistoryInfo() = NULL;
- private:
- CString m_strName;
- CString m_strMonitoringIP;
- BOOL m_bStopRecv;//该功能用于通信调试分析诊断问题,本地停止从Tcp缓冲接收数据,导致对端Tcp缓冲区满,测试对端程序处理缓冲区满时候的健壮性
- };
|