forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramedReader.cpp
More file actions
216 lines (173 loc) · 6.46 KB
/
FramedReader.cpp
File metadata and controls
216 lines (173 loc) · 6.46 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2004-present Facebook. All Rights Reserved.
#include "rsocket/framing/FramedReader.h"
#include <folly/io/Cursor.h>
#include "rsocket/framing/FrameSerializer_v0_1.h"
#include "rsocket/framing/FrameSerializer_v1_0.h"
namespace rsocket {
using namespace yarpl::flowable;
namespace {
constexpr size_t kFrameLengthFieldLengthV0_1 = sizeof(int32_t);
constexpr size_t kFrameLengthFieldLengthV1_0 = 3;
/// Get the byte size of the frame length field in an RSocket frame.
size_t frameSizeFieldLength(ProtocolVersion version) {
DCHECK_NE(version, ProtocolVersion::Unknown);
return version < FrameSerializerV1_0::Version ? kFrameLengthFieldLengthV0_1
: kFrameLengthFieldLengthV1_0;
}
/// Get the minimum size for a valid RSocket frame (including its frame length
/// field).
size_t minimalFrameLength(ProtocolVersion version) {
DCHECK_NE(version, ProtocolVersion::Unknown);
return version < FrameSerializerV1_0::Version
? FrameSerializerV0::kFrameHeaderSize + frameSizeFieldLength(version)
: FrameSerializerV1_0::kFrameHeaderSize;
}
/// Compute the length of the entire frame (including its frame length field),
/// if given only its frame length field.
size_t frameSizeWithLengthField(ProtocolVersion version, size_t frameSize) {
return version < FrameSerializerV1_0::Version
? frameSize
: frameSize + frameSizeFieldLength(version);
}
/// Compute the length of the frame (excluding its frame length field), if given
/// only its frame length field.
size_t frameSizeWithoutLengthField(ProtocolVersion version, size_t frameSize) {
DCHECK_NE(version, ProtocolVersion::Unknown);
return version < FrameSerializerV1_0::Version
? frameSize - frameSizeFieldLength(version)
: frameSize;
}
}
size_t FramedReader::readFrameLength() const {
auto fieldLength = frameSizeFieldLength(*version_);
DCHECK_GT(fieldLength, 0);
folly::io::Cursor cur{payloadQueue_.front()};
size_t frameLength = 0;
// Reading of arbitrary-sized big-endian integer.
for (size_t i = 0; i < fieldLength; ++i) {
frameLength <<= 8;
frameLength |= cur.read<uint8_t>();
}
return frameLength;
}
void FramedReader::onSubscribe(yarpl::Reference<Subscription> subscription) {
DuplexConnection::DuplexSubscriber::onSubscribe(subscription);
subscription->request(std::numeric_limits<int64_t>::max());
}
void FramedReader::onNext(std::unique_ptr<folly::IOBuf> payload) {
VLOG(4) << "incoming bytes length=" << payload->length() << '\n'
<< hexDump(payload->clone()->moveToFbString());
payloadQueue_.append(std::move(payload));
parseFrames();
}
void FramedReader::parseFrames() {
if (dispatchingFrames_) {
return;
}
// Delivering onNext can trigger termination and destroy this instance.
auto thisPtr = this->ref_from_this(this);
dispatchingFrames_ = true;
while (allowance_.canConsume(1) && inner_) {
if (!ensureOrAutodetectProtocolVersion()) {
// At this point we dont have enough bytes on the wire or we errored out.
break;
}
auto const frameSizeFieldLen = frameSizeFieldLength(*version_);
if (payloadQueue_.chainLength() < frameSizeFieldLen) {
// We don't even have the next frame size value.
break;
}
auto const nextFrameSize = readFrameLength();
if (nextFrameSize < minimalFrameLength(*version_)) {
error("Invalid frame - Frame size smaller than minimum");
break;
}
if (payloadQueue_.chainLength() <
frameSizeWithLengthField(*version_, nextFrameSize)) {
// Need to accumulate more data.
break;
}
payloadQueue_.trimStart(frameSizeFieldLen);
auto payloadSize = frameSizeWithoutLengthField(*version_, nextFrameSize);
DCHECK_GT(payloadSize, 0)
<< "folly::IOBufQueue::split(0) returns a nullptr, can't have that";
auto nextFrame = payloadQueue_.split(payloadSize);
CHECK(allowance_.tryConsume(1));
VLOG(4) << "parsed frame length=" << nextFrame->length() << '\n'
<< hexDump(nextFrame->clone()->moveToFbString());
inner_->onNext(std::move(nextFrame));
}
dispatchingFrames_ = false;
}
void FramedReader::onComplete() {
payloadQueue_.move();
DuplexConnection::DuplexSubscriber::onComplete();
if (auto subscriber = std::move(inner_)) {
// After this call the instance can be destroyed!
subscriber->onComplete();
}
}
void FramedReader::onError(folly::exception_wrapper ex) {
payloadQueue_.move();
DuplexConnection::DuplexSubscriber::onError({});
if (auto subscriber = std::move(inner_)) {
// After this call the instance can be destroyed!
subscriber->onError(std::move(ex));
}
}
void FramedReader::request(int64_t n) {
allowance_.add(n);
parseFrames();
}
void FramedReader::cancel() {
allowance_.consumeAll();
inner_ = nullptr;
}
void FramedReader::setInput(
yarpl::Reference<DuplexConnection::Subscriber> inner) {
CHECK(!inner_)
<< "Must cancel original input to FramedReader before setting a new one";
inner_ = std::move(inner);
inner_->onSubscribe(this->ref_from_this(this));
}
bool FramedReader::ensureOrAutodetectProtocolVersion() {
if (*version_ != ProtocolVersion::Unknown) {
return true;
}
auto minBytesNeeded = std::max(
FrameSerializerV0_1::kMinBytesNeededForAutodetection,
FrameSerializerV1_0::kMinBytesNeededForAutodetection);
DCHECK_GT(minBytesNeeded, 0);
if (payloadQueue_.chainLength() < minBytesNeeded) {
return false;
}
DCHECK_GT(minBytesNeeded, kFrameLengthFieldLengthV0_1);
DCHECK_GT(minBytesNeeded, kFrameLengthFieldLengthV1_0);
auto const& firstFrame = *payloadQueue_.front();
auto detected = FrameSerializerV1_0::detectProtocolVersion(
firstFrame, kFrameLengthFieldLengthV1_0);
if (detected != ProtocolVersion::Unknown) {
*version_ = FrameSerializerV1_0::Version;
return true;
}
detected = FrameSerializerV0_1::detectProtocolVersion(
firstFrame, kFrameLengthFieldLengthV0_1);
if (detected != ProtocolVersion::Unknown) {
*version_ = FrameSerializerV0_1::Version;
return true;
}
error("Could not detect protocol version from framing");
return false;
}
void FramedReader::error(std::string errorMsg) {
VLOG(1) << "error: " << errorMsg;
payloadQueue_.move();
if (DuplexConnection::DuplexSubscriber::subscription()) {
DuplexConnection::DuplexSubscriber::subscription()->cancel();
}
if (auto subscriber = std::move(inner_)) {
// After this call the instance can be destroyed!
subscriber->onError(std::runtime_error{std::move(errorMsg)});
}
}
}