From a91920a8ffe836346fd66ddf7971cda4f19071c6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 11 Jan 2024 17:06:57 +0100 Subject: [PATCH] net/SocketDescriptor: pass span to Read()/Write() --- src/net/SocketDescriptor.cxx | 15 +++++++++------ src/net/SocketDescriptor.hxx | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index b3398ec90..8cdce6f6e 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -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 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(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 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(src.data()), + src.size(), flags, address.GetAddress(), address.GetSize()); } diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index 412f3f4ae..03a994bb0 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -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 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 src, + SocketAddress address) const noexcept; #ifndef _WIN32 void Shutdown() const noexcept;