mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-12 04:22:28 +00:00
LibIPC: Refactor message header encoding to use a helper method
Manually memcpying into a Vector in the body of post_message is a bit much.
This commit is contained in:
parent
087cbf2b0a
commit
87fbfcadd1
Notes:
github-actions[bot]
2025-05-24 16:16:10 +00:00
Author: https://github.com/ADKaster
Commit: 87fbfcadd1
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4848
Reviewed-by: https://github.com/kalenikaliaksandr ✅
1 changed files with 19 additions and 10 deletions
|
@ -157,27 +157,36 @@ struct MessageHeader {
|
||||||
Type type { Type::Payload };
|
Type type { Type::Payload };
|
||||||
u32 payload_size { 0 };
|
u32 payload_size { 0 };
|
||||||
u32 fd_count { 0 };
|
u32 fd_count { 0 };
|
||||||
|
|
||||||
|
static Vector<u8> encode_with_payload(MessageHeader header, ReadonlyBytes payload)
|
||||||
|
{
|
||||||
|
Vector<u8> message_buffer;
|
||||||
|
message_buffer.resize(sizeof(MessageHeader) + payload.size());
|
||||||
|
memcpy(message_buffer.data(), &header, sizeof(MessageHeader));
|
||||||
|
memcpy(message_buffer.data() + sizeof(MessageHeader), payload.data(), payload.size());
|
||||||
|
return message_buffer;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void TransportSocket::post_message(Vector<u8> const& bytes_to_write, Vector<NonnullRefPtr<AutoCloseFileDescriptor>> const& fds)
|
void TransportSocket::post_message(Vector<u8> const& bytes_to_write, Vector<NonnullRefPtr<AutoCloseFileDescriptor>> const& fds)
|
||||||
{
|
{
|
||||||
Vector<u8> message_buffer;
|
auto num_fds_to_transfer = fds.size();
|
||||||
message_buffer.resize(sizeof(MessageHeader) + bytes_to_write.size());
|
|
||||||
MessageHeader header;
|
auto message_buffer = MessageHeader::encode_with_payload(
|
||||||
header.payload_size = bytes_to_write.size();
|
{
|
||||||
header.fd_count = fds.size();
|
.type = MessageHeader::Type::Payload,
|
||||||
header.type = MessageHeader::Type::Payload;
|
.payload_size = static_cast<u32>(bytes_to_write.size()),
|
||||||
memcpy(message_buffer.data(), &header, sizeof(MessageHeader));
|
.fd_count = static_cast<u32>(num_fds_to_transfer),
|
||||||
memcpy(message_buffer.data() + sizeof(MessageHeader), bytes_to_write.data(), bytes_to_write.size());
|
},
|
||||||
|
bytes_to_write);
|
||||||
|
|
||||||
for (auto const& fd : fds)
|
for (auto const& fd : fds)
|
||||||
m_fds_retained_until_received_by_peer.enqueue(fd);
|
m_fds_retained_until_received_by_peer.enqueue(fd);
|
||||||
|
|
||||||
auto raw_fds = Vector<int, 1> {};
|
auto raw_fds = Vector<int, 1> {};
|
||||||
auto num_fds_to_transfer = fds.size();
|
|
||||||
if (num_fds_to_transfer > 0) {
|
if (num_fds_to_transfer > 0) {
|
||||||
raw_fds.ensure_capacity(num_fds_to_transfer);
|
raw_fds.ensure_capacity(num_fds_to_transfer);
|
||||||
for (auto& owned_fd : fds) {
|
for (auto const& owned_fd : fds) {
|
||||||
raw_fds.unchecked_append(owned_fd->value());
|
raw_fds.unchecked_append(owned_fd->value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue