#pragma once #include #include #include #include #include #ifndef _WIN32 #include #include #endif // !_WIN32 #include "IYTCPServer.h" typedef std::function FunTCPServerRecv; #define RECV_DATA_LEN 12 * 1024 * 1024 #ifdef _WIN32 #define FD_SETSIZE 1024 #define _WINSOCK_DEPRECATED_NO_WARNINGS #include #include #pragma comment(lib, "ws2_32.lib") #else #define SOCKET int #define INVALID_SOCKET (SOCKET)(~0) #define SOCKET_ERROR (-1) #endif enum WORK_STATUS { WORK_INIT, WORK_RUNING, WORK_WAITSINGAL, WORK_CLOSE, WORK_EXIT, }; class CYServerTask { public: CYServerTask(); ~CYServerTask(); ///��ʼ���� ///开始线程 ///开始任务 bool StartTask(FunTCPServerRecv fRecv, bool bRecvSelfProtocol = false); ///设置异常处理回调(给CYTCPServer使用) void SetExceptionCallback(std::function fException); ///ֹͣ���� bool StopTask(); ///���ӿͻ��� bool AddClient(TCPClient* pClient); ///�Ƴ��ͻ��� bool DelClient(const TCPClient* pClient); ///��ȡTask�пͻ��˵���Ŀ int GetClientNum(); private: void _OnProcessEvent(); bool _OnProcessData(TCPClient* pClient); private: ///客户端存储vector std::mutex m_mClient; std::vector m_vClient; ///select 监听 注释:和m_vClient使用同一把锁 int m_maxSocket; fd_set m_fdRead; fd_set m_fdExp; ///线程管理 std::thread* m_tTask; bool m_bWork; WORK_STATUS m_eWorkStatus; //工作信号 std::mutex m_mutexWork; std::condition_variable m_cvWork; //处理回调[接口] private: //处理回调[接口] FunTCPServerRecv m_fRecv; std::function m_fException; char* m_pRecvBuf; std::atomic m_bUseProtocol; struct ProtocolHead { int nHead; int nTime; int nCmd; int nDef; int nLen; int nTail; char pData[RECV_DATA_LEN]; }; ProtocolHead* m_pProtocalHead; };