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

View File

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