net/SocketDescriptor: add {Read,Write}NoWait()

It was surprising that Read() was non-blocking, but there was no
blocking version of it.  Let's make the non-blocking behavior explicit
and change Read() to be blocking.

In order to find existing callers easily with compiler errors, this
also refactors Read()/Write() to take a std::span parameter.
This commit is contained in:
Max Kellermann
2023-09-27 09:32:48 +02:00
committed by Max Kellermann
parent cad35a83fb
commit 491cc8f54d
7 changed files with 48 additions and 19 deletions

View File

@@ -274,12 +274,26 @@ public:
*/
ssize_t Send(std::span<const std::byte> src, int flags=0) const noexcept;
ssize_t Read(void *buffer, std::size_t length) const noexcept;
ssize_t Write(const void *buffer, std::size_t length) const noexcept {
return Send({static_cast<const std::byte *>(buffer), length});
ssize_t Read(std::span<std::byte> dest) const noexcept {
return Receive(dest);
}
ssize_t Write(std::span<const std::byte> src) const noexcept {
return Send(src);
}
/**
* Wrapper for Receive() with MSG_DONTWAIT (not available on
* Windows).
*/
ssize_t ReadNoWait(std::span<std::byte> dest) const noexcept;
/**
* Wrapper for Receive() with MSG_DONTWAIT (not available on
* Windows).
*/
ssize_t WriteNoWait(std::span<const std::byte> src) const noexcept;
#ifdef _WIN32
int WaitReadable(int timeout_ms) const noexcept;
int WaitWritable(int timeout_ms) const noexcept;