#pragma once #include #define MAX_CLIENT_NUM 10000 struct TCPClient { int m_nFD; void* m_Task; }; enum TCPServerEventType { TCP_EVENT_CLIENT_CONNECTED, // 客户端连接 TCP_EVENT_CLIENT_DISCONNECTED, // 客户端断开 TCP_EVENT_CLIENT_EXCEPTION // 客户端异常 }; typedef std::function FunTCPServerRecv; typedef std::function FunTCPServerEvent; class IYTCPServer { public: virtual ~IYTCPServer() = default; public: ///初始化socket virtual bool Init(const int port, bool bOffNagle = false) = 0; ///初始化线程 virtual bool Start(FunTCPServerRecv fRecv, bool bRecvSelfProtocol = false) = 0; ///设置事件回调 virtual void SetEventCallback(FunTCPServerEvent fEvent) = 0; ///停止线程 virtual bool Stop() = 0; ///发送消息 virtual bool SendData(const TCPClient* pClient, const char* nBuf, const int nLen) = 0; ///发送给所有客户端 virtual bool SendAllData(const char* nBuf, const int nLen) = 0; ///接收数据 virtual bool RecvData(unsigned char** ppData, unsigned int* nLen) = 0; ///关闭 virtual bool Close() = 0; }; bool VrCreatYTCPServer(IYTCPServer** ppIYTCPServer);