83 lines
1.3 KiB
C
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();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @brief Init
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
virtual int Init(const bool bBoard);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>UDP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @brief StartUDPClient
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
virtual int StartUDPClient(UdpClientRecvDataFunc pRecvDataFunc);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UDP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @brief StopUDPClient
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
virtual int StopUDPClient();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @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;
|
|||
|
|
|
|||
|
|
|
|||
|
|
};
|