-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.cpp
357 lines (296 loc) · 7.6 KB
/
net.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "net.h"
NET::~NET() {
#ifdef _WIN32
WSACleanup();
#endif
}
NET::NET(int port, int udp_port) {
//Set exit signal
signal(SIGABRT,&NET::Cleanup);
signal(SIGFPE, &NET::Cleanup);
signal(SIGILL, &NET::Cleanup);
signal(SIGSEGV,&NET::Cleanup);
signal(SIGINT, &NET::Cleanup);
signal(SIGTERM,&NET::Cleanup);
#ifdef _WIN32
//WSA init
WSAStartup(
MAKEWORD(2, 0),
&wsaData
);
#endif //_WIN32
//Socket init
sock = socket(
AF_INET,
SOCK_STREAM,
IPPROTO_TCP
);
//TCP init
srand(static_cast<int>(time(nullptr)));
if (port == 0)
PORT = 50000 + rand() % 10000;
else
this->PORT = port;
std::string msg; //Show the rand Port
msg = "TCP port > " + std::to_string(PORT);
Show_log(_MSG, msg);
sock_addr.sin_port = htons(PORT);
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Bind
err = bind(
sock,
(sockaddr*)&sock_addr,
sizeof(sock_addr)
);
if (err == SOCKET_ERROR) {
Show_log(_ERROR, "bind error");
exit(-1);
}
Show_log(_MSG, "Create the socket successfully");
err = listen(
sock,
20 //backlog
);
Show_log(_MSG, "Listening by using backlog 20");
//init else
srand((UINT)time(nullptr));
//setup udp module
if (udp_port == 0)
udp.up(PORT + 1);
else
udp.up(udp_port);
}
void NET::Cleanup(int signum) {
#ifdef _WIN32
WSACleanup();
#endif // _WIN32
exit(signum);
}
void NET::service() {
//set some common val
std::string str;
recv_async_struct *new_async;
device_struct* newDevice;
new_async = new recv_async_struct;
newDevice = new device_struct;
new_async->device = newDevice;
//Set the size of the sockaddr size
#ifdef _WIN32
int size_of_struct = sizeof(newDevice->sock_addr);
#else
socklen_t size_of_struct = sizeof(newDevice->sock_addr);
#endif
while (is_service_on) {
//add a new device
device_list.push_back(newDevice);
//set a unique ID
newDevice->ID = IDgenerator.generateID();
//accept the request
newDevice->sock = accept(
sock,
(sockaddr*)&newDevice->sock_addr,
&size_of_struct
);
/*str.Format("Accept return value > %d", err);
Show_log(_DEBU, str);*/
//Add the async to the list
new_async->futu = std::async(
std::launch::async,
&::NET::auto_print_recv,
this,
newDevice
);
//Add into the list
auto_receive_list.push_back(new_async);
str = "New device[ ID > " + std::to_string(newDevice->ID) + " ] added";
Show_log(_MSG, str);
new_async = new recv_async_struct;
newDevice = new device_struct;
new_async->device = newDevice;
}
}
NET& NET::send(const char* message, unsigned long ID) {
int err;
std::string str;
//Get the device
device_struct *this_device = nullptr;
for ( auto temp : device_list) {
if (temp->ID == ID) {
this_device = temp;
break;
}
}
if (this_device == nullptr)
return *this;
//Send a message to the address
err = ::send(
this_device->sock,
message,
(int)strlen(message),
0
);
if (err == SOCKET_ERROR) {
#ifdef _WIN32
str = "send error,WSA error code is " + std::to_string(WSAGetLastError());
Show_log(_ERROR, str);
#else
Show_log(_ERROR, "send message error");
#endif
return *this;
}
Show_log(_MSG, "send success");
return *this;
}
void NET::auto_print_recv(device_struct* this_device) {
//Set data mem
char data[4096];
int data_size = 0;
std::string str;
//Get the recv data and print it
while (data_size = recv(
this_device->sock,
data,
sizeof(data),
0
)) {
if (data_size == SOCKET_ERROR) {
#ifdef _WIN32
str = "Socket error , error code is > " + std::to_string(WSAGetLastError());
Show_log(_ERROR, str);
#else
Show_log(_ERROR, "Socket error");
#endif
goto dis;
}
data[data_size] = '\0';
//Show_log(_RECV, data);
this_device->data.data_CS.push_back(data);
}
dis:
//shut it down
disconnect(this_device->ID);
}
unsigned long NET::connect(const char* addr, UINT port) {
std::string str;
//setup a new connection
sockaddr_in connect_sockaddr;
connect_sockaddr.sin_addr.s_addr = inet_addr(addr);
connect_sockaddr.sin_port = htons(port);
connect_sockaddr.sin_family = AF_INET;
//add to the devices' list
connect_device* device = new connect_device;
device->sock = socket(
AF_INET,
SOCK_STREAM,
IPPROTO_TCP
);
device->sock_addr = connect_sockaddr;
device->ID = IDgenerator.generateID();
//connect the target device
int err = ::connect(
device->sock,
(sockaddr*)&device->sock_addr,
sizeof(device->sock_addr)
);
if (err == SOCKET_ERROR) {
#ifdef _WIN32
str = "socket error when connecting a device , WSA error code is " +
std::to_string(WSAGetLastError());
Show_log(_ERROR, str);
#else
Show_log(_ERROR, "socket error when connecting a device");
#endif
return 0;
}
device_list.push_back(device);
str = "New device connect [ ID > " + std::to_string(device->ID) + " ]";
Show_log(_MSG, str);
//add it in to auto recv list
recv_async_struct* new_async = new recv_async_struct;
new_async->device = device;
new_async->futu = std::async(
std::launch::async,
&::NET::auto_print_recv,
this,
device_list[find_device(device->ID, device_list)]
);
auto_receive_list.push_back(new_async);
return device->ID;
}
void NET::disconnect(unsigned long ID) {
std::string str;
int device_no;
Show_log(_DEBU, "disconnecting a device");
this->mtx.lock();
if (( device_no = find_device(ID, device_list) ) != -1) {
device_struct* device = device_list[device_no];
#ifdef _WIN32
shutdown(
device->sock,
SD_BOTH
);
#else
shutdown(
device->sock,
SHUT_RDWR
);
#endif
str = "a device disconnect [ device ID > " + std::to_string(device->ID) + " ]";
Show_log(_MSG, str);
int auto_recv_list_no = find_device(device->ID, auto_receive_list);
recv_async_struct* del_async = auto_receive_list[auto_recv_list_no];
device->data.data_bin.erase(device->data.data_bin.begin(),device->data.data_bin.end());
device->data.data_CS.erase(device->data.data_CS.begin(), device->data.data_CS.end());
IDgenerator.deleteID(del_async->device->ID);
delete del_async->device;
del_async->device = nullptr;
//delete auto_receive_list[auto_recv_list_no];
//auto_receive_list[auto_recv_list_no] = nullptr;
auto_receive_list.erase(
auto_receive_list.begin() + auto_recv_list_no
);
device_list.erase(device_list.begin() + device_no);
this->mtx.unlock();
device = nullptr;
delete device;
delete del_async;
return;
}
this->mtx.unlock();
}
int NET::find_device(unsigned long ID, std::vector<device_struct*> list) {
int device_num = 0;
for (auto dev : list) {
if (dev->ID == ID) {
return device_num;
}
device_num++;
}
return -1;
}
int NET::find_device(unsigned long ID, std::vector<recv_async_struct*> list) {
int device_num = 0;
for (auto dev : list) {
if (dev->device->ID == ID) {
return device_num;
}
device_num++;
}
return -1;
}
char* NET::domain_to_IP(char* domain) {
struct addrinfo hints ,*res;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(domain, nullptr, &hints, &res)) {
Show_log(_ERROR, "can not resolve this domain > " + std::string(domain));
return nullptr;
}
return inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr);
}
char* NET::domain_to_IP(std::string domain) {
const char* domain_c = domain.c_str();
return domain_to_IP(const_cast<char*>(domain_c));
}