-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.h
More file actions
266 lines (252 loc) · 8.85 KB
/
Copy pathdecoder.h
File metadata and controls
266 lines (252 loc) · 8.85 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#pragma once
#ifndef CLASSIFY_H
#define CLASSIFY_H
#include <iostream>
#include <memory>
#include "udp.h"
#include "protocol.h"
#include <string.h>
#define IP_LENGTH 4
#define PORT_LENGTH 2
class Decoder_Base
{
public:
//using UDP_Base_Ptr = std::shared_ptr<UDP_Base>;
// using UDP_Server_Ptr = std::shared_ptr<UDP_Server_Base>;
// enum NAT_TYPE
// {
// BEG = 0,
// NAT_SYMMETRIC = BEG, //对称NAT
// NAT_FULL_TAPERED, //完全锥形NAT
// NAT_IP_RESTRICTION_TAPER, //IP限制锥形NAT
// NAT_PORT_RESTRICTION_TAPER, //PORT限制锥形NAT
// END
// };
virtual void decode(UDP_Base::Ptr executor,const std::string &buffer,const UDP_Base::Addr_Ptr& caddr) = 0;
};
class Server_Decoder:public Decoder_Base
{
public:
using Ptr = std::shared_ptr<Server_Decoder>;
void decode(UDP_Base::Ptr executor,const std::string &buffer,const UDP_Base::Addr_Ptr& caddr) override
{
UDP_Server::Ptr server = std::dynamic_pointer_cast<UDP_Server>(executor);
Protocol_Head head;
memcpy(&head,buffer.c_str(),sizeof(Protocol_Head));
server->Add_Client_Addr(head.selfid,caddr);
std::string body = buffer.substr(sizeof(Protocol_Head));
switch (head.status)
{
case STATUS_LOGIN_REQ:
{
server->Add_Client_Addr(head.selfid,caddr);
deal_login_req(executor, caddr);
//std::cout<<inet_ntoa(server->Get_Client_Addr(head.selfid)->sin_addr)<<std::endl;
break;
}
case STATUS_CONN_REQ:
{
UDP_Base::Addr_Ptr other_addr = server->Get_Client_Addr(head.otherid);
std::cout<<head.otherid<<std::endl;
std::cout<<"other addr :"<<inet_ntoa(other_addr->sin_addr)<<std::endl;
deal_conn_req(executor,caddr,other_addr);
break;
}
case STATUS_NOTIFY_ACK:
{
break;
}
case STATUS_HEARTBEAT_REQ:
break;
case STATUS_NOTIFY_REQ:
break;
case STATUS_P2P_MESSAGE_REQ:
{
deal_p2p_msg_req(executor,caddr,body);
break;
}
default:
break;
}
}
private:
void deal_login_req(UDP_Base::Ptr executor,const UDP_Base::Addr_Ptr& caddr)
{
//std::cout<< inet_ntoa(caddr->sin_addr)<<std::endl;
UDP_Server::Ptr server = std::dynamic_pointer_cast<UDP_Server>(executor);
std::string buffer;
Protocol_Head head;
head.version = 1;
head.status = STATUS_LOGIN_ACK;
int hlen = sizeof(Protocol_Head);
//// std::cout<<hlen<<std::endl;
char* temp = new char[hlen];
memcpy(temp,&head,hlen);
buffer.assign(temp,hlen);
delete temp;
int len = server->Send_To(caddr,buffer);
if(len == -1) perror("sendto");
}
void deal_conn_req(UDP_Base::Ptr executor,const UDP_Base::Addr_Ptr& caddr, const UDP_Base::Addr_Ptr& other_addr)
{
UDP_Server::Ptr server = std::dynamic_pointer_cast<UDP_Server>(executor);
std::string msg;
Protocol_Head head;
head.version = 1;
int hlen= sizeof(Protocol_Head);
// std::cout<<hlen<<std::endl;
char* buffer = new char[hlen];
if (other_addr == nullptr)
{
head.status = STATUS_P2P_CONNECT_FAIL;
memcpy(buffer,&head,hlen);
msg.assign(buffer,hlen);
server->Send_To(caddr,msg);
return;
}
head.status = STATUS_CONN_ACK;
memcpy(&head.ip,&other_addr->sin_addr.s_addr,IP_LENGTH);
memcpy(&head.port,&other_addr->sin_port,PORT_LENGTH);
//std::cout<<head.ip<<" : "<<head.port<<std::endl;
memset(buffer,0,hlen);
memcpy(buffer,&head,hlen);
msg.assign(buffer,hlen);
server->Send_To(caddr,msg);
memcpy(&head.ip,&caddr->sin_addr.s_addr,IP_LENGTH);
memcpy(&head.port,&caddr->sin_port,PORT_LENGTH);
head.status = STATUS_NOTIFY_REQ;
//std::cout<<head.ip<<" : "<<head.port<<std::endl;
memset(buffer,0,hlen);
memcpy(buffer,&head,hlen);
msg.assign(buffer,hlen);
server->Send_To(other_addr,msg);
delete buffer;
}
void deal_p2p_msg_req(UDP_Base::Ptr executor,const UDP_Base::Addr_Ptr& caddr,std::string& body)
{
UDP_Server::Ptr server = std::dynamic_pointer_cast<UDP_Server>(executor);
std::cout<<inet_ntoa(caddr->sin_addr)<<" : "<<ntohs(caddr->sin_port)<<" msg :"<<body<<std::endl;
Protocol_Head head;
head.version = 1;
head.status = STATUS_P2P_MESSAGE_ACK;
int hlen = sizeof(Protocol_Head);
// std::cout<<hlen<<std::endl;
char* buffer = new char[hlen];
memcpy(buffer,&head,hlen);
std::string msg(buffer,hlen);
server->Send_To(caddr,msg);
}
};
class Client_Decoder:public Decoder_Base
{
public:
Client_Decoder()
{
this->_caddr = std::make_shared<sockaddr_in>();
this->_saddr = std::make_shared<sockaddr_in>();
P2P_Modle = false;
}
using UDP_Client_Ptr = std::shared_ptr<UDP_Client>;
void decode(UDP_Base::Ptr executor,const std::string& buffer,const UDP_Base::Addr_Ptr& caddr) override
{
UDP_Client_Ptr client = std::dynamic_pointer_cast<UDP_Client>(executor);
Protocol_Head head;
memcpy(&head,buffer.c_str(),sizeof(Protocol_Head));
std::string body = buffer.substr(sizeof(Protocol_Head));
int hlen = sizeof(Protocol_Head);
//// std::cout<<hlen<<std::endl;
switch (head.status)
{
case STATUS_NOTIFY_REQ:
{
Protocol_Head ret_head;
ret_head.version = 1;
ret_head.status = STATUS_NOTIFY_ACK;
char* temp = new char[hlen];
std::string str;
memcpy(temp,&ret_head,hlen);
str.assign(temp,hlen);
client->Send_To(_saddr,str);
ret_head.status = STATUS_P2P_CONNECT_REQ;
memcpy(&_caddr->sin_addr.s_addr,&head.ip,IP_LENGTH);
memcpy(&_caddr->sin_port,&head.port,PORT_LENGTH);
std::cout<<inet_ntoa(caddr->sin_addr)<<" : "<<ntohs(caddr->sin_port)<<" msg :"<<body<<std::endl;
_caddr->sin_family = AF_INET;
memset(temp,0,hlen);
memcpy(temp,&ret_head,hlen);
str.assign(temp,hlen);
client->Send_To(_caddr,str);
delete temp;
break;
}
case STATUS_P2P_CONNECT_REQ:
{
if(caddr->sin_addr.s_addr == _caddr->sin_addr.s_addr)
{
machine_status = MACHINE_STATUS_P2P_MODEL;
std::cout<<inet_ntoa(caddr->sin_addr)<<" : "<<ntohs(caddr->sin_port)<<" msg :"<<body<<std::endl;
Protocol_Head ret_head;
ret_head.version = 1;
ret_head.status = STATUS_P2P_CONNECT_ACK;
char* temp = new char[hlen];
memcpy(temp,&ret_head,hlen);
std::string str(temp,hlen);
client->Send_To(_caddr,str);
delete temp;
}
else{
Protocol_Head ret_head;
ret_head.version = 1;
ret_head.status = STATUS_P2P_CONNECT_FAIL;
char* temp = new char[hlen];
memcpy(temp,&ret_head,hlen);
std::string str(temp,hlen);
client->Send_To(_caddr,str);
delete temp;
}
break;
}
case STATUS_P2P_CONNECT_ACK:
{
machine_status = MACHINE_STATUS_P2P_MODEL;
std::cout<<"connect success"<<std::endl;
break;
}
case STATUS_P2P_CONNECT_FAIL:
{
std::cout<<"connect failed"<<std::endl;
break;
}
case STATUS_HEARTBEAT_ACK:
break;
case STATUS_CONN_ACK: //请求连接的ack
{
memcpy(&_caddr->sin_addr.s_addr,&head.ip,IP_LENGTH);
memcpy(&_caddr->sin_port,&head.port,PORT_LENGTH);
_caddr->sin_family = AF_INET;
std::cout<<"_caddr :" << inet_ntoa(caddr->sin_addr)<<std::endl;
break;
}
case STATUS_P2P_MESSAGE_REQ:
{
Protocol_Head ret_head;
ret_head.version = 1;
ret_head.status = STATUS_P2P_MESSAGE_ACK;
char* buffer = new char[hlen];
memcpy(buffer,&ret_head,hlen);
std::cout<<inet_ntoa(caddr->sin_addr)<<" : "<<ntohs(caddr->sin_port)<<" msg :"<<body<<std::endl;
std::string str(buffer,hlen);
client->Send_To(caddr,str);
break;
}
case STATUS_P2P_MESSAGE_ACK:
break;
default:
break;
}
}
UDP_Base::Addr_Ptr _saddr;
UDP_Base::Addr_Ptr _caddr;
bool P2P_Modle;
};
#endif