BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
bcnp::MessageQueue< MsgType > Class Template Reference

Generic timed message queue for any message type with durationMs field. More...

#include <message_queue.h>

Classes

class  Transaction
 RAII transaction for atomic batch operations. More...
 

Public Types

using Clock = std::chrono::steady_clock
 

Public Member Functions

 MessageQueue (MessageQueueConfig config={})
 
void Clear ()
 Remove all messages from the queue.
 
bool Push (const MsgType &message)
 Add a message to the back of the queue.
 
std::size_t Size () const
 Get the current number of queued messages.
 
void NotifyReceived (Clock::time_point now)
 Notify that messages were received from the network.
 
void Update (Clock::time_point now)
 Update queue state - call once per control loop iteration.
 
std::optional< MsgType > ActiveMessage () const
 Get the currently executing message.
 
bool IsConnected (Clock::time_point now) const
 Check if the connection is still active.
 
MessageQueueMetrics GetMetrics () const
 Get current queue metrics.
 
void ResetMetrics ()
 Reset all metrics to zero.
 
void SetConfig (const MessageQueueConfig &config)
 Update queue configuration.
 
MessageQueueConfig GetConfig () const
 
Transaction BeginTransaction ()
 

Detailed Description

template<typename MsgType>
class bcnp::MessageQueue< MsgType >

Generic timed message queue for any message type with durationMs field.

This queue manages timed execution of messages, ensuring each message runs for its specified duration before the next one starts. It handles connection timeouts, lag compensation

Template Parameters
MsgTypeMessage struct with a uint16_t durationMs field
// In network handler:
driveQueue.Push(cmd);
driveQueue.NotifyReceived(now);
// In periodic loop:
driveQueue.Update(now);
if (auto cmd = driveQueue.ActiveMessage()) {
drivetrain.execute(*cmd);
}
Generic timed message queue for any message type with durationMs field.
void Update(Clock::time_point now)
Update queue state - call once per control loop iteration.
std::optional< MsgType > ActiveMessage() const
Get the currently executing message.
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.

Definition at line 84 of file message_queue.h.

Member Typedef Documentation

◆ Clock

template<typename MsgType >
using bcnp::MessageQueue< MsgType >::Clock = std::chrono::steady_clock

Definition at line 89 of file message_queue.h.

Constructor & Destructor Documentation

◆ MessageQueue()

template<typename MsgType >
bcnp::MessageQueue< MsgType >::MessageQueue ( MessageQueueConfig config = {})
inlineexplicit

Definition at line 91 of file message_queue.h.

Member Function Documentation

◆ ActiveMessage()

template<typename MsgType >
std::optional< MsgType > bcnp::MessageQueue< MsgType >::ActiveMessage ( ) const
inline

Get the currently executing message.

Returns the message whose duration is currently being executed. Blocks briefly if mutex is held by network thread (typically <5µs).

Returns
The active message, or nullopt if none active

Definition at line 200 of file message_queue.h.

◆ BeginTransaction()

template<typename MsgType >
Transaction bcnp::MessageQueue< MsgType >::BeginTransaction ( )
inline

Definition at line 297 of file message_queue.h.

◆ Clear()

template<typename MsgType >
void bcnp::MessageQueue< MsgType >::Clear ( )
inline

Remove all messages from the queue.

Also clears the active message and resets virtual cursor. Thread-safe.

Definition at line 103 of file message_queue.h.

◆ GetConfig()

template<typename MsgType >
MessageQueueConfig bcnp::MessageQueue< MsgType >::GetConfig ( ) const
inline

Definition at line 253 of file message_queue.h.

◆ GetMetrics()

template<typename MsgType >
MessageQueueMetrics bcnp::MessageQueue< MsgType >::GetMetrics ( ) const
inline

Get current queue metrics.

Returns
Snapshot of queue statistics

Definition at line 223 of file message_queue.h.

◆ IsConnected()

template<typename MsgType >
bool bcnp::MessageQueue< MsgType >::IsConnected ( Clock::time_point now) const
inline

Check if the connection is still active.

Parameters
nowCurrent timestamp for timeout calculation
Returns
true if packets received within connectionTimeout, false otherwise

Definition at line 214 of file message_queue.h.

◆ NotifyReceived()

template<typename MsgType >
void bcnp::MessageQueue< MsgType >::NotifyReceived ( Clock::time_point now)
inline

Notify that messages were received from the network.

Call this after pushing messages to update the connection timeout. The queue will clear itself if no notifications occur within the configured connectionTimeout period.

Parameters
nowCurrent timestamp (typically steady_clock::now())

Definition at line 144 of file message_queue.h.

◆ Push()

template<typename MsgType >
bool bcnp::MessageQueue< MsgType >::Push ( const MsgType & message)
inline

Add a message to the back of the queue.

Parameters
messageThe message to enqueue
Returns
true if successfully added, false if queue was full
Note
Increments queueOverflows metric on failure.

Definition at line 116 of file message_queue.h.

◆ ResetMetrics()

template<typename MsgType >
void bcnp::MessageQueue< MsgType >::ResetMetrics ( )
inline

Reset all metrics to zero.

Definition at line 231 of file message_queue.h.

◆ SetConfig()

template<typename MsgType >
void bcnp::MessageQueue< MsgType >::SetConfig ( const MessageQueueConfig & config)
inline

Update queue configuration.

Note
Clears the queue if capacity changes.
Parameters
configNew configuration to apply

Definition at line 243 of file message_queue.h.

◆ Size()

template<typename MsgType >
std::size_t bcnp::MessageQueue< MsgType >::Size ( ) const
inline

Get the current number of queued messages.

Returns
Number of messages waiting (excludes active message)

Definition at line 130 of file message_queue.h.

◆ Update()

template<typename MsgType >
void bcnp::MessageQueue< MsgType >::Update ( Clock::time_point now)
inline

Update queue state - call once per control loop iteration.

Performs the following: Checks connection timeout - clears queue if timed out Checks if active message duration has elapsed Promotes next message from queue if ready Handles lag compensation by skipping stale messages

Parameters
nowCurrent timestamp for timing calculations

Definition at line 160 of file message_queue.h.


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