10#include <unordered_map>
51 using Clock = std::chrono::steady_clock;
56 void PushBytes(
const uint8_t* data, std::size_t length);
59 template<
typename MsgType>
89 template<
typename... MsgTypes>
92 return GetWireSizeFor<MsgTypes...>(typeId);
100 template<
typename First,
typename... Rest>
102 if (typeId == First::kTypeId) {
103 return First::kWireSize;
105 if constexpr (
sizeof...(Rest) > 0) {
106 return GetWireSizeFor<Rest...>(typeId);
111 void HandlePacket(
const PacketView& packet);
112 void HandleError(
const StreamParser::ErrorInfo& error);
114 DispatcherConfig m_config;
115 StreamParser m_parser;
116 mutable std::mutex m_mutex;
117 std::unordered_map<uint16_t, PacketHandler> m_handlers;
119 Clock::time_point m_lastRx{Clock::time_point::min()};
120 uint64_t m_parseErrors{0};
Parses BCNP stream and dispatches packets to registered handlers.
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 SetWireSizeLookup(StreamParser::WireSizeLookup lookup)
Set wire size lookup for custom message types (for testing)
void RegisterHandler(PacketHandler handler)
Register a handler for a message type (by type)
const StreamParser & Parser() const
void SetErrorHandler(ErrorHandler handler)
Set error callback.
uint64_t ParseErrorCount() const
Get parse error count.
StreamParser & Parser()
Access the parser (for diagnostics)
std::chrono::steady_clock Clock
bool IsConnected(Clock::time_point now) const
Check if any packets received recently.
void RegisterMessageTypes()
Convenience: set wire size lookup from a list of message types.
Parses a byte stream into BCNP packets.
void SetWireSizeLookup(WireSizeLookup lookup)
Set custom wire size lookup (for testing with custom message types)
std::function< std::size_t(MessageTypeId)> WireSizeLookup
Timed message queue for executing duration-based commands.
std::function< void(const StreamParser::ErrorInfo &)> ErrorHandler
Error callback for parse errors.
PacketHandler MessageHandler
std::function< void(const PacketView &)> PacketHandler
Callback for handling message packets.
Configuration for the packet dispatcher.
std::size_t parserBufferSize
std::chrono::milliseconds connectionTimeout
Zero-copy view into a decoded packet buffer.