net/SocketDescriptor: add sendmsg(), recvmsg() wrappers

This commit is contained in:
Max Kellermann 2024-01-12 11:55:25 +01:00 committed by Max Kellermann
parent a91920a8ff
commit 974ed0166c
2 changed files with 30 additions and 0 deletions

View File

@ -417,6 +417,12 @@ SocketDescriptor::Receive(std::span<std::byte> dest, int flags) const noexcept
return ::recv(Get(), (char *)dest.data(), dest.size(), flags);
}
ssize_t
SocketDescriptor::Receive(struct msghdr &msg, int flags) const noexcept
{
return ::recvmsg(Get(), &msg, flags);
}
ssize_t
SocketDescriptor::Send(std::span<const std::byte> src, int flags) const noexcept
{
@ -427,6 +433,16 @@ SocketDescriptor::Send(std::span<const std::byte> src, int flags) const noexcept
return ::send(Get(), (const char *)src.data(), src.size(), flags);
}
ssize_t
SocketDescriptor::Send(const struct msghdr &msg, int flags) const noexcept
{
#ifdef __linux__
flags |= MSG_NOSIGNAL;
#endif
return ::sendmsg(Get(), &msg, flags);
}
ssize_t
SocketDescriptor::ReadNoWait(std::span<std::byte> dest) const noexcept
{

View File

@ -303,6 +303,12 @@ public:
[[nodiscard]]
ssize_t Receive(std::span<std::byte> dest, int flags=0) const noexcept;
/**
* Wrapper for recvmsg().
*/
[[nodiscard]]
ssize_t Receive(struct msghdr &msg, int flags=0) const noexcept;
/**
* Wrapper for send().
*
@ -311,6 +317,14 @@ public:
[[nodiscard]]
ssize_t Send(std::span<const std::byte> src, int flags=0) const noexcept;
/**
* Wrapper for sendmsg().
*
* MSG_NOSIGNAL is implicitly added (if available).
*/
[[nodiscard]]
ssize_t Send(const struct msghdr &msg, int flags=0) const noexcept;
[[nodiscard]]
ssize_t Read(std::span<std::byte> dest) const noexcept {
return Receive(dest);