proxygen
wangle::LengthFieldPrepender Class Reference

#include <LengthFieldPrepender.h>

Inheritance diagram for wangle::LengthFieldPrepender:
wangle::OutboundHandler< Win, Wout > wangle::HandlerBase< OutboundHandlerContext< Wout > >

Public Member Functions

 LengthFieldPrepender (int lengthFieldLength=4, int lengthAdjustment=0, bool lengthIncludesLengthField=false, bool networkByteOrder=true)
 
folly::Future< folly::Unitwrite (Context *ctx, std::unique_ptr< folly::IOBuf > buf) override
 
- Public Member Functions inherited from wangle::OutboundHandler< Win, Wout >
 ~OutboundHandler () override=default
 
virtual folly::Future< folly::Unitwrite (Context *ctx, Win msg)=0
 
virtual folly::Future< folly::UnitwriteException (Context *ctx, folly::exception_wrapper e)
 
virtual folly::Future< folly::Unitclose (Context *ctx)
 
- Public Member Functions inherited from wangle::HandlerBase< OutboundHandlerContext< Wout > >
virtual ~HandlerBase ()=default
 
virtual void attachPipeline (OutboundHandlerContext< Wout > *)
 
virtual void detachPipeline (OutboundHandlerContext< Wout > *)
 
OutboundHandlerContext< Wout > * getContext ()
 

Private Attributes

int lengthFieldLength_
 
int lengthAdjustment_
 
bool lengthIncludesLengthField_
 
bool networkByteOrder_
 

Additional Inherited Members

- Public Types inherited from wangle::OutboundHandler< Win, Wout >
typedef folly::Unit rin
 
typedef folly::Unit rout
 
typedef Win win
 
typedef Wout wout
 
typedef OutboundHandlerContext< Wout > Context
 
- Static Public Attributes inherited from wangle::OutboundHandler< Win, Wout >
static const HandlerDir dir = HandlerDir::OUT
 

Detailed Description

An encoder that prepends the length of the message. The length value is prepended as a binary form.

For example, LengthFieldPrepender(2)will encode the following 12-bytes string:

+-------------—+ | "HELLO, WORLD" | +-------------—+

into the following:

+-----—+-------------—+

  • 0x000C | "HELLO, WORLD" | +-----—+-------------—+

If you turned on the lengthIncludesLengthFieldLength flag in the constructor, the encoded data would look like the following (12 (original data) + 2 (prepended data) = 14 (0xE)):

+-----—+-------------—+

  • 0x000E | "HELLO, WORLD" | +-----—+-------------—+

Definition at line 50 of file LengthFieldPrepender.h.

Constructor & Destructor Documentation

wangle::LengthFieldPrepender::LengthFieldPrepender ( int  lengthFieldLength = 4,
int  lengthAdjustment = 0,
bool  lengthIncludesLengthField = false,
bool  networkByteOrder = true 
)
explicit

Definition at line 25 of file LengthFieldPrepender.cpp.

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  }

Member Function Documentation

Future< Unit > wangle::LengthFieldPrepender::write ( Context ctx,
std::unique_ptr< folly::IOBuf buf 
)
override

Definition at line 40 of file LengthFieldPrepender.cpp.

References c, folly::IOBuf::computeChainDataLength(), folly::IOBuf::create(), wangle::OutboundHandlerContext< Out >::fireWrite(), lengthAdjustment_, lengthFieldLength_, lengthIncludesLengthField_, folly::gen::move, networkByteOrder_, uint16_t, uint32_t, uint64_t, and uint8_t.

41  {
42  int length = lengthAdjustment_ + buf->computeChainDataLength();
44  length += lengthFieldLength_;
45  }
46 
47  if (length < 0) {
48  throw std::runtime_error("Length field < 0");
49  }
50 
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 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
char c

Member Data Documentation

int wangle::LengthFieldPrepender::lengthAdjustment_
private

Definition at line 63 of file LengthFieldPrepender.h.

Referenced by write().

int wangle::LengthFieldPrepender::lengthFieldLength_
private

Definition at line 62 of file LengthFieldPrepender.h.

Referenced by write().

bool wangle::LengthFieldPrepender::lengthIncludesLengthField_
private

Definition at line 64 of file LengthFieldPrepender.h.

Referenced by write().

bool wangle::LengthFieldPrepender::networkByteOrder_
private

Definition at line 65 of file LengthFieldPrepender.h.

Referenced by write().


The documentation for this class was generated from the following files: