net/SocketDescriptor: add [[nodiscard]]

This commit is contained in:
Max Kellermann 2024-01-11 17:04:44 +01:00 committed by Max Kellermann
parent b63a794fbe
commit 7cd38dde09
2 changed files with 35 additions and 1 deletions

View File

@ -214,7 +214,7 @@ int
SocketDescriptor::GetIntOption(int level, int name, int fallback) const noexcept
{
int value = fallback;
GetOption(level, name, &value, sizeof(value));
(void)GetOption(level, name, &value, sizeof(value));
return value;
}

View File

@ -39,17 +39,21 @@ protected:
special type */
SOCKET fd;
#else // !_WIN32
[[nodiscard]]
explicit constexpr SocketDescriptor(FileDescriptor _fd) noexcept
:FileDescriptor(_fd) {}
#endif // !_WIN32
public:
[[nodiscard]]
SocketDescriptor() = default;
#ifdef _WIN32
[[nodiscard]]
explicit constexpr SocketDescriptor(SOCKET _fd) noexcept
:fd(_fd) {}
#else // !_WIN32
[[nodiscard]]
explicit constexpr SocketDescriptor(int _fd) noexcept
:FileDescriptor(_fd) {}
#endif // !_WIN32
@ -69,6 +73,7 @@ public:
* same as file descriptors (i.e. not on Windows). Use this only
* when you know what you're doing.
*/
[[nodiscard]]
static constexpr SocketDescriptor FromFileDescriptor(FileDescriptor fd) noexcept {
return SocketDescriptor(fd);
}
@ -79,6 +84,7 @@ public:
* same as file descriptors (i.e. not on Windows). Use this only
* when you know what you're doing.
*/
[[nodiscard]]
constexpr const FileDescriptor &ToFileDescriptor() const noexcept {
return *this;
}
@ -110,6 +116,7 @@ public:
int GetProtocol() const noexcept;
#endif // __linux__
[[nodiscard]]
static constexpr SocketDescriptor Undefined() noexcept {
#ifdef _WIN32
return SocketDescriptor{INVALID_SOCKET};
@ -133,10 +140,12 @@ public:
using FileDescriptor::CheckDuplicate;
using FileDescriptor::Close;
#else
[[nodiscard]]
constexpr SOCKET Get() const noexcept {
return fd;
}
[[nodiscard]]
constexpr void Set(SOCKET _fd) noexcept {
fd = _fd;
}
@ -145,6 +154,7 @@ public:
fd = INVALID_SOCKET;
}
[[nodiscard]]
constexpr SOCKET Steal() noexcept {
return std::exchange(fd, INVALID_SOCKET);
}
@ -172,17 +182,22 @@ public:
* @return True on success, False on failure
* See man 2 socket for detailed information
*/
[[nodiscard]]
bool Create(int domain, int type, int protocol) noexcept;
/**
* Like Create(), but enable non-blocking mode.
*/
[[nodiscard]]
bool CreateNonBlock(int domain, int type, int protocol) noexcept;
#ifndef _WIN32
[[nodiscard]]
static bool CreateSocketPair(int domain, int type, int protocol,
SocketDescriptor &a,
SocketDescriptor &b) noexcept;
[[nodiscard]]
static bool CreateSocketPairNonBlock(int domain, int type, int protocol,
SocketDescriptor &a,
SocketDescriptor &b) noexcept;
@ -194,6 +209,7 @@ public:
/**
* @return the value size or 0 on error
*/
[[nodiscard]]
std::size_t GetOption(int level, int name,
void *value, std::size_t size) const noexcept;
@ -260,12 +276,19 @@ public:
bool AutoBind() const noexcept;
#endif
[[nodiscard]]
bool Listen(int backlog) const noexcept;
[[nodiscard]]
SocketDescriptor Accept() const noexcept;
[[nodiscard]]
SocketDescriptor AcceptNonBlock() const noexcept;
[[nodiscard]]
SocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const noexcept;
[[nodiscard]]
bool Connect(SocketAddress address) const noexcept;
[[gnu::pure]]
@ -277,6 +300,7 @@ public:
/**
* Wrapper for recv().
*/
[[nodiscard]]
ssize_t Receive(std::span<std::byte> dest, int flags=0) const noexcept;
/**
@ -284,12 +308,15 @@ public:
*
* MSG_NOSIGNAL is implicitly added (if available).
*/
[[nodiscard]]
ssize_t Send(std::span<const std::byte> src, int flags=0) const noexcept;
[[nodiscard]]
ssize_t Read(std::span<std::byte> dest) const noexcept {
return Receive(dest);
}
[[nodiscard]]
ssize_t Write(std::span<const std::byte> src) const noexcept {
return Send(src);
}
@ -298,16 +325,21 @@ public:
* Wrapper for Receive() with MSG_DONTWAIT (not available on
* Windows).
*/
[[nodiscard]]
ssize_t ReadNoWait(std::span<std::byte> dest) const noexcept;
/**
* Wrapper for Receive() with MSG_DONTWAIT (not available on
* Windows).
*/
[[nodiscard]]
ssize_t WriteNoWait(std::span<const std::byte> src) const noexcept;
#ifdef _WIN32
[[nodiscard]]
int WaitReadable(int timeout_ms) const noexcept;
[[nodiscard]]
int WaitWritable(int timeout_ms) const noexcept;
#else
using FileDescriptor::WaitReadable;
@ -318,12 +350,14 @@ public:
/**
* Receive a datagram and return the source address.
*/
[[nodiscard]]
ssize_t Read(void *buffer, std::size_t length,
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;