BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
bcnp::StreamParser Class Reference

Parses a byte stream into BCNP packets. More...

#include <stream_parser.h>

Classes

struct  ErrorInfo
 

Public Types

using PacketCallback = std::function<void(const PacketView&)>
 
using ErrorCallback = std::function<void(const ErrorInfo&)>
 
using WireSizeLookup = std::function<std::size_t(MessageTypeId)>
 

Public Member Functions

 StreamParser (PacketCallback onPacket, ErrorCallback onError={}, std::size_t bufferSize=4096)
 Construct a stream parser with callbacks and buffer size.
 
void Push (const uint8_t *data, std::size_t length)
 Push raw bytes into the parser for processing.
 
void Reset (bool resetErrorState=true)
 Reset the parser to its initial state.
 
void SetWireSizeLookup (WireSizeLookup lookup)
 Set custom wire size lookup (for testing with custom message types)
 

Static Public Attributes

static constexpr std::size_t kMaxParseIterationsPerPush = 1024
 

Detailed Description

Parses a byte stream into BCNP packets.

Handles stream reassembly, framing, CRC validation, and error recovery. Uses a ring buffer internally for efficient parsing of partial packets.

Thread-safety: Not thread-safe. Caller must synchronize access.

Definition at line 21 of file stream_parser.h.

Member Typedef Documentation

◆ ErrorCallback

using bcnp::StreamParser::ErrorCallback = std::function<void(const ErrorInfo&)>

Definition at line 29 of file stream_parser.h.

◆ PacketCallback

using bcnp::StreamParser::PacketCallback = std::function<void(const PacketView&)>

Definition at line 23 of file stream_parser.h.

◆ WireSizeLookup

using bcnp::StreamParser::WireSizeLookup = std::function<std::size_t(MessageTypeId)>

Callback to get wire size for a message type ID Return 0 if message type is unknown

Definition at line 33 of file stream_parser.h.

Constructor & Destructor Documentation

◆ StreamParser()

bcnp::StreamParser::StreamParser ( PacketCallback onPacket,
ErrorCallback onError = {},
std::size_t bufferSize = 4096 )

Construct a stream parser with callbacks and buffer size.

Parameters
onPacketCallback invoked for each valid packet
onErrorCallback invoked on parse errors (optional)
bufferSizeInternal ring buffer size (minimum: header + checksum)

Definition at line 24 of file stream_parser.cpp.

Member Function Documentation

◆ Push()

void bcnp::StreamParser::Push ( const uint8_t * data,
std::size_t length )

Push raw bytes into the parser for processing.

Bytes are added to the internal ring buffer and parsed for complete packets. Valid packets trigger the onPacket callback; parse errors trigger onError. The parser handles partial packets across multiple Push() calls.

Parameters
dataPointer to incoming byte data
lengthNumber of bytes to process
Note
Limits iterations per call to prevent infinite loops on malformed data. Returns early if the iteration budget is exhausted.

Definition at line 46 of file stream_parser.cpp.

Here is the caller graph for this function:

◆ Reset()

void bcnp::StreamParser::Reset ( bool resetErrorState = true)

Reset the parser to its initial state.

Clears the internal buffer and optionally resets error tracking. Call this when starting a new connection or after unrecoverable errors.

Parameters
resetErrorStateIf true, also resets consecutive error count and stream offset

Definition at line 100 of file stream_parser.cpp.

◆ SetWireSizeLookup()

void bcnp::StreamParser::SetWireSizeLookup ( WireSizeLookup lookup)
inline

Set custom wire size lookup (for testing with custom message types)

Definition at line 42 of file stream_parser.h.

Here is the caller graph for this function:

Member Data Documentation

◆ kMaxParseIterationsPerPush

std::size_t bcnp::StreamParser::kMaxParseIterationsPerPush = 1024
staticconstexpr

Definition at line 44 of file stream_parser.h.


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