forked from truvorskameikin/udp-discovery-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.cpp
574 lines (476 loc) · 14.7 KB
/
endpoint.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include "protocol.hpp"
#include "endpoint.hpp"
// sockets
#if defined(_WIN32)
#define NOMINMAX
#include <winsock2.h>
#include <ws2tcpip.h>
typedef SOCKET SocketType;
typedef int AddressLenType;
const SocketType kInvalidSocket = INVALID_SOCKET;
#else
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
typedef int SocketType;
typedef socklen_t AddressLenType;
const SocketType kInvalidSocket = -1;
#endif
void InitSockets() {
#if defined(_WIN32)
WSADATA wsa_data;
WSAStartup(MAKEWORD(2, 2), &wsa_data);
#endif
}
void CloseSocket(SocketType sock) {
#if defined(_WIN32)
closesocket(sock);
#else
close(sock);
#endif
}
void SetSocketTimeout(SocketType sock, int param, int timeout_ms) {
#if defined(_WIN32)
setsockopt(sock, SOL_SOCKET, param, (const char *) &timeout_ms, sizeof(timeout_ms));
#else
struct timeval timeout;
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_usec = 1000 * (timeout_ms % 1000);
setsockopt(sock, SOL_SOCKET, param, (const char *) &timeout, sizeof(timeout));
#endif
}
// threads
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <stdlib.h>
#include <pthread.h>
#endif
void SleepFor(int time_ms) {
#if defined(_WIN32)
Sleep(time_ms);
#else
usleep(time_ms * 1000);
#endif
}
class MinimalisticMutex {
public:
MinimalisticMutex() {
#if defined(_WIN32)
InitializeCriticalSection(&critical_section_);
#else
pthread_mutex_init(&mutex_, 0);
#endif
}
~MinimalisticMutex() {
#if defined(_WIN32)
DeleteCriticalSection(&critical_section_);
#else
pthread_mutex_destroy(&mutex_);
#endif
}
void Lock() {
#if defined(_WIN32)
EnterCriticalSection(&critical_section_);
#else
pthread_mutex_lock(&mutex_);
#endif
}
void Unlock() {
#if defined(_WIN32)
LeaveCriticalSection(&critical_section_);
#else
pthread_mutex_unlock(&mutex_);
#endif
}
private:
#if defined(_WIN32)
CRITICAL_SECTION critical_section_;
#else
pthread_mutex_t mutex_;
#endif
};
// time
#if defined(__APPLE__)
#include <stdint.h>
#include <mach/mach_time.h>
#endif
#if !defined(_WIN32)
#include <sys/time.h>
#endif
#include <time.h>
static
long NowTime() {
#if defined(_WIN32)
LARGE_INTEGER freq;
if (!QueryPerformanceFrequency(&freq))
return 0;
LARGE_INTEGER cur;
QueryPerformanceCounter(&cur);
return (long) (cur.QuadPart * 1000 / freq.QuadPart);
#elif defined(__APPLE__)
mach_timebase_info_data_t time_info;
mach_timebase_info(&time_info);
uint64_t cur = mach_absolute_time();
return (long) ((cur / (time_info.denom * 1000000)) * time_info.numer);
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long) (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
#endif
return 0;
}
namespace udpdiscovery {
namespace impl {
uint64_t MakeRandomId() {
srand(time(0));
uint64_t r1 = rand();
uint64_t r2 = rand();
return ((r1 & 0xffffffff) < 32) | (r2 & 0xffffffff);
}
class EndpointEnv : public EndpointEnvInterface {
public:
EndpointEnv()
: sock_(kInvalidSocket),
packet_index_(0),
max_packet_index_(1073741824),
exit_(false) {
}
~EndpointEnv() {
CloseSocket(sock_);
}
bool Start(const EndpointParameters& parameters, const std::string& user_data) {
parameters_ = parameters;
user_data_ = user_data;
endpoint_id_ = MakeRandomId();
if (!parameters_.can_discover() && !parameters_.can_be_discovered()) {
std::cerr << "udpdiscovery::Endpoint can't discover and can't be discovered" << std::endl;
return false;
}
InitSockets();
sock_ = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_ == kInvalidSocket) {
std::cerr << "udpdiscovery::Endpoint can't create socket" << std::endl;
return false;
}
{
int value = 1;
setsockopt(sock_, SOL_SOCKET, SO_BROADCAST, (const char *) &value, sizeof(value));
}
if (parameters_.can_discover()) {
{
int value = 1;
setsockopt(sock_, SOL_SOCKET, SO_REUSEADDR, (const char *) &value, sizeof(value));
}
SetSocketTimeout(sock_, SO_RCVTIMEO, parameters_.receive_timeout_ms());
sockaddr_in addr;
memset((char *) &addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(parameters_.port());
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock_, (struct sockaddr *) &addr, sizeof(sockaddr_in)) < 0) {
CloseSocket(sock_);
sock_ = kInvalidSocket;
std::cerr << "udpdiscovery::Endpoint can't bind socket" << std::endl;
return false;
}
buffer_.resize(kMaxPacketSize);
}
return true;
}
void SetUserData(const std::string& user_data) {
lock_.Lock();
user_data_ = user_data;
lock_.Unlock();
}
std::list<DiscoveredEndpoint> ListDiscovered() {
std::list<DiscoveredEndpoint> result;
lock_.Lock();
result = discovered_endpoints_;
lock_.Unlock();
return result;
}
void Exit() {
lock_.Lock();
exit_ = true;
lock_.Unlock();
}
void DoWork() {
long last_send_time = 0;
while (true) {
lock_.Lock();
bool exit = exit_;
lock_.Unlock();
if (exit)
break;
if (parameters_.can_be_discovered()) {
long cur_time = NowTime();
if (last_send_time == 0) {
send(kPacketIAmHere);
last_send_time = cur_time;
} else {
if (cur_time - last_send_time > parameters_.send_timeout_ms()) {
send(kPacketIAmHere);
last_send_time = cur_time;
}
}
}
if (parameters_.can_discover()) {
receive();
deleteIdle(NowTime());
} else {
SleepFor(parameters_.receive_timeout_ms());
}
}
if (parameters_.can_be_discovered())
send(kPacketIAmOutOfHere);
}
private:
void receive() {
sockaddr_in from_addr;
AddressLenType addr_length = sizeof(sockaddr_in);
int length = (int) recvfrom(sock_, &buffer_[0], buffer_.size(), 0, (struct sockaddr *) &from_addr, &addr_length);
if (length <= 0)
return;
IpPort from;
from.set_port(ntohs(from_addr.sin_port));
from.set_ip(ntohl(from_addr.sin_addr.s_addr));
if (length >= sizeof(PacketHeader)) {
PacketHeader header;
if (ParsePacketHeader(buffer_.data(), sizeof(PacketHeader), header)) {
bool accept_packet = false;
if (parameters_.application_id() == header.application_id) {
if (!parameters_.discover_self()) {
if (header.peer_id != endpoint_id_)
accept_packet = true;
} else {
accept_packet = true;
}
}
if (accept_packet) {
std::string user_data(
buffer_.begin() + sizeof(PacketHeader), buffer_.begin() + sizeof(PacketHeader) + header.user_data_size);
lock_.Lock();
std::list<DiscoveredEndpoint>::iterator find_it = discovered_endpoints_.end();
for (std::list<DiscoveredEndpoint>::iterator it = discovered_endpoints_.begin(); it != discovered_endpoints_.end(); ++it) {
if (Same(parameters_.same_endpoint_mode(), (*it).ip_port(), from)) {
find_it = it;
break;
}
}
long cur_time = NowTime();
if (header.packet_type == kPacketIAmHere) {
if (find_it == discovered_endpoints_.end()) {
discovered_endpoints_.push_back(DiscoveredEndpoint());
discovered_endpoints_.back().set_ip_port(from);
discovered_endpoints_.back().SetUserData(user_data, header.packet_index);
discovered_endpoints_.back().set_last_updated(cur_time);
} else {
bool update_user_data = false;
if (header.packet_index_reset) {
update_user_data = true;
} else {
if ((*find_it).last_received_packet() < header.packet_index)
update_user_data = true;
}
if (update_user_data)
(*find_it).SetUserData(user_data, header.packet_index);
(*find_it).set_last_updated(cur_time);
}
} else if (header.packet_type == kPacketIAmOutOfHere) {
if (find_it != discovered_endpoints_.end()) {
discovered_endpoints_.erase(find_it);
}
}
lock_.Unlock();
}
}
}
}
void deleteIdle(long cur_time) {
lock_.Lock();
std::vector<std::list<DiscoveredEndpoint>::iterator> to_delete;
for (std::list<DiscoveredEndpoint>::iterator it = discovered_endpoints_.begin(); it != discovered_endpoints_.end(); ++it) {
if (cur_time - (*it).last_updated() > parameters_.discovered_endpoint_ttl_ms())
to_delete.push_back(it);
}
for (size_t i = 0; i < to_delete.size(); ++i)
discovered_endpoints_.erase(to_delete[i]);
lock_.Unlock();
}
void send(PacketType packet_type) {
lock_.Lock();
std::string user_data = user_data_;
lock_.Unlock();
PacketHeader header;
header.packet_type = packet_type;
header.application_id = parameters_.application_id();
header.peer_id = endpoint_id_;
header.packet_index = packet_index_;
if (header.packet_index >= max_packet_index_) {
packet_index_ = 0;
header.packet_index = 0;
header.packet_index_reset = 1;
}
++packet_index_;
std::string packet_data;
if (MakePacket(header, user_data, packet_data)) {
sockaddr_in addr;
memset((char *) &addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(parameters_.port());
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
sendto(sock_, &packet_data[0], packet_data.size(), 0, (struct sockaddr *) &addr, sizeof(sockaddr_in));
}
}
private:
EndpointParameters parameters_;
uint64_t endpoint_id_;
std::vector<char> buffer_;
SocketType sock_;
uint64_t packet_index_;
uint64_t max_packet_index_;
MinimalisticMutex lock_;
bool exit_;
std::string user_data_;
std::list<DiscoveredEndpoint> discovered_endpoints_;
};
class MinimalisticThread : public MinimalisticThreadInterface {
public:
#if defined(_WIN32)
MinimalisticThread(LPTHREAD_START_ROUTINE f, void* env) : detached_(false) {
thread_ = CreateThread(NULL, 0, f, env, 0, NULL);
}
#else
MinimalisticThread(void* (*f)(void*), void* env) : detached_(false) {
pthread_create(&thread_, 0, f, env);
}
#endif
~MinimalisticThread() {
Detach();
}
void Detach() {
if (detached_)
return;
#if defined(_WIN32)
CloseHandle(thread_);
#else
pthread_detach(thread_);
#endif
detached_ = true;
}
void Join() {
if (detached_)
return;
#if defined(_WIN32)
WaitForSingleObject(thread_, INFINITE);
CloseHandle(thread_);
#else
pthread_join(thread_, 0);
#endif
detached_ = true;
}
private:
bool detached_;
#if defined(_WIN32)
HANDLE thread_;
#else
pthread_t thread_;
#endif
};
#if defined(_WIN32)
DWORD WINAPI EndpointWork(void* env_typeless) {
EndpointEnv* env = (EndpointEnv*) env_typeless;
env->DoWork();
return 0;
}
#else
void* EndpointWork(void* env_typeless) {
EndpointEnv* env = (EndpointEnv*) env_typeless;
env->DoWork();
return 0;
}
#endif
};
Endpoint::Endpoint() : env_(0), thread_(0) {
}
Endpoint::~Endpoint() {
Stop(false);
}
bool Endpoint::Start(const EndpointParameters& parameters, const std::string& user_data) {
Stop(false);
impl::EndpointEnv* env = new impl::EndpointEnv();
if (!env->Start(parameters, user_data)) {
delete env;
env = 0;
return false;
}
impl::MinimalisticThread* thread = new impl::MinimalisticThread(impl::EndpointWork, env);
env_ = env;
thread_ = thread;
return true;
}
void Endpoint::SetUserData(const std::string& user_data) {
if (env_)
env_->SetUserData(user_data);
}
std::list<DiscoveredEndpoint> Endpoint::ListDiscovered() const {
std::list<DiscoveredEndpoint> result;
if (env_)
result = env_->ListDiscovered();
return result;
}
void Endpoint::Stop(bool wait_for_thread) {
if (!env_)
return;
env_->Exit();
env_ = 0;
if (wait_for_thread)
thread_->Join();
else
thread_->Detach();
delete thread_;
thread_ = 0;
}
bool Same(EndpointParameters::SameEndpointMode mode, const IpPort& lhv, const IpPort& rhv) {
switch (mode) {
case EndpointParameters::kSameEndpointIp:
return lhv.ip() == rhv.ip();
case EndpointParameters::kSameEndpointIpAndPort:
return (lhv.ip() == rhv.ip()) && (lhv.port() == rhv.port());
}
return false;
}
bool Same(EndpointParameters::SameEndpointMode mode, const std::list<DiscoveredEndpoint>& lhv, const std::list<DiscoveredEndpoint>& rhv) {
for (std::list<DiscoveredEndpoint>::const_iterator lhv_it = lhv.begin(); lhv_it != lhv.end(); ++lhv_it) {
std::list<DiscoveredEndpoint>::const_iterator in_rhv = rhv.end();
for (std::list<DiscoveredEndpoint>::const_iterator rhv_it = rhv.begin(); rhv_it != rhv.end(); ++rhv_it) {
if (Same(mode, (*lhv_it).ip_port(), (*rhv_it).ip_port())) {
in_rhv = rhv_it;
break;
}
}
if (in_rhv == rhv.end())
return false;
}
for (std::list<DiscoveredEndpoint>::const_iterator rhv_it = rhv.begin(); rhv_it != rhv.end(); ++rhv_it) {
std::list<DiscoveredEndpoint>::const_iterator in_lhv = lhv.end();
for (std::list<DiscoveredEndpoint>::const_iterator lhv_it = lhv.begin(); lhv_it != lhv.end(); ++lhv_it) {
if (Same(mode, (*rhv_it).ip_port(), (*lhv_it).ip_port())) {
in_lhv = lhv_it;
break;
}
}
if (in_lhv == lhv.end())
return false;
}
return true;
}
};