30 lines
1.0 KiB
C++
30 lines
1.0 KiB
C++
#pragma once
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <span>
|
|
#include "wire.h"
|
|
|
|
using handler_fn = msgpack::result<size_t> (*)(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out);
|
|
|
|
struct handler_entry {
|
|
int8_t type_id;
|
|
handler_fn handle;
|
|
};
|
|
|
|
template <typename Req, auto Fn>
|
|
msgpack::result<size_t> typed_handler(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out) {
|
|
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) {
|
|
return encode_response_into(out, message_id, DeviceError{1, "decode request ext_id=" +
|
|
std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast<int>(r.error()))});
|
|
}
|
|
return encode_response_into(out, message_id, Fn(req));
|
|
}
|
|
|
|
void dispatch_init();
|
|
void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn);
|
|
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers);
|