GrabBag/VrNets/UDPServer/Inc/IYUDPServer.h
2026-07-11 15:49:14 +08:00

64 lines
1.1 KiB
C++

#pragma once
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <thread>
#include <functional>
#include <chrono>
#ifdef _WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib, "UDPServer.lib")
#else
#include <sys/socket.h>
#include <netinet/in.h>
#endif
typedef std::function<void(const sockaddr_in clientPeer, const char* pData, const int nLen)> UdpRecvDataFunc;
class IYUDPServer
{
public:
virtual ~IYUDPServer() = default;
/**
* 创建对象
*/
static bool VrCreateUDPServer(IYUDPServer** ppIUDPServer);
/**
* 初始化服务
* @brief Init
* @return
*/
virtual int Init(const int nPort) = 0;
/**
* 开始接收UDP数据
* @brief StartUDPServer
* @return
*/
virtual int StartUDPServer(UdpRecvDataFunc pRecvDataFunc, bool bRecvSelfProtocol) = 0;
/**
* 结束接收UDP数据
* @brief StopUDPServer
* @return
*/
virtual int StopUDPServer() = 0;
/**
* 发送数据
* @brief SendData
* @param pData
* @param nLen
* @return
*/
virtual int SendData(const sockaddr_in clientPeer, const char* pData, const int nLen) = 0;
};