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:
@@ -1,26 +1,33 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include "wire.h"
|
||||
#include "timer_queue.h"
|
||||
#include "net.h"
|
||||
#include "prepend_buffer.h"
|
||||
#include "udp.h"
|
||||
|
||||
struct responder {
|
||||
uint32_t message_id;
|
||||
send_fn send;
|
||||
udp::address reply_to;
|
||||
|
||||
template <typename T>
|
||||
void respond(const T& msg) const {
|
||||
send([&](span_writer& out) {
|
||||
return encode_response_into(out, message_id, msg);
|
||||
});
|
||||
const auto& ns = net_get_state();
|
||||
prepend_buffer<4096> buf;
|
||||
span_writer out(buf.payload_ptr(), 2048);
|
||||
auto r = encode_response_into(out, message_id, msg);
|
||||
if (!r) return;
|
||||
buf.append(*r);
|
||||
udp::prepend(buf, reply_to.mac, ns.mac, ns.ip, reply_to.ip,
|
||||
PICOMAP_PORT_BE, reply_to.port, *r);
|
||||
net_send_raw(buf.span());
|
||||
}
|
||||
};
|
||||
|
||||
using handler_fn = void (*)(responder resp, std::span<const uint8_t> payload);
|
||||
using handler_fn = void (*)(const responder& resp, std::span<const uint8_t> payload);
|
||||
|
||||
struct handler_entry {
|
||||
int8_t type_id;
|
||||
@@ -28,7 +35,7 @@ struct handler_entry {
|
||||
};
|
||||
|
||||
template <typename Req, auto Fn>
|
||||
void typed_handler(responder resp, std::span<const uint8_t> payload) {
|
||||
void typed_handler(const responder& resp, std::span<const uint8_t> payload) {
|
||||
msgpack::parser p(payload.data(), static_cast<int>(payload.size()));
|
||||
Req req;
|
||||
auto tup = req.as_tuple();
|
||||
@@ -46,6 +53,6 @@ void typed_handler(responder resp, std::span<const uint8_t> payload) {
|
||||
}
|
||||
|
||||
void dispatch_init();
|
||||
timer_handle dispatch_schedule_ms(uint32_t ms, std::function<void()> fn);
|
||||
timer_handle dispatch_schedule_ms(uint32_t ms, void (*fn)());
|
||||
bool dispatch_cancel_timer(timer_handle h);
|
||||
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers);
|
||||
|
||||
Reference in New Issue
Block a user