-
Notifications
You must be signed in to change notification settings - Fork 5
/
stream_id.cc
85 lines (78 loc) · 2.05 KB
/
stream_id.cc
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
#include <arpa/inet.h>
#include "stream_id.h"
/**
* Construct an StreamId from a gRPC address.
* \param gaddr
* The address of the peer for the RPC.
* \param id
* Unique id assigned to this RPC by the client.
*/
StreamId::StreamId(grpc_resolved_address *gaddr, uint32_t id)
: addr()
, id(id)
{
GPR_ASSERT(gaddr->len <= sizeof(addr));
memset(&addr, 0, sizeof(addr));
memcpy(&addr, gaddr->addr, gaddr->len);
}
/**
* Constructor for testing.
* \param id
* Identifier for this particular RPC.
*/
StreamId::StreamId(uint32_t id)
: addr()
, id(id)
{
addr.in6.sin6_family = AF_INET6;
addr.in6.sin6_addr = in6addr_any;
addr.in6.sin6_port = htons(40);
}
StreamId::StreamId(const StreamId &other)
{
addr = other.addr;
id = other.id;
}
StreamId& StreamId::operator=(const StreamId &other)
{
addr = other.addr;
id = other.id;
return *this;
}
/**
* Comparison operator for StreamIds.
* \param other
* StreamId to compare against
* \return
* True means the StreamIds match, false means they don't.
*/
bool StreamId::operator==(const StreamId& other) const
{
if (addr.in6.sin6_family == AF_INET6) {
return (id == other.id) &&
(bcmp(&addr.in6, &other.addr.in6, sizeof(addr.in6)) == 0);
} else {
return (id == other.id) &&
(bcmp(&addr.in4, &other.addr.in4, sizeof(addr.in4)) == 0);
}
}
/**
* Return a string containing all of the information in this struct.
*/
std::string StreamId::toString() {
char buf[INET6_ADDRSTRLEN];
const char *ntop_result = nullptr;
if (addr.in6.sin6_family == AF_INET6) {
ntop_result = inet_ntop(AF_INET6, &addr.in6.sin6_addr, buf, sizeof(buf));
} else if (addr.in4.sin_family == AF_INET) {
ntop_result = inet_ntop(AF_INET, &addr.in4.sin_addr, buf, sizeof(buf));
}
if (!ntop_result) {
return "invalid address";
}
char buf2[50];
snprintf(buf2, sizeof(buf2), ":%d, stream id %d", port(), id);
std::string result(buf);
result.append(buf2);
return result;
}