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

Parses BCNP stream and dispatches packets to registered handlers. More...

#include <dispatcher.h>

Public Types

using Clock = std::chrono::steady_clock
 

Public Member Functions

 PacketDispatcher (DispatcherConfig config={})
 Construct a dispatcher with the given configuration.
 
void PushBytes (const uint8_t *data, std::size_t length)
 Feed raw bytes from transport (thread-safe)
 
template<typename MsgType >
void RegisterHandler (PacketHandler handler)
 Register a handler for a message type (by type)
 
void RegisterHandler (MessageTypeId typeId, PacketHandler handler)
 Register a handler for a message type (by ID)
 
void UnregisterHandler (MessageTypeId typeId)
 Remove a handler.
 
void SetErrorHandler (ErrorHandler handler)
 Set error callback.
 
bool IsConnected (Clock::time_point now) const
 Check if any packets received recently.
 
Clock::time_point LastReceiveTime () const
 Get last receive time.
 
StreamParserParser ()
 Access the parser (for diagnostics)
 
const StreamParserParser () const
 
void SetWireSizeLookup (StreamParser::WireSizeLookup lookup)
 Set wire size lookup for custom message types (for testing)
 
template<typename... MsgTypes>
void RegisterMessageTypes ()
 Convenience: set wire size lookup from a list of message types.
 
uint64_t ParseErrorCount () const
 Get parse error count.
 

Detailed Description

Parses BCNP stream and dispatches packets to registered handlers.

PacketDispatcher doesn't own any queues. Robot code creates queues per subsystem and registers handlers to fill them.

PacketDispatcher dispatcher;
dispatcher.RegisterHandler<MyMotorCmd>([&](const PacketView& pkt) {
for (auto it = pkt.begin_as<MyMotorCmd>(); it != pkt.end_as<MyMotorCmd>(); ++it) {
motorQueue.Push(*it);
}
motorQueue.NotifyReceived(Clock::now());
});
dispatcher.RegisterHandler<MyServoCmd>([&](const PacketView& pkt) {
// Handle servo commands...
});
Generic timed message queue for any message type with durationMs field.
bool Push(const MsgType &message)
Add a message to the back of the queue.
void NotifyReceived(Clock::time_point now)
Notify that messages were received from the network.
Parses BCNP stream and dispatches packets to registered handlers.
Definition dispatcher.h:49
void RegisterHandler(PacketHandler handler)
Register a handler for a message type (by type)
Definition dispatcher.h:60
Zero-copy view into a decoded packet buffer.
Definition packet.h:170

Definition at line 49 of file dispatcher.h.

Member Typedef Documentation

◆ Clock

using bcnp::PacketDispatcher::Clock = std::chrono::steady_clock

Definition at line 51 of file dispatcher.h.

Constructor & Destructor Documentation

◆ PacketDispatcher()

bcnp::PacketDispatcher::PacketDispatcher ( DispatcherConfig config = {})
explicit

Construct a dispatcher with the given configuration.

Creates internal StreamParser with callbacks wired to HandlePacket/HandleError.

Parameters
configDispatcher configuration (buffer size, timeouts)

Definition at line 20 of file dispatcher.cpp.

Member Function Documentation

◆ IsConnected()

bool bcnp::PacketDispatcher::IsConnected ( Clock::time_point now) const

Check if any packets received recently.

Check if the connection is active.

Returns true if a packet was received within the configured timeout period.

Parameters
nowCurrent time point for comparison
Returns
true if recently received packets, false if timed out

Definition at line 78 of file dispatcher.cpp.

◆ LastReceiveTime()

PacketDispatcher::Clock::time_point bcnp::PacketDispatcher::LastReceiveTime ( ) const

Get last receive time.

Get the timestamp of the last received packet.

Returns
Time point of last successful packet reception

Definition at line 90 of file dispatcher.cpp.

◆ ParseErrorCount()

uint64_t bcnp::PacketDispatcher::ParseErrorCount ( ) const

Get parse error count.

Get the total number of parse errors encountered.

Returns
Cumulative count of parse errors since creation

Definition at line 99 of file dispatcher.cpp.

◆ Parser() [1/2]

StreamParser & bcnp::PacketDispatcher::Parser ( )
inline

Access the parser (for diagnostics)

Definition at line 80 of file dispatcher.h.

◆ Parser() [2/2]

const StreamParser & bcnp::PacketDispatcher::Parser ( ) const
inline

Definition at line 81 of file dispatcher.h.

◆ PushBytes()

void bcnp::PacketDispatcher::PushBytes ( const uint8_t * data,
std::size_t length )

Feed raw bytes from transport (thread-safe)

Push raw bytes for parsing and dispatch.

Thread-safe: acquires mutex before accessing parser. Parsed packets are dispatched to registered handlers synchronously.

Parameters
dataPointer to incoming byte data
lengthNumber of bytes to process

Definition at line 36 of file dispatcher.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterHandler() [1/2]

void bcnp::PacketDispatcher::RegisterHandler ( MessageTypeId typeId,
PacketHandler handler )

Register a handler for a message type (by ID)

Register a handler for a specific message type.

Parameters
typeIdMessage type ID to handle
handlerCallback to invoke when packets of this type arrive

Definition at line 47 of file dispatcher.cpp.

◆ RegisterHandler() [2/2]

template<typename MsgType >
void bcnp::PacketDispatcher::RegisterHandler ( PacketHandler handler)
inline

Register a handler for a message type (by type)

Definition at line 60 of file dispatcher.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterMessageTypes()

template<typename... MsgTypes>
void bcnp::PacketDispatcher::RegisterMessageTypes ( )
inline

Convenience: set wire size lookup from a list of message types.

Definition at line 90 of file dispatcher.h.

Here is the call graph for this function:

◆ SetErrorHandler()

void bcnp::PacketDispatcher::SetErrorHandler ( ErrorHandler handler)

Set error callback.

Set the error callback for parse failures.

Parameters
handlerCallback to invoke on stream parsing errors

Definition at line 65 of file dispatcher.cpp.

◆ SetWireSizeLookup()

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

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

Definition at line 84 of file dispatcher.h.

Here is the call graph for this function:

◆ UnregisterHandler()

void bcnp::PacketDispatcher::UnregisterHandler ( MessageTypeId typeId)

Remove a handler.

Remove a previously registered handler.

Parameters
typeIdMessage type ID to stop handling

Definition at line 56 of file dispatcher.cpp.


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