From 7cd38dde0972bad98673701f1f56f8343f3fbc37 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 11 Jan 2024 17:04:44 +0100 Subject: [PATCH] net/SocketDescriptor: add [[nodiscard]] --- src/net/SocketDescriptor.cxx | 2 +- src/net/SocketDescriptor.hxx | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index f90feae8f..b3398ec90 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -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; } diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index 143a88417..412f3f4ae 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -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 dest, int flags=0) const noexcept; /** @@ -284,12 +308,15 @@ public: * * MSG_NOSIGNAL is implicitly added (if available). */ + [[nodiscard]] ssize_t Send(std::span src, int flags=0) const noexcept; + [[nodiscard]] ssize_t Read(std::span dest) const noexcept { return Receive(dest); } + [[nodiscard]] ssize_t Write(std::span 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 dest) const noexcept; /** * Wrapper for Receive() with MSG_DONTWAIT (not available on * Windows). */ + [[nodiscard]] ssize_t WriteNoWait(std::span 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;