From 314667259ed74ebe3f52c31190dc920769788dbd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 2 Aug 2024 16:52:17 +0200 Subject: [PATCH] net/SocketDescriptor: add Duplicate() method The the Duplicate() method we inherited from class FileDescriptor returns a UniqueFileDescriptor, but we really want to return a UniqueSocketDescriptor. --- src/net/SocketDescriptor.cxx | 11 ++++++++++- src/net/SocketDescriptor.hxx | 6 +++++- src/net/meson.build | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index 3c1c624f6..4752b7e14 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -6,6 +6,7 @@ #include "StaticSocketAddress.hxx" #include "IPv4Address.hxx" #include "IPv6Address.hxx" +#include "UniqueSocketDescriptor.hxx" #ifdef _WIN32 #include @@ -243,7 +244,15 @@ SocketDescriptor::SetNonBlocking() const noexcept return ioctlsocket(fd, FIONBIO, &val) == 0; } -#endif +#else + +UniqueSocketDescriptor +SocketDescriptor::Duplicate() const noexcept +{ + return UniqueSocketDescriptor{FileDescriptor::Duplicate()}; +} + +#endif // !_WIN32 bool SocketDescriptor::SetOption(int level, int name, diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index 49ec6c74d..1b2f0c01d 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -24,6 +24,7 @@ class SocketAddress; class StaticSocketAddress; class IPv4Address; class IPv6Address; +class UniqueSocketDescriptor; /** * An OO wrapper for a Berkeley or WinSock socket descriptor. @@ -137,7 +138,10 @@ public: using FileDescriptor::SetNonBlocking; using FileDescriptor::SetBlocking; - using FileDescriptor::Duplicate; + + [[nodiscard]] + UniqueSocketDescriptor Duplicate() const noexcept; + using FileDescriptor::CheckDuplicate; using FileDescriptor::Close; #else diff --git a/src/net/meson.build b/src/net/meson.build index cd817850a..ac60bd4a5 100644 --- a/src/net/meson.build +++ b/src/net/meson.build @@ -62,5 +62,6 @@ net_dep = declare_dependency( link_with: net, dependencies: [ system_dep, + io_dep, ], )