GrabBag/VrNets/tcpServer/Inc/IYTCPServer.h
2025-09-14 14:51:38 +08:00

57 lines
1.3 KiB
C++

#pragma once
#include <functional>
#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<void (const TCPClient* pClient, const char* pData, const unsigned int nLen)> FunTCPServerRecv;
typedef std::function<void (const TCPClient* pClient, TCPServerEventType eventType)> 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);