diff options
Diffstat (limited to 'src/network/socket/qnativesocketengine_win.cpp')
-rw-r--r-- | src/network/socket/qnativesocketengine_win.cpp | 92 |
1 files changed, 31 insertions, 61 deletions
diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp index b2ab8a9fdf..6525f46e30 100644 --- a/src/network/socket/qnativesocketengine_win.cpp +++ b/src/network/socket/qnativesocketengine_win.cpp @@ -1,50 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtNetwork module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -// Prevent windows system header files from defining min/max as macros. -#define NOMINMAX 1 +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <winsock2.h> #include <ws2tcpip.h> -#include "qnativesocketengine_p.h" +#include "qnativesocketengine_p_p.h" #include <qabstracteventdispatcher.h> #include <qsocketnotifier.h> @@ -52,8 +13,10 @@ #include <qdatetime.h> #include <qnetworkinterface.h> #include <qoperatingsystemversion.h> +#include <qvarlengtharray.h> #include <algorithm> +#include <chrono> //#define QNATIVESOCKETENGINE_DEBUG #if defined(QNATIVESOCKETENGINE_DEBUG) @@ -808,10 +771,10 @@ bool QNativeSocketEnginePrivate::nativeListen(int backlog) return true; } -int QNativeSocketEnginePrivate::nativeAccept() +qintptr QNativeSocketEnginePrivate::nativeAccept() { - int acceptedDescriptor = WSAAccept(socketDescriptor, 0,0,0,0); - if (acceptedDescriptor == -1) { + SOCKET acceptedDescriptor = WSAAccept(socketDescriptor, 0,0,0,0); + if (acceptedDescriptor == INVALID_SOCKET) { int err = WSAGetLastError(); switch (err) { case WSAEACCES: @@ -845,19 +808,19 @@ int QNativeSocketEnginePrivate::nativeAccept() setError(QAbstractSocket::UnknownSocketError, UnknownSocketErrorString); break; } - } else if (acceptedDescriptor != -1 && QAbstractEventDispatcher::instance()) { + } else if (acceptedDescriptor != INVALID_SOCKET && QAbstractEventDispatcher::instance()) { // Because of WSAAsyncSelect() WSAAccept returns a non blocking socket // with the same attributes as the listening socket including the current // WSAAsyncSelect(). To be able to change the socket to blocking mode the - // WSAAsyncSelect() call must be cancled. + // WSAAsyncSelect() call must be canceled. QSocketNotifier n(acceptedDescriptor, QSocketNotifier::Read); n.setEnabled(true); n.setEnabled(false); } #if defined (QNATIVESOCKETENGINE_DEBUG) - qDebug("QNativeSocketEnginePrivate::nativeAccept() == %i", acceptedDescriptor); + qDebug("QNativeSocketEnginePrivate::nativeAccept() == %lld", qint64(acceptedDescriptor)); #endif - return acceptedDescriptor; + return qintptr(acceptedDescriptor); } static bool multicastMembershipHelper(QNativeSocketEnginePrivate *d, @@ -1466,7 +1429,18 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxLength) return ret; } -int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const +inline timeval durationToTimeval(std::chrono::nanoseconds dur) noexcept +{ + using namespace std::chrono; + const auto secs = duration_cast<seconds>(dur); + const auto frac = duration_cast<microseconds>(dur - secs); + struct timeval tval; + tval.tv_sec = secs.count(); + tval.tv_usec = frac.count(); + return tval; +} + +int QNativeSocketEnginePrivate::nativeSelect(QDeadlineTimer deadline, bool selectForRead) const { bool readEnabled = selectForRead && readNotifier && readNotifier->isEnabled(); if (readEnabled) @@ -1480,12 +1454,10 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co fds.fd_count = 1; fds.fd_array[0] = (SOCKET)socketDescriptor; - struct timeval tv; - tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; + struct timeval tv = durationToTimeval(deadline.remainingTimeAsDuration()); if (selectForRead) { - ret = select(0, &fds, 0, 0, timeout < 0 ? 0 : &tv); + ret = select(0, &fds, 0, 0, &tv); } else { // select for write @@ -1494,7 +1466,7 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co FD_ZERO(&fdexception); FD_SET((SOCKET)socketDescriptor, &fdexception); - ret = select(0, 0, &fds, &fdexception, timeout < 0 ? 0 : &tv); + ret = select(0, 0, &fds, &fdexception, &tv); // ... but if it is actually set, pretend it did not happen if (ret > 0 && FD_ISSET((SOCKET)socketDescriptor, &fdexception)) @@ -1507,7 +1479,7 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co return ret; } -int QNativeSocketEnginePrivate::nativeSelect(int timeout, +int QNativeSocketEnginePrivate::nativeSelect(QDeadlineTimer deadline, bool checkRead, bool checkWrite, bool *selectForRead, bool *selectForWrite) const { @@ -1536,11 +1508,9 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, FD_SET((SOCKET)socketDescriptor, &fdexception); } - struct timeval tv; - tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; + struct timeval tv = durationToTimeval(deadline.remainingTimeAsDuration()); - ret = select(socketDescriptor + 1, &fdread, &fdwrite, &fdexception, timeout < 0 ? 0 : &tv); + ret = select(socketDescriptor + 1, &fdread, &fdwrite, &fdexception, &tv); //... but if it is actually set, pretend it did not happen if (ret > 0 && FD_ISSET((SOCKET)socketDescriptor, &fdexception)) |