proxygen
wangle::LineBasedFrameDecoder Class Reference

#include <LineBasedFrameDecoder.h>

Inheritance diagram for wangle::LineBasedFrameDecoder:
wangle::ByteToMessageDecoder< M > wangle::InboundHandler< folly::IOBufQueue &, M > wangle::HandlerBase< InboundHandlerContext< M > >

Public Types

enum  TerminatorType { TerminatorType::BOTH, TerminatorType::NEWLINE, TerminatorType::CARRIAGENEWLINE }
 
- Public Types inherited from wangle::ByteToMessageDecoder< M >
typedef InboundHandler< folly::IOBufQueue &, M >::Context Context
 
- Public Types inherited from wangle::InboundHandler< folly::IOBufQueue &, M >
typedef folly::IOBufQueuerin
 
typedef M rout
 
typedef folly::Unit win
 
typedef folly::Unit wout
 
typedef InboundHandlerContext< MContext
 

Public Member Functions

 LineBasedFrameDecoder (uint32_t maxLength=UINT_MAX, bool stripDelimiter=true, TerminatorType terminatorType=TerminatorType::BOTH)
 
bool decode (Context *ctx, folly::IOBufQueue &buf, std::unique_ptr< folly::IOBuf > &result, size_t &) override
 
- Public Member Functions inherited from wangle::ByteToMessageDecoder< M >
virtual bool decode (Context *ctx, folly::IOBufQueue &buf, M &result, size_t &)=0
 
void transportActive (Context *ctx) override
 
void transportInactive (Context *ctx) override
 
void read (Context *ctx, folly::IOBufQueue &q) override
 
- Public Member Functions inherited from wangle::InboundHandler< folly::IOBufQueue &, M >
 ~InboundHandler () override=default
 
virtual void read (Context *ctx, folly::IOBufQueue &msg)=0
 
virtual void readEOF (Context *ctx)
 
virtual void readException (Context *ctx, folly::exception_wrapper e)
 
virtual void transportActive (Context *ctx)
 
virtual void transportInactive (Context *ctx)
 
- Public Member Functions inherited from wangle::HandlerBase< InboundHandlerContext< M > >
virtual ~HandlerBase ()=default
 
virtual void attachPipeline (InboundHandlerContext< M > *)
 
virtual void detachPipeline (InboundHandlerContext< M > *)
 
InboundHandlerContext< M > * getContext ()
 

Private Member Functions

int64_t findEndOfLine (folly::IOBufQueue &buf)
 
void fail (Context *ctx, std::string len)
 

Private Attributes

uint32_t maxLength_
 
bool stripDelimiter_
 
bool discarding_ {false}
 
uint32_t discardedBytes_ {0}
 
TerminatorType terminatorType_
 

Additional Inherited Members

- Static Public Attributes inherited from wangle::InboundHandler< folly::IOBufQueue &, M >
static const HandlerDir dir
 

Detailed Description

A decoder that splits the received IOBufQueue on line endings.

Both "\n" and "\r\n" are handled, or optionally reqire only one or the other.

Definition at line 30 of file LineBasedFrameDecoder.h.

Member Enumeration Documentation

Enumerator
BOTH 
NEWLINE 
CARRIAGENEWLINE 

Definition at line 32 of file LineBasedFrameDecoder.h.

32  {
33  BOTH,
34  NEWLINE,
35  CARRIAGENEWLINE
36  };

Constructor & Destructor Documentation

wangle::LineBasedFrameDecoder::LineBasedFrameDecoder ( uint32_t  maxLength = UINT_MAX,
bool  stripDelimiter = true,
TerminatorType  terminatorType = TerminatorType::BOTH 
)
explicit

Definition at line 25 of file LineBasedFrameDecoder.cpp.

28  : maxLength_(maxLength)
29  , stripDelimiter_(stripDelimiter)
30  , terminatorType_(terminatorType) {}

Member Function Documentation

bool wangle::LineBasedFrameDecoder::decode ( Context ctx,
folly::IOBufQueue buf,
std::unique_ptr< folly::IOBuf > &  result,
size_t &   
)
override

Definition at line 32 of file LineBasedFrameDecoder.cpp.

References c, folly::IOBufQueue::chainLength(), discardedBytes_, discarding_, fail(), findEndOfLine(), folly::IOBufQueue::front(), int64_t, maxLength_, folly::IOBufQueue::move(), folly::gen::move, folly::IOBufQueue::split(), stripDelimiter_, and folly::IOBufQueue::trimStart().

35  {
36  int64_t eol = findEndOfLine(buf);
37 
38  if (!discarding_) {
39  if (eol >= 0) {
40  Cursor c(buf.front());
41  c += eol;
42  auto delimLength = c.read<char>() == '\r' ? 2 : 1;
43  if (eol > maxLength_) {
44  buf.split(eol + delimLength);
45  fail(ctx, folly::to<std::string>(eol));
46  return false;
47  }
48 
49  std::unique_ptr<folly::IOBuf> frame;
50 
51  if (stripDelimiter_) {
52  frame = buf.split(eol);
53  buf.trimStart(delimLength);
54  } else {
55  frame = buf.split(eol + delimLength);
56  }
57 
58  result = std::move(frame);
59  return true;
60  } else {
61  auto len = buf.chainLength();
62  if (len > maxLength_) {
63  discardedBytes_ = len;
64  buf.trimStart(len);
65  discarding_ = true;
66  fail(ctx, "over " + folly::to<std::string>(len));
67  }
68  return false;
69  }
70  } else {
71  if (eol >= 0) {
72  Cursor c(buf.front());
73  c += eol;
74  auto delimLength = c.read<char>() == '\r' ? 2 : 1;
75  buf.trimStart(eol + delimLength);
76  discardedBytes_ = 0;
77  discarding_ = false;
78  } else {
80  buf.move();
81  }
82 
83  return false;
84  }
85 }
std::unique_ptr< folly::IOBuf > split(size_t n)
Definition: IOBufQueue.h:420
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
size_t chainLength() const
Definition: IOBufQueue.h:492
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< folly::IOBuf > move()
Definition: IOBufQueue.h:459
void fail(Context *ctx, std::string len)
int64_t findEndOfLine(folly::IOBufQueue &buf)
void trimStart(size_t amount)
Definition: IOBufQueue.cpp:255
char c
void wangle::LineBasedFrameDecoder::fail ( Context ctx,
std::string  len 
)
private

Definition at line 87 of file LineBasedFrameDecoder.cpp.

References maxLength_.

Referenced by gtest_xml_test_utils.GTestXMLTestCase::_GetChildren(), decode(), and fatal_test.FatalTests::is_debug_build().

87  {
88  ctx->fireReadException(
89  folly::make_exception_wrapper<std::runtime_error>(
90  "frame length" + len +
91  " exeeds max " + folly::to<std::string>(maxLength_)));
92 }
int64_t wangle::LineBasedFrameDecoder::findEndOfLine ( folly::IOBufQueue buf)
private

Definition at line 94 of file LineBasedFrameDecoder.cpp.

References b, c, CARRIAGENEWLINE, folly::IOBufQueue::chainLength(), folly::IOBufQueue::front(), i, maxLength_, NEWLINE, terminatorType_, and uint32_t.

Referenced by decode().

94  {
95  Cursor c(buf.front());
96  for (uint32_t i = 0; i < maxLength_ && i < buf.chainLength(); i++) {
97  auto b = c.read<char>();
99  return i;
101  b == '\r' && !c.isAtEnd() && c.read<char>() == '\n') {
102  return i;
103  }
104  }
105 
106  return -1;
107 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
size_t chainLength() const
Definition: IOBufQueue.h:492
char b
char c

Member Data Documentation

uint32_t wangle::LineBasedFrameDecoder::discardedBytes_ {0}
private

Definition at line 58 of file LineBasedFrameDecoder.h.

Referenced by decode().

bool wangle::LineBasedFrameDecoder::discarding_ {false}
private

Definition at line 57 of file LineBasedFrameDecoder.h.

Referenced by decode().

uint32_t wangle::LineBasedFrameDecoder::maxLength_
private

Definition at line 54 of file LineBasedFrameDecoder.h.

Referenced by decode(), fail(), and findEndOfLine().

bool wangle::LineBasedFrameDecoder::stripDelimiter_
private

Definition at line 55 of file LineBasedFrameDecoder.h.

Referenced by decode().

TerminatorType wangle::LineBasedFrameDecoder::terminatorType_
private

Definition at line 60 of file LineBasedFrameDecoder.h.

Referenced by findEndOfLine().


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