/
ReplicaPdus.h
162 lines (139 loc) · 3.68 KB
/
ReplicaPdus.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
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
#if !defined(daemon_ReplicaPdus_h)
#define daemon_ReplicaPdus_h
#include <inttypes.h>
#include <vector>
#include <boost/noncopyable.hpp>
#include <stdexcept>
#include <istat/Header.h>
#include <istat/Bucket.h>
#include "threadfunc.h"
#include "IStatStore.h"
#include "EagerConnection.h"
#include "Signal.h"
enum {
ReplicateProtocolVersion1 = 1
};
// PDU == Protocol Data Unit
struct PduHeader
{
uint32_t type;
uint32_t size;
std::vector<char> payload;
};
class PduOverflowException : public std::runtime_error
{
public:
PduOverflowException(std::string const &what) : std::runtime_error(what) {}
};
class PduWriter
{
public:
PduWriter(void *buf, size_t sz, size_t &oPos);
PduWriter &write(void const *src, size_t sz);
template<typename T> inline PduWriter &operator<<(T const &t)
{
write(&t, sizeof(t));
return *this;
}
private:
// can't write pointers
template<typename T> PduWriter &operator<<(T const *t);
void *buf_;
size_t sz_;
size_t &pos_;
};
template<> inline PduWriter &PduWriter::operator<< <std::string>(std::string const &s)
{
write(s.c_str(), s.size() + 1);
return *this;
}
template<uint32_t PduId>
struct PduBase
{
public:
enum { Id = PduId };
};
struct PduConnect : PduBase<1>
{
uint32_t Version;
UniqueId ServerId;
inline static PduConnect *make(char *buf, size_t sz, size_t &oPos, uint32_t version, UniqueId const &server)
{
PduWriter pw(buf, sz, oPos);
pw << version;
pw << server;
return (PduConnect *)buf;
}
};
struct PduDiscover : PduBase<2>
{
istat::Header header;
inline static PduDiscover *make(char *buf, size_t sz, size_t &oPos, istat::Header const &h)
{
PduWriter pw(buf, sz, oPos);
pw << h;
return (PduDiscover *)buf;
}
};
struct PduRequest : PduBase<3>
{
int64_t fromTime;
int64_t numBuckets;
// char name[1];
inline std::string getName() const { return std::string((char *)(&numBuckets+1)); }
inline static PduRequest *make(char *buf, size_t sz, size_t &oPos, int64_t fromTime, int64_t numBuckets, std::string const &name)
{
PduWriter pw(buf, sz, oPos);
pw << fromTime;
pw << numBuckets;
pw << name;
return (PduRequest *)buf;
}
};
struct PduData : PduBase<4>
{
int64_t numBuckets;
istat::Bucket buckets[1];
// char name[1]; // goes at end
inline std::string getName() const { return std::string((char *)(&buckets[numBuckets])); }
inline static PduData *make(char *buf, size_t sz, size_t &oPos, int64_t numBuckets, istat::Bucket const *buckets, std::string const &name)
{
PduWriter pw(buf, sz, oPos);
pw << numBuckets;
for (int64_t i = 0; i != numBuckets; ++i)
{
pw << buckets[i];
}
pw << name;
return (PduData *)buf;
}
};
struct PduError : PduBase<5>
{
// char text[1];
inline std::string getText() const { return std::string((char const *)this); }
inline static PduError *make(char *buf, size_t sz, size_t &oPos, std::string const &text)
{
PduWriter pw(buf, sz, oPos);
pw << text;
return (PduError *)buf;
}
};
class IEagerConnection;
class PduReaderActor : public boost::noncopyable
{
public:
PduReaderActor(boost::shared_ptr<ConnectionInfo> const &conn);
~PduReaderActor();
bool nextPdu(PduHeader &got);
boost::signals2::signal<void()> onPdu_;
boost::signals2::signal<void()> onDeleted_;
private:
void on_disconnect();
void on_data();
boost::shared_ptr<ConnectionInfo> conn_;
lock lock_;
std::list<PduHeader> pending_;
bool pduComplete_;
};
#endif // daemon_ReplicaPdus_h