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 SocketDescriptor::GetIntOption(int level, int name, int fallback) const noexcept
{ {
int value = fallback; int value = fallback;
GetOption(level, name, &value, sizeof(value)); (void)GetOption(level, name, &value, sizeof(value));
return value; return value;
} }

View File

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