net/SocketDescriptor: pass span<byte> to Read()/Write()

This commit is contained in:
Max Kellermann 2024-01-11 17:06:57 +01:00 committed by Max Kellermann
parent 7cd38dde09
commit a91920a8ff
2 changed files with 13 additions and 10 deletions

View File

@ -492,8 +492,8 @@ SocketDescriptor::WaitWritable(int timeout_ms) const noexcept
#endif
ssize_t
SocketDescriptor::Read(void *buffer, std::size_t length,
StaticSocketAddress &address) const noexcept
SocketDescriptor::ReadNoWait(std::span<std::byte> dest,
StaticSocketAddress &address) const noexcept
{
int flags = 0;
#ifndef _WIN32
@ -501,7 +501,9 @@ SocketDescriptor::Read(void *buffer, std::size_t length,
#endif
socklen_t addrlen = address.GetCapacity();
ssize_t nbytes = ::recvfrom(Get(), (char *)buffer, length, flags,
ssize_t nbytes = ::recvfrom(Get(),
reinterpret_cast<char *>(dest.data()),
dest.size(), flags,
address, &addrlen);
if (nbytes > 0)
address.SetSize(addrlen);
@ -510,8 +512,8 @@ SocketDescriptor::Read(void *buffer, std::size_t length,
}
ssize_t
SocketDescriptor::Write(const void *buffer, std::size_t length,
SocketAddress address) const noexcept
SocketDescriptor::WriteNoWait(std::span<const std::byte> src,
SocketAddress address) const noexcept
{
int flags = 0;
#ifndef _WIN32
@ -521,7 +523,8 @@ SocketDescriptor::Write(const void *buffer, std::size_t length,
flags |= MSG_NOSIGNAL;
#endif
return ::sendto(Get(), (const char *)buffer, length, flags,
return ::sendto(Get(), reinterpret_cast<const char *>(src.data()),
src.size(), flags,
address.GetAddress(), address.GetSize());
}

View File

@ -351,15 +351,15 @@ public:
* Receive a datagram and return the source address.
*/
[[nodiscard]]
ssize_t Read(void *buffer, std::size_t length,
StaticSocketAddress &address) const noexcept;
ssize_t ReadNoWait(std::span<std::byte> dest,
StaticSocketAddress &address) const noexcept;
/**
* Send a datagram to the specified address.
*/
[[nodiscard]]
ssize_t Write(const void *buffer, std::size_t length,
SocketAddress address) const noexcept;
ssize_t WriteNoWait(std::span<const std::byte> src,
SocketAddress address) const noexcept;
#ifndef _WIN32
void Shutdown() const noexcept;