forked from qualisys/qualisys_cpp_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.h
70 lines (58 loc) · 1.9 KB
/
Network.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef NETWORK_H
#define NETWORK_H
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#else
#define INVALID_SOCKET -1
#define SOCKET int
#endif
#include <vector>
class CNetwork
{
public:
enum class ResponseType
{
success,
timeout,
disconnect,
error
};
struct Response
{
int received;
ResponseType type;
Response(ResponseType type_, int received_) : received(received_), type(type_) {}
operator bool() { return type == ResponseType::success; }
operator ResponseType() { return type; }
};
CNetwork();
~CNetwork();
bool Connect(const char* pServerAddr, unsigned short nPort);
void Disconnect();
bool Connected() const;
bool CreateUDPSocket(unsigned short &nUDPPort, bool bBroadcast = false);
Response Receive(char* rtDataBuff, int nDataBufSize, bool bHeader, int timeoutMicroseconds, unsigned int *ipAddr = nullptr);
Response ReceiveUdpBroadcast(char* rtDataBuff, int nDataBufSize, int timeoutMicroseconds, unsigned int *ipAddr = nullptr);
bool Send(const char* pSendBuf, int nSize);
bool SendUDPBroadcast(const char* pSendBuf, int nSize, short nPort, unsigned int nFilterAddr = 0);
char* GetErrorString();
int GetError() const;
bool IsLocalAddress(unsigned int nAddr) const;
unsigned short GetUdpServerPort();
unsigned short GetUdpBroadcastServerPort();
private:
Response Receive(SOCKET socket, SOCKET udpSocket, char* rtDataBuff, int nDataBufSize, bool bHeader, int timeoutMicroseconds, unsigned int *ipAddr = nullptr);
bool InitWinsock();
void SetErrorString();
unsigned short GetUdpServerPort(SOCKET nSocket);
private:
SOCKET mSocket;
SOCKET mUDPSocket;
SOCKET mUDPBroadcastSocket;
char mErrorStr[256];
unsigned long mLastError;
};
#endif