GrabBag/VrNets/UDPClient/_Inc/CYUDPClient.h
2026-07-11 15:49:14 +08:00

83 lines
1.3 KiB
C++

#pragma once
#include <atomic>
#include <cstdio>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SOCKET int
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#else
#include <WS2tcpip.h>
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32.lib")
#endif
#include "IYUDPClient.h"
#include "VrError.h"
#define IP_ADDR_LEN 16
class CUDPClient : public IYUDPClient
{
public:
CUDPClient();
~CUDPClient();
/**
* 初始化服务
* @brief Init
* @return
*/
virtual int Init(const bool bBoard);
/**
* 开始接收UDP数据
* @brief StartUDPClient
* @return
*/
virtual int StartUDPClient(UdpClientRecvDataFunc pRecvDataFunc);
/**
* 结束接收UDP数据
* @brief StopUDPClient
* @return
*/
virtual int StopUDPClient();
/**
* 发送数据
* @brief SendData
* @param pData
* @param nLen
* @return
*/
virtual int SendData(const int nPort, const char* pAddr, const char* pData, const int nLen);
private:
void _CloseSocket();
void _UdpRecvData();
private:
SOCKET m_sockfd;
sockaddr_in m_sin_from;
std::atomic_bool m_bRecv;
std::atomic_bool m_bRecvRuning;
UdpClientRecvDataFunc m_funcRecvData;
};