-
Notifications
You must be signed in to change notification settings - Fork 1
/
P2P.h
145 lines (119 loc) · 3.43 KB
/
P2P.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include "Network.h"
#include "Utils.h"
#include <uuids.h>
#include <thread>
#include <future>
#include <atomic>
#include <list>
#include <queue>
using namespace std;
#define NT_FLAG_IP_REQ 0b01
#define NT_FLAG_IP_RES 0b10
constexpr int ttl_default = 30;
constexpr int port_router = 511;
constexpr int NetMaxChild = min(WSA_MAXIMUM_WAIT_EVENTS, 5);
constexpr int NetTotalCount = NetMaxChild+2;
/*
Router
-소켓 리스닝 (스레드 1)-비동기 작업
이미 리스트가 포화인 경우 리스트 요소중 하나로 연결을 토스한다
-브로드 캐스팅 (스레드 2)
-TTL확인
-전송
-연결 관리 (스레드 2)
-주기적으로 끊어진 소켓 유뮤 확인 (참->제거)
-주변 노드와 비교해서 자식 수 2 이상 차이나는 자신의 노드를 노드에게 이전
-노드에게 이전 요청을 보내고 수락 문자 받은 후 연결 해제
-수락 노드는 연결 해제후 요청의 주소 중 하나로 연결한다
*/
class NetRouter {
private:
atomic_bool isRunning;
atomic_uint8_t cntChild;
thread routing, tasking, subtasking;
TcpListener listener;
SOCKET parent;
ULONG parentid;
ULONG rep_addr;
AtomicMinimum<BYTE, ULONG> orderer;
struct SOCKETIN {
int needSize = -1;
BYTE cntChild = NetMaxChild;
SOCKET socket;
ULONG id;
RecyclerBuffer<256> buffer;
};
mutex m_modifyLock;
SequentArrayList<WSAEVENT, NetTotalCount> m_arrEvt;
SequentArrayList<SOCKETIN, NetTotalCount> m_arrIn;
struct PacketPair {
ULONG id;
CBuffer buffer;
};
mutex m_packetLock;
queue<PacketPair> m_packetBuffer;
mutex m_outputLock;
condition_variable cv;
atomic_bool newdata;
queue<PacketPair> m_outputBuffer;
private:
void Stacking();
void Tasking();
void SubTasking();
void ClearStacking();
void StopListening();
void CalculateOptimalNode();
void ConnectToParent(ULONG addr);
void BroadCastToNodes(CBuffer& packet, const ULONG& filter);
void OutputQueue(PacketPair&& packet);
void AddEvent(SOCKET s, WSAEVENT e, ULONG addr);
void AddListenEvent(SOCKET s, WSAEVENT e, ULONG addr);
void RemoveEvent(int idx);
public:
NetRouter(ULONG _parent);
void Start();
void Stop();
};
class NetHost {
public:
};
enum class NetPacketProtocol : BYTE {
REPLACE_ADDR,
QUERY,
CHILDCOUNT
};
enum class NetPacketFlag : BYTE {
CONN,
DISCONN,
REFUSE
};
struct NetPacketHeader {
NetPacketProtocol protocol;
NetPacketFlag flag;
};
class PacketFactory {
public:
static constexpr auto szHeader = sizeof(NetPacketHeader);
static auto NetPacketBuffer(const NetPacketHeader& header, const CBuffer& content) {
CBuffer buffer(static_cast<int>(szHeader + content.size()));
memcpy(buffer.getBuffer(), &header, szHeader);
memcpy(buffer.getBuffer() + szHeader, content.getBuffer(), content.size());
return buffer;
}
static auto NetPacketIP(ULONG ip, NetPacketFlag flag=NetPacketFlag::CONN) {
CBuffer buffer(static_cast<int>(szHeader + sizeof(ULONG)));
static NetPacketHeader header{NetPacketProtocol::REPLACE_ADDR, NetPacketFlag::CONN};
header.flag = flag;
memcpy(buffer.getBuffer(), &header, szHeader);
memcpy(buffer.getBuffer() + szHeader, &ip, sizeof(ULONG));
return buffer;
}
static auto NetPacketChildCnt(BYTE cnt) {
CBuffer buffer(static_cast<int>(szHeader + 1));
static constexpr NetPacketHeader header{ NetPacketProtocol::CHILDCOUNT, NetPacketFlag::CONN };
memcpy(buffer.getBuffer(), &header, szHeader);
*reinterpret_cast<BYTE*>(buffer.getBuffer() + szHeader) = cnt;
return buffer;
}
};