Comms: Always use the inline buffer

PiperOrigin-RevId: 606974170
Change-Id: I5f384bfd1b0cd5fecf493162bc40f17860b5975b
This commit is contained in:
Wiktor Garbacz 2024-02-14 07:12:01 -08:00 committed by Copybara-Service
parent 4f93af65e6
commit 34f129dc51

View File

@ -27,6 +27,7 @@
#include <syscall.h> #include <syscall.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm>
#include <atomic> #include <atomic>
#include <cerrno> #include <cerrno>
#include <cstdint> #include <cstdint>
@ -236,17 +237,19 @@ bool Comms::SendTLV(uint32_t tag, size_t length, const void* value) {
.len = length, .len = length,
}; };
if (length + sizeof(tl) > kSendTLVTempBufferSize) { const size_t inline_size =
if (!Send(&tl, sizeof(tl))) { std::min(length, kSendTLVTempBufferSize - sizeof(tl));
return false;
}
return Send(value, length);
}
uint8_t tlv[kSendTLVTempBufferSize]; uint8_t tlv[kSendTLVTempBufferSize];
memcpy(tlv, &tl, sizeof(tl)); memcpy(tlv, &tl, sizeof(tl));
memcpy(reinterpret_cast<uint8_t*>(tlv) + sizeof(tl), value, length); memcpy(&tlv[sizeof(tl)], value, inline_size);
if (!Send(&tlv, sizeof(tl) + inline_size)) {
return Send(&tlv, sizeof(tl) + length); return false;
}
if (inline_size < length) {
return Send(reinterpret_cast<const uint8_t*>(value) + inline_size,
length - inline_size);
}
return true;
} }
bool Comms::RecvString(std::string* v) { bool Comms::RecvString(std::string* v) {