From 3ab5db438436a1a68ae76fe8799e1a3cbe8bde0b Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 17 Mar 2015 17:06:22 +0100 Subject: [PATCH] Replacement for dup() on Windows It doesn't work on sockets, which require a bit more care. --- common/network/TcpSocket.cxx | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx index bd9971c1..1ed6df08 100644 --- a/common/network/TcpSocket.cxx +++ b/common/network/TcpSocket.cxx @@ -120,6 +120,23 @@ static void initSockets() { } +// -=- Socket duplication help for Windows +static int dupsocket(int fd) +{ +#ifdef WIN32 + int ret; + WSAPROTOCOL_INFO info; + ret = WSADuplicateSocket(fd, GetCurrentProcessId(), &info); + if (ret != 0) + throw SocketException("unable to duplicate socket", errorNumber); + return WSASocket(info.iAddressFamily, info.iSocketType, info.iProtocol, + &info, 0, 0); +#else + return dup(fd); +#endif +} + + // -=- TcpSocket TcpSocket::TcpSocket(int sock, bool close) @@ -432,7 +449,7 @@ TcpListener::TcpListener(int sock) TcpListener::TcpListener(const TcpListener& other) { - fd = dup (other.fd); + fd = dupsocket (other.fd); // Hope TcpListener::shutdown(other) doesn't get called... } @@ -441,7 +458,7 @@ TcpListener& TcpListener::operator= (const TcpListener& other) if (this != &other) { closesocket (fd); - fd = dup (other.fd); + fd = dupsocket (other.fd); // Hope TcpListener::shutdown(other) doesn't get called... } return *this; -- 2.39.5