Zero-copy ethernet TX: encode directly into prepend_buffer, add info/test skills

This commit is contained in:
Ian Gulliver
2026-04-12 07:50:20 +09:00
parent 846d6bb201
commit 761b740c10
6 changed files with 40 additions and 12 deletions

View File

@@ -45,7 +45,7 @@ bool dispatch_cancel_timer(timer_handle h) {
static static_vector<uint8_t, 4096> usb_rx_buf;
static std::array<uint8_t, 4096> tx_buf;
auto dispatch_msg = [&](const DecodedMessage& msg, std::function<void(std::span<const uint8_t>)> send) {
auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) {
auto it = handler_map.find(msg.type_id);
if (it == handler_map.end()) {
dlogf("dispatch: unknown type_id %d", msg.type_id);
@@ -56,7 +56,7 @@ bool dispatch_cancel_timer(timer_handle h) {
};
net_set_handler([&](std::span<const uint8_t> payload,
std::function<void(std::span<const uint8_t>)> send) {
send_fn send) {
auto msg = try_decode(payload.data(), payload.size());
if (!msg) return;
dispatch_msg(*msg, std::move(send));
@@ -82,7 +82,12 @@ bool dispatch_cancel_timer(timer_handle h) {
continue;
}
dispatch_msg(*msg, [&](std::span<const uint8_t> data) {
dispatch_msg(*msg, [&](encode_fn encode) {
uint8_t buf[4096];
span_writer out(buf, sizeof(buf));
auto r = encode(out);
if (!r) return;
std::span<const uint8_t> data{buf, *r};
if (data.size() <= usb.tx.free()) {
if (!usb.send(data))
dlogf("usb send failed: %zu bytes, %u free", data.size(), usb.tx.free());