forked from nathanielherman/silo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_common.h
63 lines (59 loc) · 1.33 KB
/
stats_common.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include "counter.h"
#include "macros.h"
#include "fileutils.h"
enum class stats_command : uint8_t { GET_COUNTER_VALUE = 0x1 };
struct get_counter_value_t {
uint64_t timestamp_us_; // usec
counter_data d_;
};
class packet {
public:
static const size_t MAX_DATA = 0xFFFF - 4;
packet() : size_(0) {}
inline void clear() { size_ = 0; }
inline void
assign(const char *p, size_t n)
{
INVARIANT(n <= MAX_DATA);
NDB_MEMCPY(&data_[0], p, n);
size_ = n;
}
inline void
assign(const std::string &s)
{
assign(s.data(), s.size());
}
int
sendpkt(int fd) const
{
// XXX: we don't care about endianness
return fileutils::writeall(
fd, (const char *) &size_, sizeof(size_) + size_);
}
int
recvpkt(int fd)
{
// XXX: we don't care about endianness
int r;
if ((r = fileutils::readall(fd, (char *) &size_, sizeof(size_)))) {
clear();
return r;
}
if (size_ > packet::MAX_DATA) {
std::cerr << "bad packet read with excessive size" << std::endl;
clear();
return -1;
}
if ((r = fileutils::readall(fd, &data_[0], size_))) {
clear();
return r;
}
return 0;
}
inline uint32_t size() const { return size_; }
inline const char * data() const { return &data_[0]; }
private:
uint32_t size_;
char data_[MAX_DATA];
};