BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
dispatcher.h
Go to the documentation of this file.
1#pragma once
2
6#include <chrono>
7#include <cstdint>
8#include <functional>
9#include <mutex>
10#include <unordered_map>
11
12namespace bcnp {
13
16 std::size_t parserBufferSize{4096};
17 std::chrono::milliseconds connectionTimeout{200};
18};
19
21using PacketHandler = std::function<void(const PacketView&)>;
22
24using ErrorHandler = std::function<void(const StreamParser::ErrorInfo&)>;
25
50public:
51 using Clock = std::chrono::steady_clock;
52
53 explicit PacketDispatcher(DispatcherConfig config = {});
54
56 void PushBytes(const uint8_t* data, std::size_t length);
57
59 template<typename MsgType>
61 RegisterHandler(MsgType::kTypeId, std::move(handler));
62 }
63
65 void RegisterHandler(MessageTypeId typeId, PacketHandler handler);
66
69
71 void SetErrorHandler(ErrorHandler handler);
72
74 bool IsConnected(Clock::time_point now) const;
75
77 Clock::time_point LastReceiveTime() const;
78
80 StreamParser& Parser() { return m_parser; }
81 const StreamParser& Parser() const { return m_parser; }
82
85 m_parser.SetWireSizeLookup(std::move(lookup));
86 }
87
89 template<typename... MsgTypes>
91 m_parser.SetWireSizeLookup([](MessageTypeId typeId) -> std::size_t {
92 return GetWireSizeFor<MsgTypes...>(typeId);
93 });
94 }
95
97 uint64_t ParseErrorCount() const;
98
99private:
100 template<typename First, typename... Rest>
101 static std::size_t GetWireSizeFor(MessageTypeId typeId) {
102 if (typeId == First::kTypeId) {
103 return First::kWireSize;
104 }
105 if constexpr (sizeof...(Rest) > 0) {
106 return GetWireSizeFor<Rest...>(typeId);
107 }
108 return 0;
109 }
110
111 void HandlePacket(const PacketView& packet);
112 void HandleError(const StreamParser::ErrorInfo& error);
113
114 DispatcherConfig m_config;
115 StreamParser m_parser;
116 mutable std::mutex m_mutex;
117 std::unordered_map<uint16_t, PacketHandler> m_handlers;
118 ErrorHandler m_errorHandler;
119 Clock::time_point m_lastRx{Clock::time_point::min()};
120 uint64_t m_parseErrors{0};
121};
122
123// Type alias for backward compatibility
125
126} // namespace bcnp
Parses BCNP stream and dispatches packets to registered handlers.
Definition dispatcher.h:49
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)
Definition dispatcher.h:84
void RegisterHandler(PacketHandler handler)
Register a handler for a message type (by type)
Definition dispatcher.h:60
const StreamParser & Parser() const
Definition dispatcher.h:81
void SetErrorHandler(ErrorHandler handler)
Set error callback.
uint64_t ParseErrorCount() const
Get parse error count.
StreamParser & Parser()
Access the parser (for diagnostics)
Definition dispatcher.h:80
std::chrono::steady_clock Clock
Definition dispatcher.h:51
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.
Definition dispatcher.h:90
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.
Definition dispatcher.h:24
PacketHandler MessageHandler
Definition dispatcher.h:124
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::size_t parserBufferSize
Definition dispatcher.h:16
std::chrono::milliseconds connectionTimeout
Definition dispatcher.h:17
Zero-copy view into a decoded packet buffer.
Definition packet.h:170