binpac: Add FlowBuffer policy mechanisms

This allows for tunability of the following behaviors:

* Minimum flowbuffer capacity to use when parsing a new unit

* Threshold at which flowbuffer capacity is contracted back to the
  minimum after parsing a complete unit and before parsing the next

* Maximum flowbuffer capacity to allow when parsing a given unit

Failed flowbuffer allocations due to reaching maximum capacity or any
other reason now throw ExceptionFlowBufferAlloc.
This commit is contained in:
Jon Siwek 2019-04-05 12:21:42 -07:00 committed by Tim Wojtulewicz
parent 7e6e24a4d8
commit b4b229acf7
4 changed files with 73 additions and 13 deletions

View file

@ -8,6 +8,12 @@ namespace binpac {
class FlowBuffer {
public:
struct Policy {
int max_capacity;
int min_capacity;
int contract_threshold;
};
enum LineBreakStyle {
CR_OR_LF, // CR or LF or CRLF
STRICT_CRLF, // CR followed by LF
@ -95,6 +101,9 @@ public:
bool have_pending_request() const { return have_pending_request_; }
static void init(Policy p)
{ policy = p; }
protected:
// Reset the buffer for a new message
void NewMessage();
@ -106,6 +115,13 @@ protected:
// buffer.
void ExpandBuffer(int length);
// Contract the buffer to some minimum capacity.
// Existing contents in the buffer are preserved (but only usage
// at the time of creation this function is when the contents
// are being discarded due to parsing exception or have already been
// copied out after parsing a complete unit).
void ContractBuffer();
// Reset line state when transit from frame mode to line mode.
void ResetLineState();
@ -153,6 +169,8 @@ protected:
int data_seq_at_orig_data_end_;
bool eof_;
bool have_pending_request_;
static Policy policy;
};
typedef FlowBuffer *flow_buffer_t;