#pragma once #include #include #include #include "VrError.h" #ifndef _WIN32 #include #include #endif // !_WIN32 #include "IYTCPServer.h" #include "CYServerTask.h" #define MAX_LINK_NUM 10000 class CYTCPServer : public IYTCPServer { public: CYTCPServer(); ~CYTCPServer(); public: ///初始化socket virtual bool Init(const int port, bool bOffNagle = false); ///初始化线程 virtual bool Start(FunTCPServerRecv fRecv, bool bRecvSelfProtocol = false); ///设置事件回调 virtual void SetEventCallback(FunTCPServerEvent fEvent); ///停止线程 virtual bool Stop(); ///发送消息 virtual bool SendData(const TCPClient* pClient, const char* nBuf, const int nLen); ///发送给所有客户端 virtual bool SendAllData(const char* nBuf, const int nLen); ///接收数据 virtual bool RecvData(unsigned char** ppData, unsigned int* nLen); ///关闭 virtual bool Close(); private: WORK_STATUS m_eWorkStats; private: /// 回调函数 FunTCPServerRecv m_fRecv; FunTCPServerEvent m_fEvent; SOCKET m_nSocket; std::mutex m_mVectorSocket; std::vector m_vTCPClient; std::atomic m_bWork; std::mutex m_mSocketMutex; std::mutex m_mutexRecv; std::condition_variable m_cvRecv; bool m_bCreateRecv; ///管理Task std::mutex m_mVectorTask; std::vector m_vServerTask; ///tcp 算法优化 bool m_bOffNagle; private: /// 记录客户端 void _AddClient(const SOCKET nSocket, const sockaddr_in sClientAddr); /// 关闭客户端 void _CloseClient(const TCPClient* pClient); /// 监控连接 void _OnMonitorLink(); /// 处理异常 void _Exception(const TCPClient* pClient); };