-
Notifications
You must be signed in to change notification settings - Fork 5
/
wire.cc
103 lines (97 loc) · 3.22 KB
/
wire.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <arpa/inet.h>
#include <string.h>
#include <optional>
#include <string>
#include "util.h"
#include "wire.h"
/**
* Print to the log the contents of a block of metadata, as serialized
* by appendMetadata.
* \param buffer
* First byte of serialized data.
* \param length
* Total amount of serialized data.
* \param severity
* Log severity level to use for messages.
*/
void Wire::dumpMetadata(void *buffer, size_t length, gpr_log_severity severity)
{
size_t remaining = length;
uint8_t *src = static_cast<uint8_t *>(buffer);
// Each iteration prints one metadata value
while (remaining > 0) {
Wire::Mdata* msgMd = reinterpret_cast<Wire::Mdata*>(src);
if (remaining < sizeof(*msgMd)) {
gpr_log(__FILE__, __LINE__, severity, "Not enough bytes for "
"metadata header: need %lu, have %lu",
sizeof(*msgMd), remaining);
return;
}
uint32_t keyLength = ntohl(msgMd->keyLength);
uint32_t valueLength = ntohl(msgMd->valueLength);
remaining -= sizeof(*msgMd);
src += sizeof(*msgMd);
if (remaining < (keyLength + valueLength)) {
gpr_log(__FILE__, __LINE__, severity, "Not enough bytes for "
"key and value: need %u, have %lu",
keyLength + valueLength, remaining);
return;
}
gpr_log(__FILE__, __LINE__, severity,
"Key: %.*s, value: %.*s", keyLength, src, valueLength,
src+keyLength);
remaining -= keyLength + valueLength;
src += keyLength + valueLength;
}
}
/**
* Log information about the contents of a Homa message header.
* \param msg
* Address of the first byte of a Homa message (expected to contain
* a valid header).
* \param severity
* Log severity level to use for messages.
*/
void Wire::dumpHeader(void *msg, gpr_log_severity severity)
{
Wire::Header *hdr = static_cast<Wire::Header *>(msg);
std::string s;
char buffer[100];
snprintf(buffer, sizeof(buffer), "id: %u, sequence %u",
ntohl(hdr->streamId), ntohl(hdr->sequenceNum));
s.append(buffer);
if (hdr->initMdBytes) {
snprintf(buffer, sizeof(buffer), ", initMdBytes %u",
ntohl(hdr->initMdBytes));
s.append(buffer);
}
if (hdr->messageBytes) {
snprintf(buffer, sizeof(buffer), ", messageBytes %u",
ntohl(hdr->messageBytes));
s.append(buffer);
}
if (hdr->trailMdBytes) {
snprintf(buffer, sizeof(buffer), ", trailMdBytes %u",
ntohl(hdr->trailMdBytes));
s.append(buffer);
}
if (hdr->flags & Header::initMdPresent) {
s.append(", initMdPresent");
}
if (hdr->flags & Header::messageComplete) {
s.append(", messageComplete");
}
if (hdr->flags & Header::trailMdPresent) {
s.append(", trailMdPresent");
}
if (hdr->flags & Header::request) {
s.append(", request");
}
if (hdr->flags & Header::emptyResponse) {
s.append(", emptyResponse");
}
if (hdr->flags & Header::cancelled) {
s.append(", cancelled");
}
gpr_log(__FILE__, __LINE__, severity, "Header: %s", s.c_str());
}