GrabBag/VrNets/TCPClient/_Inc/CVrTCPClient.h
2025-09-18 23:49:32 +08:00

105 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "IVrTCPClient.h"
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <chrono>
#ifdef _WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2def.h>
#include <ws2ipdef.h>
#include <IPHlpApi.h>
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "IPHlpApi.lib")
typedef SOCKET socket_t;
#define INVALID_SOCKET_VALUE INVALID_SOCKET
#define SOCKET_ERROR_VALUE SOCKET_ERROR
#define closesocket_func closesocket
#else
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <cstring>
typedef int socket_t;
#define INVALID_SOCKET_VALUE (-1)
#define SOCKET_ERROR_VALUE (-1)
#define closesocket_func close
#endif
#define MAX_BUF_LEN 2048
class CVrTCPClient : public IVrTCPClient
{
public:
CVrTCPClient();
~CVrTCPClient();
///
int LinkDevice(const std::string sDevIP, int nPort, bool bReLink, LinkEventFunc linkFunc, void* pParam) override;
/// ر豸
int CloseDevice() override;
/// ʼܹ
int StartWork(TCPRecvFunc fRecvFunc, void* pParam) override;
///
bool SendData(const char* pdata, const int nLen) override;
private:
struct DeviceConfig
{
std::string ip;
std::string mask;
std::string borad;
};
// ʼ
bool _Init();
//
void _RecvData();
// 豸
void _ReLinkDevThread();
// ӹ
int _ExecLinkDev(std::string sIP, int nPort);
void* m_pLinkParam;
void* m_pWorkParam;
socket_t m_nSocket;
bool m_bRecv;
bool m_bRecvWorking;
TCPRecvFunc m_fRecvCallback;
LinkEventFunc m_fLinkcCallback;
bool m_bLink;
std::string m_sIp;
int m_nPort;
std::mutex m_mutexRelink;
std::condition_variable m_condRelink;
};