101 lines
1.8 KiB
C++
101 lines
1.8 KiB
C++
#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) override;
|
||
|
||
/// ر豸
|
||
int CloseDevice() override;
|
||
|
||
/// ʼܹ
|
||
int StartWork(TCPRecvFunc fRecvFunc) 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);
|
||
|
||
|
||
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;
|
||
|
||
};
|
||
|
||
|