23 [this](const
PacketView& packet) { HandlePacket(packet); },
25 m_config.parserBufferSize) {}
37 std::lock_guard<std::mutex> lock(m_mutex);
38 m_parser.
Push(data, length);
48 std::lock_guard<std::mutex> lock(m_mutex);
49 m_handlers[
static_cast<uint16_t
>(typeId)] = std::move(handler);
57 std::lock_guard<std::mutex> lock(m_mutex);
58 m_handlers.erase(
static_cast<uint16_t
>(typeId));
66 std::lock_guard<std::mutex> lock(m_mutex);
67 m_errorHandler = std::move(handler);
79 std::lock_guard<std::mutex> lock(m_mutex);
80 if (m_lastRx == Clock::time_point::min()) {
91 std::lock_guard<std::mutex> lock(m_mutex);
100 std::lock_guard<std::mutex> lock(m_mutex);
101 return m_parseErrors;
112void PacketDispatcher::HandlePacket(
const PacketView& packet) {
113 m_lastRx = Clock::now();
116 if (it != m_handlers.end()) {
129void PacketDispatcher::HandleError(
const StreamParser::ErrorInfo& error) {
131 if (m_errorHandler) {
132 m_errorHandler(error);
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)
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.
std::function< void(const PacketView &)> PacketHandler
Callback for handling message packets.
Configuration for the packet dispatcher.
std::chrono::milliseconds connectionTimeout
Zero-copy view into a decoded packet buffer.
PacketHeader header
Parsed header information.