BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
dispatcher.cpp
Go to the documentation of this file.
1
9#include "bcnp/dispatcher.h"
10
11namespace bcnp {
12
21 : m_config(config),
22 m_parser(
23 [this](const PacketView& packet) { HandlePacket(packet); },
24 [this](const StreamParser::ErrorInfo& error) { HandleError(error); },
25 m_config.parserBufferSize) {}
26
36void PacketDispatcher::PushBytes(const uint8_t* data, std::size_t length) {
37 std::lock_guard<std::mutex> lock(m_mutex);
38 m_parser.Push(data, length);
39}
40
48 std::lock_guard<std::mutex> lock(m_mutex);
49 m_handlers[static_cast<uint16_t>(typeId)] = std::move(handler);
50}
51
57 std::lock_guard<std::mutex> lock(m_mutex);
58 m_handlers.erase(static_cast<uint16_t>(typeId));
59}
60
66 std::lock_guard<std::mutex> lock(m_mutex);
67 m_errorHandler = std::move(handler);
68}
69
78bool PacketDispatcher::IsConnected(Clock::time_point now) const {
79 std::lock_guard<std::mutex> lock(m_mutex);
80 if (m_lastRx == Clock::time_point::min()) {
81 return false;
82 }
83 return (now - m_lastRx) <= m_config.connectionTimeout;
84}
85
90PacketDispatcher::Clock::time_point PacketDispatcher::LastReceiveTime() const {
91 std::lock_guard<std::mutex> lock(m_mutex);
92 return m_lastRx;
93}
94
100 std::lock_guard<std::mutex> lock(m_mutex);
101 return m_parseErrors;
102}
103
112void PacketDispatcher::HandlePacket(const PacketView& packet) {
113 m_lastRx = Clock::now();
114
115 auto it = m_handlers.find(static_cast<uint16_t>(packet.header.messageType));
116 if (it != m_handlers.end()) {
117 it->second(packet);
118 }
119 // Unknown message types are silently ignored (no handler registered)
120}
121
129void PacketDispatcher::HandleError(const StreamParser::ErrorInfo& error) {
130 ++m_parseErrors;
131 if (m_errorHandler) {
132 m_errorHandler(error);
133 }
134}
135
136} // namespace bcnp
137
PacketDispatcher(DispatcherConfig config={})
Construct a dispatcher with the given configuration.
void UnregisterHandler(MessageTypeId typeId)
Remove a handler.
void PushBytes(const uint8_t *data, std::size_t length)
Feed raw bytes from transport (thread-safe)
Clock::time_point LastReceiveTime() const
Get last receive time.
void RegisterHandler(PacketHandler handler)
Register a handler for a message type (by type)
Definition dispatcher.h:60
void SetErrorHandler(ErrorHandler handler)
Set error callback.
uint64_t ParseErrorCount() const
Get parse error count.
bool IsConnected(Clock::time_point now) const
Check if any packets received recently.
void Push(const uint8_t *data, std::size_t length)
Push raw bytes into the parser for processing.
std::function< void(const StreamParser::ErrorInfo &)> ErrorHandler
Error callback for parse errors.
Definition dispatcher.h:24
std::function< void(const PacketView &)> PacketHandler
Callback for handling message packets.
Definition dispatcher.h:21
Configuration for the packet dispatcher.
Definition dispatcher.h:15
std::chrono::milliseconds connectionTimeout
Definition dispatcher.h:17
MessageTypeId messageType
Type ID of messages in payload.
Definition packet.h:79
Zero-copy view into a decoded packet buffer.
Definition packet.h:170
PacketHeader header
Parsed header information.
Definition packet.h:171