Eliminate std::function: fn-pointer callbacks, per-test test_state structs, udp.cpp with link-time udp::client::handler, udp::address

This commit is contained in:
Ian Gulliver
2026-04-19 00:32:13 -07:00
parent 40f7fb5941
commit 32044a5cbd
11 changed files with 378 additions and 285 deletions

View File

@@ -1,13 +1,16 @@
#include "dispatch.h"
#include <array>
#include "pico/stdlib.h"
#include "wire.h"
#include "timer_queue.h"
#include "net.h"
#include "igmp.h"
#include "udp.h"
#include "debug_log.h"
#include "hardware/sync.h"
static timer_queue timers;
static std::array<handler_fn, 128> handler_map{};
static void igmp_reannounce() {
auto& ns = net_get_state();
@@ -21,8 +24,8 @@ void dispatch_init() {
dlog("dispatch_init complete");
}
timer_handle dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
auto h = timers.schedule_ms(ms, std::move(fn));
timer_handle dispatch_schedule_ms(uint32_t ms, void (*fn)()) {
auto h = timers.schedule_ms(ms, fn);
if (!h) dlogf("timer alloc failed: %lu ms", static_cast<unsigned long>(ms));
return h;
}
@@ -31,30 +34,23 @@ bool dispatch_cancel_timer(timer_handle h) {
return timers.cancel(h);
}
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
std::array<handler_fn, 128> handler_map{};
for (auto& entry : handlers) {
handler_map[entry.type_id] = entry.handle;
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;
static std::array<uint8_t, 4096> tx_buf;
auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) {
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, std::move(send)};
handler_map[msg.type_id](resp, msg.payload);
};
net_set_handler([&](std::span<const uint8_t> payload,
send_fn send) {
auto msg = try_decode(payload.data(), payload.size());
if (!msg) return;
dispatch_msg(*msg, std::move(send));
});
while (true) {
uint32_t save = save_and_disable_interrupts();