diff options
Diffstat (limited to 'src/network/socket/qtcpserver.cpp')
-rw-r--r-- | src/network/socket/qtcpserver.cpp | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/network/socket/qtcpserver.cpp b/src/network/socket/qtcpserver.cpp index 1f443010e4..a0c0f00aaa 100644 --- a/src/network/socket/qtcpserver.cpp +++ b/src/network/socket/qtcpserver.cpp @@ -18,7 +18,9 @@ Call listen() to have the server listen for incoming connections. The newConnection() signal is then emitted each time a client - connects to the server. + connects to the server. When the client connection has been added + to the pending connection queue using the addPendingConnection() + function, the pendingConnectionAvailable() signal is emitted. Call nextPendingConnection() to accept the pending connection as a connected QTcpSocket. The function returns a pointer to a @@ -41,17 +43,27 @@ use waitForNewConnection(), which blocks until either a connection is available or a timeout expires. - \sa QTcpSocket, {Fortune Server Example}, {Threaded Fortune Server Example}, - {Loopback Example}, {Torrent Example} + \sa QTcpSocket, {Fortune Server}, {Threaded Fortune Server}, + {Torrent Example} */ /*! \fn void QTcpServer::newConnection() - This signal is emitted every time a new connection is available. + This signal is emitted every time a new connection is available, regardless + of whether it has been added to the pending connections queue or not. \sa hasPendingConnections(), nextPendingConnection() */ +/*! \fn void QTcpServer::pendingConnectionAvailable() + + This signal is emitted every time a new connection has been added to the + pending connections queue. + + \sa hasPendingConnections(), nextPendingConnection() + \since 6.4 +*/ + /*! \fn void QTcpServer::acceptError(QAbstractSocket::SocketError socketError) \since 5.0 @@ -121,7 +133,7 @@ QNetworkProxy QTcpServerPrivate::resolveProxy(const QHostAddress &address, quint } // return the first that we can use - for (const QNetworkProxy &p : qAsConst(proxies)) { + for (const QNetworkProxy &p : std::as_const(proxies)) { if (socketType == QAbstractSocket::TcpSocket && (p.capabilities() & QNetworkProxy::ListeningCapability) != 0) return p; @@ -160,7 +172,7 @@ void QTcpServerPrivate::readNotification() { Q_Q(QTcpServer); for (;;) { - if (pendingConnections.count() >= maxConnections) { + if (totalPendingConnections() >= maxConnections) { #if defined (QTCPSERVER_DEBUG) qDebug("QTcpServerPrivate::_q_processIncomingConnection() too many connections"); #endif @@ -169,7 +181,7 @@ void QTcpServerPrivate::readNotification() return; } - int descriptor = socketEngine->accept(); + qintptr descriptor = socketEngine->accept(); if (descriptor == -1) { if (socketEngine->error() != QAbstractSocket::TemporaryError) { q->pauseAccepting(); @@ -182,16 +194,32 @@ void QTcpServerPrivate::readNotification() #if defined (QTCPSERVER_DEBUG) qDebug("QTcpServerPrivate::_q_processIncomingConnection() accepted socket %i", descriptor); #endif + QPointer<QTcpServer> that = q; q->incomingConnection(descriptor); - QPointer<QTcpServer> that = q; - emit q->newConnection(); + if (that) + emit q->newConnection(); + if (!that || !q->isListening()) return; } } /*! + \internal + Return the amount of sockets currently in queue for the server. + This is to make maxPendingConnections work properly with servers that don't + necessarily have 'ready-to-go' sockets as soon as they connect, + e.g. QSslServer. + By default we just return pendingConnections.size(), which is equivalent to + what it did before. +*/ +int QTcpServerPrivate::totalPendingConnections() const +{ + return int(pendingConnections.size()); +} + +/*! Constructs a QTcpServer object. \a parent is passed to the QObject constructor. @@ -470,7 +498,7 @@ bool QTcpServer::waitForNewConnection(int msec, bool *timedOut) if (d->state != QAbstractSocket::ListeningState) return false; - if (!d->socketEngine->waitForRead(msec, timedOut)) { + if (!d->socketEngine->waitForRead(QDeadlineTimer(msec), timedOut)) { d->serverSocketError = d->socketEngine->error(); d->serverSocketErrorString = d->socketEngine->errorString(); return false; @@ -572,14 +600,17 @@ void QTcpServer::incomingConnection(qintptr socketDescriptor) \note Don't forget to call this member from reimplemented incomingConnection() if you do not want to break the - Pending Connections mechanism. + Pending Connections mechanism. This function emits the + pendingConnectionAvailable() signal after the socket has been + added. - \sa incomingConnection() + \sa incomingConnection(), pendingConnectionAvailable() \since 4.7 */ void QTcpServer::addPendingConnection(QTcpSocket* socket) { d_func()->pendingConnections.append(socket); + emit pendingConnectionAvailable(QPrivateSignal()); } /*! |