52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#pragma once
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <span>
|
|
#include "wire.h"
|
|
#include "timer_queue.h"
|
|
#include "net.h"
|
|
|
|
struct responder {
|
|
uint32_t message_id;
|
|
send_fn send;
|
|
|
|
template <typename T>
|
|
void respond(const T& msg) const {
|
|
send([&](span_writer& out) {
|
|
return encode_response_into(out, message_id, msg);
|
|
});
|
|
}
|
|
};
|
|
|
|
using handler_fn = void (*)(responder resp, std::span<const uint8_t> payload);
|
|
|
|
struct handler_entry {
|
|
int8_t type_id;
|
|
handler_fn handle;
|
|
};
|
|
|
|
template <typename Req, auto Fn>
|
|
void typed_handler(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();
|
|
auto r = msgpack::unpack(p, tup);
|
|
if (!r) {
|
|
char err[64];
|
|
snprintf(err, sizeof(err), "decode request ext_id=%d: msgpack error %d",
|
|
Req::ext_id, static_cast<int>(r.error()));
|
|
resp.respond(DeviceError{1, err});
|
|
return;
|
|
}
|
|
auto result = Fn(resp, req);
|
|
if (result)
|
|
resp.respond(*result);
|
|
}
|
|
|
|
void dispatch_init();
|
|
timer_handle dispatch_schedule_ms(uint32_t ms, std::function<void()> fn);
|
|
bool dispatch_cancel_timer(timer_handle h);
|
|
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers);
|