Zero-copy decode: span-based Envelope and DecodedMessage, defer usb_rx_buf clear
This commit is contained in:
@@ -773,6 +773,13 @@ inline result<parser> unpack(const parser &p, std::vector<uint8_t> &out) {
|
|||||||
return p.next();
|
return p.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline result<parser> unpack(const parser &p, std::span<const uint8_t> &out) {
|
||||||
|
auto v = p.get_binary_view();
|
||||||
|
if (!v) return std::unexpected(v.error());
|
||||||
|
out = std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(v->data()), v->size());
|
||||||
|
return p.next();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
requires (!std::is_same_v<T, uint8_t>)
|
requires (!std::is_same_v<T, uint8_t>)
|
||||||
result<parser> unpack(const parser &p, std::vector<T> &out) {
|
result<parser> unpack(const parser &p, std::vector<T> &out) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ struct Envelope {
|
|||||||
static constexpr int8_t ext_id = 0;
|
static constexpr int8_t ext_id = 0;
|
||||||
uint32_t message_id;
|
uint32_t message_id;
|
||||||
uint32_t checksum;
|
uint32_t checksum;
|
||||||
std::vector<uint8_t> payload;
|
std::span<const uint8_t> payload;
|
||||||
auto as_tuple() const { return std::tie(message_id, checksum, payload); }
|
auto as_tuple() const { return std::tie(message_id, checksum, payload); }
|
||||||
auto as_tuple() { return std::tie(message_id, checksum, payload); }
|
auto as_tuple() { return std::tie(message_id, checksum, payload); }
|
||||||
};
|
};
|
||||||
@@ -94,7 +94,7 @@ static constexpr uint8_t hash_key[8] = {};
|
|||||||
struct DecodedMessage {
|
struct DecodedMessage {
|
||||||
uint32_t message_id;
|
uint32_t message_id;
|
||||||
int8_t type_id;
|
int8_t type_id;
|
||||||
std::vector<uint8_t> payload;
|
std::span<const uint8_t> payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr size_t ext16_header_len = 4;
|
static constexpr size_t ext16_header_len = 4;
|
||||||
@@ -151,8 +151,7 @@ inline msgpack::result<DecodedMessage> try_decode(const uint8_t *data, size_t le
|
|||||||
|
|
||||||
auto& [type_id, ext_data] = *ext;
|
auto& [type_id, ext_data] = *ext;
|
||||||
return DecodedMessage{env.message_id, type_id,
|
return DecodedMessage{env.message_id, type_id,
|
||||||
std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(ext_data.data()),
|
std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(ext_data.data()), ext_data.size())};
|
||||||
reinterpret_cast<const uint8_t*>(ext_data.data()) + ext_data.size())};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
|
|||||||
@@ -59,12 +59,11 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
usb_rx_buf.clear();
|
|
||||||
|
|
||||||
auto it = handler_map.find(msg->type_id);
|
auto it = handler_map.find(msg->type_id);
|
||||||
if (it == handler_map.end()) continue;
|
if (it == handler_map.end()) { usb_rx_buf.clear(); continue; }
|
||||||
span_writer out(tx_buf);
|
span_writer out(tx_buf);
|
||||||
auto resp = it->second(msg->message_id, msg->payload, out);
|
auto resp = it->second(msg->message_id, msg->payload, out);
|
||||||
|
usb_rx_buf.clear();
|
||||||
if (!resp || *resp == 0) continue;
|
if (!resp || *resp == 0) continue;
|
||||||
size_t resp_len = *resp;
|
size_t resp_len = *resp;
|
||||||
if (resp_len <= usb.tx.free()) {
|
if (resp_len <= usb.tx.free()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user