-
Notifications
You must be signed in to change notification settings - Fork 0
/
ID.h
49 lines (37 loc) · 1.07 KB
/
ID.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
#ifndef __ID_H__
#define __ID_H__
#include <boost/asio/ip/udp.hpp>
// TODO: I'm not sure how safe it is to use endpoint as an ID yet.
// Will have to think about it, but right now it's the easiest
// option. If its only to be run on a local network, then it
// should be safe (?).
class ID {
using UDP = boost::asio::ip::udp;
public:
ID() {}
// For testing only
ID(unsigned short i) : ID(UDP::endpoint(UDP::v4(), i)) { }
ID(UDP::endpoint ep)
{
if (ep.address().is_unspecified()) {
ep.address(boost::asio::ip::address_v4::loopback());
}
endpoint = ep;
}
bool operator<(const ID& id) const {
return endpoint < id.endpoint;
}
bool operator==(const ID& id) const {
return endpoint == id.endpoint;
}
bool operator!=(const ID& id) const {
return endpoint != id.endpoint;
}
private:
friend std::ostream& operator<<(std::ostream&, const ID&);
boost::asio::ip::udp::endpoint endpoint;
};
inline std::ostream& operator<<(std::ostream& os, const ID& id) {
return os << id.endpoint.port();
}
#endif // ifndef __ID_H__