Self-registering IP-protocol and UDP-port dispatch; move picomap-specific constants out of generic net/dispatch layers
This commit is contained in:
@@ -11,14 +11,29 @@
|
||||
|
||||
static timer_queue timers;
|
||||
static std::array<handler_fn, 128> handler_map{};
|
||||
static uint16_t listen_port_be = 0;
|
||||
|
||||
uint16_t dispatch_listen_port_be() { return listen_port_be; }
|
||||
|
||||
static void igmp_reannounce() {
|
||||
auto& ns = net_get_state();
|
||||
igmp::send_all_reports(ns.mac, ns.ip);
|
||||
igmp::send_all_reports();
|
||||
dispatch_schedule_ms(60000, igmp_reannounce);
|
||||
}
|
||||
|
||||
void dispatch_init() {
|
||||
static void on_udp_message(std::span<const uint8_t> payload, const udp::address& from) {
|
||||
auto msg = try_decode(payload.data(), payload.size());
|
||||
if (!msg) return;
|
||||
if (msg->type_id < 0 || !handler_map[msg->type_id]) {
|
||||
dlogf("dispatch: unknown type_id %d", msg->type_id);
|
||||
return;
|
||||
}
|
||||
responder resp{msg->message_id, from};
|
||||
handler_map[msg->type_id](resp, msg->payload);
|
||||
}
|
||||
|
||||
void dispatch_init(uint16_t port_be) {
|
||||
listen_port_be = port_be;
|
||||
udp::register_port(port_be, on_udp_message);
|
||||
net_init();
|
||||
dispatch_schedule_ms(60000, igmp_reannounce);
|
||||
dlog("dispatch_init complete");
|
||||
@@ -34,17 +49,6 @@ bool dispatch_cancel_timer(timer_handle h) {
|
||||
return timers.cancel(h);
|
||||
}
|
||||
|
||||
void udp::client::handler(std::span<const uint8_t> payload, const udp::address& from) {
|
||||
auto msg = try_decode(payload.data(), payload.size());
|
||||
if (!msg) return;
|
||||
if (msg->type_id < 0 || !handler_map[msg->type_id]) {
|
||||
dlogf("dispatch: unknown type_id %d", msg->type_id);
|
||||
return;
|
||||
}
|
||||
responder resp{msg->message_id, from};
|
||||
handler_map[msg->type_id](resp, msg->payload);
|
||||
}
|
||||
|
||||
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
|
||||
for (auto& entry : handlers)
|
||||
handler_map[entry.type_id] = entry.handle;
|
||||
|
||||
Reference in New Issue
Block a user