-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
46 lines (43 loc) · 1.44 KB
/
server.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
#pragma once
#include "public_api.h"
using namespace std;
struct sockaddr_in6_compare {
bool operator()(const sockaddr_in6& lhs, const sockaddr_in6& rhs) const {
// Compare the port first
if (lhs.sin6_port != rhs.sin6_port) {
return lhs.sin6_port < rhs.sin6_port;
}
// Compare the address
return std::memcmp(&lhs.sin6_addr, &rhs.sin6_addr, sizeof(lhs.sin6_addr)) < 0;
}
};
class Server {
public:
Server(uint16_t serverPort, uint16_t remotePort);
~Server();
bool initUdpSockets(uint16_t remotePort);
int receiveUdpPacket();
int forwardUdpPacket();
int sendUdpPacketToCtrl();
int cleanUselessSockets();
inline int getUdpPacketQueueSize() { return udpPacketQueue.size(); }
int start();
int stop();
private:
unordered_set<SOCKET> udpSokcetConnectWithLocalHost;
map<SOCKET, int8_t> socketLifeCycle;
vector<SOCKET> udpSokcetsConnectWithRemoteHost;
sockaddr_in ctrlSockAddr;
SOCKET ctrlSocket;
unordered_set<SOCKET> udpSockets;
fd_set udpSokcetsFDSet;
condition_variable udpSokcetsFDSetCV;
sockaddr_in serverAddr;
queue<UdpPacketPtr> udpPacketQueue;
mutex udpPacketQueueMutex;
condition_variable udpPacketQueueCV;
map<sockaddr_in6, SOCKET, sockaddr_in6_compare> addrMapSocket;
map<SOCKET, sockaddr_in6> socketMapAddr;
bool keepRunning;
inline uint16_t getUniquePort(uint16_t port) { return (port - (port % 4)); }
};