forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramer.cpp
More file actions
204 lines (169 loc) · 6.24 KB
/
Framer.cpp
File metadata and controls
204 lines (169 loc) · 6.24 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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "rsocket/framing/Framer.h"
#include <folly/io/Cursor.h>
#include "rsocket/framing/FrameSerializer_v1_0.h"
namespace rsocket {
namespace {
constexpr size_t kFrameLengthFieldLengthV1_0 = 3;
constexpr auto kMaxFrameLength = 0xFFFFFF; // 24bit max value
template <typename TWriter>
void writeFrameLength(
TWriter& cur,
size_t frameLength,
size_t frameSizeFieldLength) {
DCHECK(frameSizeFieldLength > 0);
// starting from the highest byte
// frameSizeFieldLength == 3 => shift = [16,8,0]
// frameSizeFieldLength == 4 => shift = [24,16,8,0]
auto shift = (frameSizeFieldLength - 1) * 8;
while (frameSizeFieldLength--) {
const auto byte = (frameLength >> shift) & 0xFF;
cur.write(static_cast<uint8_t>(byte));
shift -= 8;
}
}
} // namespace
/// Get the byte size of the frame length field in an RSocket frame.
size_t Framer::frameSizeFieldLength() const {
DCHECK_NE(protocolVersion_, ProtocolVersion::Unknown);
if (protocolVersion_ < FrameSerializerV1_0::Version) {
return sizeof(int32_t);
} else {
return 3; // bytes
}
}
/// Get the minimum size for a valid RSocket frame (including its frame length
/// field).
size_t Framer::minimalFrameLength() const {
DCHECK_NE(protocolVersion_, ProtocolVersion::Unknown);
return FrameSerializerV1_0::kFrameHeaderSize;
}
/// Compute the length of the entire frame (including its frame length field),
/// if given only its frame length field.
size_t Framer::frameSizeWithLengthField(size_t frameSize) const {
return protocolVersion_ < FrameSerializerV1_0::Version
? frameSize
: frameSize + frameSizeFieldLength();
}
/// Compute the length of the frame (excluding its frame length field), if given
/// only its frame length field.
size_t Framer::frameSizeWithoutLengthField(size_t frameSize) const {
DCHECK_NE(protocolVersion_, ProtocolVersion::Unknown);
return protocolVersion_ < FrameSerializerV1_0::Version
? frameSize - frameSizeFieldLength()
: frameSize;
}
size_t Framer::readFrameLength() const {
const auto fieldLength = frameSizeFieldLength();
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 Framer::addFrameChunk(std::unique_ptr<folly::IOBuf> payload) {
payloadQueue_.append(std::move(payload));
parseFrames();
}
void Framer::parseFrames() {
if (payloadQueue_.empty() || !ensureOrAutodetectProtocolVersion()) {
// At this point we dont have enough bytes on the wire or we errored out.
return;
}
while (!payloadQueue_.empty()) {
auto const frameSizeFieldLen = frameSizeFieldLength();
if (payloadQueue_.chainLength() < frameSizeFieldLen) {
// We don't even have the next frame size value.
break;
}
auto const nextFrameSize = readFrameLength();
if (nextFrameSize < minimalFrameLength()) {
error("Invalid frame - Frame size smaller than minimum");
break;
}
if (payloadQueue_.chainLength() < frameSizeWithLengthField(nextFrameSize)) {
// Need to accumulate more data.
break;
}
auto payloadSize = frameSizeWithoutLengthField(nextFrameSize);
if (stripFrameLengthField_) {
payloadQueue_.trimStart(frameSizeFieldLen);
} else {
payloadSize += frameSizeFieldLen;
}
DCHECK_GT(payloadSize, 0)
<< "folly::IOBufQueue::split(0) returns a nullptr, can't have that";
auto nextFrame = payloadQueue_.split(payloadSize);
onFrame(std::move(nextFrame));
}
}
bool Framer::ensureOrAutodetectProtocolVersion() {
if (protocolVersion_ != ProtocolVersion::Unknown) {
return true;
}
const auto minBytesNeeded =
FrameSerializerV1_0::kMinBytesNeededForAutodetection;
DCHECK_GT(minBytesNeeded, 0);
if (payloadQueue_.chainLength() < minBytesNeeded) {
return false;
}
DCHECK_GT(minBytesNeeded, kFrameLengthFieldLengthV1_0);
auto const& firstFrame = *payloadQueue_.front();
const auto detectedV1 = FrameSerializerV1_0::detectProtocolVersion(
firstFrame, kFrameLengthFieldLengthV1_0);
if (detectedV1 != ProtocolVersion::Unknown) {
protocolVersion_ = FrameSerializerV1_0::Version;
return true;
}
error("Could not detect protocol version from data");
return false;
}
std::unique_ptr<folly::IOBuf> Framer::prependSize(
std::unique_ptr<folly::IOBuf> payload) {
CHECK(payload);
const auto frameSizeFieldLengthValue = frameSizeFieldLength();
const auto payloadLength = payload->computeChainDataLength();
CHECK_LE(payloadLength, kMaxFrameLength)
<< "payloadLength: " << payloadLength
<< " kMaxFrameLength: " << kMaxFrameLength;
if (payload->headroom() >= frameSizeFieldLengthValue) {
// move the data pointer back and write value to the payload
payload->prepend(frameSizeFieldLengthValue);
folly::io::RWPrivateCursor cur(payload.get());
writeFrameLength(cur, payloadLength, frameSizeFieldLengthValue);
return payload;
} else {
auto newPayload = folly::IOBuf::createCombined(frameSizeFieldLengthValue);
folly::io::Appender appender(newPayload.get(), /* do not grow */ 0);
writeFrameLength(appender, payloadLength, frameSizeFieldLengthValue);
newPayload->appendChain(std::move(payload));
return newPayload;
}
}
StreamId Framer::peekStreamId(
const folly::IOBuf& frame,
bool skipFrameLengthBytes) const {
return FrameSerializer::peekStreamId(
protocolVersion_, frame, skipFrameLengthBytes)
.value();
}
std::unique_ptr<folly::IOBuf> Framer::drainPayloadQueue() {
return payloadQueue_.move();
}
} // namespace rsocket