Split net stack into eth/arp/ipv4/icmp, deferred handler responses, ping tests
This commit is contained in:
@@ -1,10 +1,24 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#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 responder {
|
||||
uint32_t message_id;
|
||||
std::function<void(std::span<const uint8_t>)> send;
|
||||
|
||||
template <typename T>
|
||||
void respond(const T& msg) const {
|
||||
uint8_t buf[1024];
|
||||
span_writer out(buf, sizeof(buf));
|
||||
auto r = encode_response_into(out, message_id, msg);
|
||||
if (r) send({buf, *r});
|
||||
}
|
||||
};
|
||||
|
||||
using handler_fn = void (*)(responder resp, std::span<const uint8_t> payload);
|
||||
|
||||
struct handler_entry {
|
||||
int8_t type_id;
|
||||
@@ -12,16 +26,19 @@ struct handler_entry {
|
||||
};
|
||||
|
||||
template <typename Req, auto Fn>
|
||||
msgpack::result<size_t> typed_handler(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out) {
|
||||
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) {
|
||||
return encode_response_into(out, message_id, DeviceError{1, "decode request ext_id=" +
|
||||
resp.respond(DeviceError{1, "decode request ext_id=" +
|
||||
std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast<int>(r.error()))});
|
||||
return;
|
||||
}
|
||||
return encode_response_into(out, message_id, Fn(req));
|
||||
auto result = Fn(resp, req);
|
||||
if (result)
|
||||
resp.respond(*result);
|
||||
}
|
||||
|
||||
void dispatch_init();
|
||||
|
||||
Reference in New Issue
Block a user