proxygen
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
LengthFieldPrepender.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2017-present Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
#include <
wangle/codec/LengthFieldPrepender.h
>
18
19
using
folly::Future
;
20
using
folly::Unit
;
21
using
folly::IOBuf
;
22
23
namespace
wangle
{
24
25
LengthFieldPrepender::LengthFieldPrepender
(
26
int
lengthFieldLength,
27
int
lengthAdjustment,
28
bool
lengthIncludesLengthField,
29
bool
networkByteOrder)
30
: lengthFieldLength_(lengthFieldLength)
31
, lengthAdjustment_(lengthAdjustment)
32
, lengthIncludesLengthField_(lengthIncludesLengthField)
33
, networkByteOrder_(networkByteOrder) {
34
CHECK(lengthFieldLength == 1 ||
35
lengthFieldLength == 2 ||
36
lengthFieldLength == 4 ||
37
lengthFieldLength == 8 );
38
}
39
40
Future<Unit>
LengthFieldPrepender::write
(
41
Context
* ctx, std::unique_ptr<IOBuf> buf) {
42
int
length =
lengthAdjustment_
+ buf->
computeChainDataLength
();
43
if
(
lengthIncludesLengthField_
) {
44
length +=
lengthFieldLength_
;
45
}
46
47
if
(length < 0) {
48
throw
std::runtime_error(
"Length field < 0"
);
49
}
50
51
auto
len =
IOBuf::create
(
lengthFieldLength_
);
52
len->append(
lengthFieldLength_
);
53
folly::io::RWPrivateCursor
c
(len.get());
54
55
switch
(
lengthFieldLength_
) {
56
case
1: {
57
if
(length >= 256) {
58
throw
std::runtime_error(
"length does not fit byte"
);
59
}
60
if
(
networkByteOrder_
) {
61
c
.writeBE((
uint8_t
)length);
62
}
else
{
63
c
.writeLE((
uint8_t
)length);
64
}
65
break
;
66
}
67
case
2: {
68
if
(length >= 65536) {
69
throw
std::runtime_error(
"length does not fit byte"
);
70
}
71
if
(
networkByteOrder_
) {
72
c
.writeBE((
uint16_t
)length);
73
}
else
{
74
c
.writeLE((
uint16_t
)length);
75
}
76
break
;
77
}
78
case
4: {
79
if
(
networkByteOrder_
) {
80
c
.writeBE((
uint32_t
)length);
81
}
else
{
82
c
.writeLE((
uint32_t
)length);
83
}
84
break
;
85
}
86
case
8: {
87
if
(
networkByteOrder_
) {
88
c
.writeBE((
uint64_t
)length);
89
}
else
{
90
c
.writeLE((
uint64_t
)length);
91
}
92
break
;
93
}
94
default
: {
95
throw
std::runtime_error(
"Invalid lengthFieldLength"
);
96
}
97
}
98
99
len->prependChain(
std::move
(buf));
100
return
ctx->
fireWrite
(
std::move
(len));
101
}
102
103
104
}
// namespace wangle
folly::IOBuf
Definition:
IOBuf.h:221
wangle::OutboundHandlerContext::fireWrite
virtual folly::Future< folly::Unit > fireWrite(Out msg)=0
folly::IOBuf::create
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition:
IOBuf.cpp:229
wangle::LengthFieldPrepender::lengthFieldLength_
int lengthFieldLength_
Definition:
LengthFieldPrepender.h:62
folly::gen::move
constexpr detail::Map< Move > move
Definition:
Base-inl.h:2567
wangle::LengthFieldPrepender::lengthIncludesLengthField_
bool lengthIncludesLengthField_
Definition:
LengthFieldPrepender.h:64
wangle
Definition:
Acceptor.cpp:49
LengthFieldPrepender.h
wangle::LengthFieldPrepender::lengthAdjustment_
int lengthAdjustment_
Definition:
LengthFieldPrepender.h:63
wangle::OutboundHandlerContext< Wout >
folly::Unit
Definition:
Unit.h:36
uint8_t
uint8_t
Definition:
ConstexprMathBenchmark.cpp:178
folly::Future
Definition:
FiberManagerInternal.h:46
wangle::LengthFieldPrepender::write
folly::Future< folly::Unit > write(Context *ctx, std::unique_ptr< folly::IOBuf > buf) override
Definition:
LengthFieldPrepender.cpp:40
folly::IOBuf::computeChainDataLength
std::size_t computeChainDataLength() const
Definition:
IOBuf.cpp:501
folly::io::RWCursor
Definition:
Cursor.h:815
uint64_t
uint64_t
Definition:
ConstexprMathBenchmark.cpp:190
uint32_t
uint32_t
Definition:
ConstexprMathBenchmark.cpp:186
uint16_t
uint16_t
Definition:
ConstexprMathBenchmark.cpp:182
wangle::LengthFieldPrepender::networkByteOrder_
bool networkByteOrder_
Definition:
LengthFieldPrepender.h:65
c
char c
Definition:
OptionalTest.cpp:53
wangle::LengthFieldPrepender::LengthFieldPrepender
LengthFieldPrepender(int lengthFieldLength=4, int lengthAdjustment=0, bool lengthIncludesLengthField=false, bool networkByteOrder=true)
Definition:
LengthFieldPrepender.cpp:25
proxygen
wangle
wangle
codec
LengthFieldPrepender.cpp
Generated by
1.8.11