diff options
Diffstat (limited to 'examples/network')
-rw-r--r-- | examples/network/bearermonitor/bearermonitor.pro | 2 | ||||
-rw-r--r-- | examples/network/downloadmanager/downloadmanager.cpp | 4 | ||||
-rw-r--r-- | examples/network/downloadmanager/downloadmanager.h | 2 | ||||
-rw-r--r-- | examples/network/http/httpwindow.cpp | 24 | ||||
-rw-r--r-- | examples/network/http/httpwindow.h | 8 | ||||
-rw-r--r-- | examples/network/http/main.cpp | 4 | ||||
-rw-r--r-- | examples/network/secureudpserver/server.cpp | 23 | ||||
-rw-r--r-- | examples/network/secureudpserver/server.h | 8 | ||||
-rw-r--r-- | examples/network/torrent/ratecontroller.cpp | 14 | ||||
-rw-r--r-- | examples/network/torrent/ratecontroller.h | 4 | ||||
-rw-r--r-- | examples/network/torrent/torrentclient.cpp | 60 |
11 files changed, 81 insertions, 72 deletions
diff --git a/examples/network/bearermonitor/bearermonitor.pro b/examples/network/bearermonitor/bearermonitor.pro index 7d90b408e0..16ac41298a 100644 --- a/examples/network/bearermonitor/bearermonitor.pro +++ b/examples/network/bearermonitor/bearermonitor.pro @@ -13,7 +13,7 @@ FORMS = bearermonitor_240_320.ui \ bearermonitor_640_480.ui \ sessionwidget.ui -win32:LIBS += -lws2_32 +win32: QMAKE_USE += ws2_32 CONFIG += console diff --git a/examples/network/downloadmanager/downloadmanager.cpp b/examples/network/downloadmanager/downloadmanager.cpp index e820b4ff70..9e0c03c6af 100644 --- a/examples/network/downloadmanager/downloadmanager.cpp +++ b/examples/network/downloadmanager/downloadmanager.cpp @@ -132,7 +132,7 @@ void DownloadManager::startNextDownload() // prepare the output printf("Downloading %s...\n", url.toEncoded().constData()); - downloadTime.start(); + downloadTimer.start(); } void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) @@ -140,7 +140,7 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) progressBar.setStatus(bytesReceived, bytesTotal); // calculate the download speed - double speed = bytesReceived * 1000.0 / downloadTime.elapsed(); + double speed = bytesReceived * 1000.0 / downloadTimer.elapsed(); QString unit; if (speed < 1024) { unit = "bytes/sec"; diff --git a/examples/network/downloadmanager/downloadmanager.h b/examples/network/downloadmanager/downloadmanager.h index 4bc6351ff9..818a774f1f 100644 --- a/examples/network/downloadmanager/downloadmanager.h +++ b/examples/network/downloadmanager/downloadmanager.h @@ -83,7 +83,7 @@ private: QQueue<QUrl> downloadQueue; QNetworkReply *currentDownload = nullptr; QFile output; - QTime downloadTime; + QElapsedTimer downloadTimer; TextProgressBar progressBar; int downloadedCount = 0; diff --git a/examples/network/http/httpwindow.cpp b/examples/network/http/httpwindow.cpp index 39ffb3cc87..c7bf0c0dff 100644 --- a/examples/network/http/httpwindow.cpp +++ b/examples/network/http/httpwindow.cpp @@ -48,13 +48,14 @@ ** ****************************************************************************/ +#include "httpwindow.h" + +#include "ui_authenticationdialog.h" + #include <QtWidgets> #include <QtNetwork> #include <QUrl> -#include "httpwindow.h" -#include "ui_authenticationdialog.h" - #if QT_CONFIG(ssl) const char defaultUrl[] = "https://www.qt.io/"; #else @@ -74,6 +75,10 @@ ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent) setMinimumSize(QSize(400, 75)); } +ProgressDialog::~ProgressDialog() +{ +} + void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes) { setMaximum(totalBytes); @@ -137,6 +142,10 @@ HttpWindow::HttpWindow(QWidget *parent) urlLineEdit->setFocus(); } +HttpWindow::~HttpWindow() +{ +} + void HttpWindow::startRequest(const QUrl &requestedUrl) { url = requestedUrl; @@ -204,9 +213,9 @@ void HttpWindow::downloadFile() startRequest(newUrl); } -QFile *HttpWindow::openFileForWrite(const QString &fileName) +std::unique_ptr<QFile> HttpWindow::openFileForWrite(const QString &fileName) { - QScopedPointer<QFile> file(new QFile(fileName)); + std::unique_ptr<QFile> file(new QFile(fileName)); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("Error"), tr("Unable to save the file %1: %2.") @@ -214,7 +223,7 @@ QFile *HttpWindow::openFileForWrite(const QString &fileName) file->errorString())); return nullptr; } - return file.take(); + return file; } void HttpWindow::cancelDownload() @@ -231,8 +240,7 @@ void HttpWindow::httpFinished() if (file) { fi.setFile(file->fileName()); file->close(); - delete file; - file = nullptr; + file.reset(); } if (httpRequestAborted) { diff --git a/examples/network/http/httpwindow.h b/examples/network/http/httpwindow.h index 20ad2bb4da..f7bd0047de 100644 --- a/examples/network/http/httpwindow.h +++ b/examples/network/http/httpwindow.h @@ -55,6 +55,8 @@ #include <QNetworkAccessManager> #include <QUrl> +#include <memory> + QT_BEGIN_NAMESPACE class QFile; class QLabel; @@ -72,6 +74,7 @@ class ProgressDialog : public QProgressDialog { public: explicit ProgressDialog(const QUrl &url, QWidget *parent = nullptr); + ~ProgressDialog(); public slots: void networkReplyProgress(qint64 bytesRead, qint64 totalBytes); @@ -83,6 +86,7 @@ class HttpWindow : public QDialog public: explicit HttpWindow(QWidget *parent = nullptr); + ~HttpWindow(); void startRequest(const QUrl &requestedUrl); @@ -98,7 +102,7 @@ private slots: #endif private: - QFile *openFileForWrite(const QString &fileName); + std::unique_ptr<QFile> openFileForWrite(const QString &fileName); QLabel *statusLabel; QLineEdit *urlLineEdit; @@ -110,7 +114,7 @@ private: QUrl url; QNetworkAccessManager qnam; QNetworkReply *reply; - QFile *file; + std::unique_ptr<QFile> file; bool httpRequestAborted; }; diff --git a/examples/network/http/main.cpp b/examples/network/http/main.cpp index b7d254ff22..f126c7846a 100644 --- a/examples/network/http/main.cpp +++ b/examples/network/http/main.cpp @@ -49,8 +49,8 @@ ****************************************************************************/ #include <QApplication> -#include <QDesktopWidget> #include <QDir> +#include <QScreen> #include "httpwindow.h" @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); HttpWindow httpWin; - const QRect availableSize = QApplication::desktop()->availableGeometry(&httpWin); + const QRect availableSize = httpWin.screen()->availableGeometry(); httpWin.resize(availableSize.width() / 5, availableSize.height() / 5); httpWin.move((availableSize.width() - httpWin.width()) / 2, (availableSize.height() - httpWin.height()) / 2); diff --git a/examples/network/secureudpserver/server.cpp b/examples/network/secureudpserver/server.cpp index 0d83fa9b51..450eb9e68d 100644 --- a/examples/network/secureudpserver/server.cpp +++ b/examples/network/secureudpserver/server.cpp @@ -62,7 +62,7 @@ QString peer_info(const QHostAddress &address, quint16 port) return info.arg(address.toString()).arg(port); } -QString connection_info(QSharedPointer<QDtls> connection) +QString connection_info(QDtls *connection) { QString info(DtlsServer::tr("Session cipher: ")); info += connection->sessionCipher().name(); @@ -157,7 +157,7 @@ void DtlsServer::readyRead() } const auto client = std::find_if(knownClients.begin(), knownClients.end(), - [&](const DtlsConnection &connection){ + [&](const std::unique_ptr<QDtls> &connection){ return connection->peerAddress() == peerAddress && connection->peerPort() == peerPort; }); @@ -170,7 +170,7 @@ void DtlsServer::readyRead() //! [6] if ((*client)->isConnectionEncrypted()) { - decryptDatagram(*client, dgram); + decryptDatagram(client->get(), dgram); if ((*client)->dtlsError() == QDtlsError::RemoteClosedConnectionError) knownClients.erase(client); return; @@ -178,7 +178,7 @@ void DtlsServer::readyRead() //! [6] //! [7] - doHandshake(*client, dgram); + doHandshake(client->get(), dgram); //! [7] } @@ -205,13 +205,13 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, emit infoMessage(peerInfo + tr(": verified, starting a handshake")); //! [8] //! [9] - DtlsConnection newConnection(new QDtls(QSslSocket::SslServerMode)); + std::unique_ptr<QDtls> newConnection{new QDtls{QSslSocket::SslServerMode}}; newConnection->setDtlsConfiguration(serverConfiguration); newConnection->setPeer(peerAddress, peerPort); - newConnection->connect(newConnection.data(), &QDtls::pskRequired, + newConnection->connect(newConnection.get(), &QDtls::pskRequired, this, &DtlsServer::pskRequired); - knownClients.push_back(newConnection); - doHandshake(newConnection, clientHello); + knownClients.push_back(std::move(newConnection)); + doHandshake(knownClients.back().get(), clientHello); //! [9] } else if (cookieSender.dtlsError() != QDtlsError::NoError) { emit errorMessage(tr("DTLS error: ") + cookieSender.dtlsErrorString()); @@ -221,7 +221,7 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, } //! [11] -void DtlsServer::doHandshake(DtlsConnection newConnection, const QByteArray &clientHello) +void DtlsServer::doHandshake(QDtls *newConnection, const QByteArray &clientHello) { const bool result = newConnection->doHandshake(&serverSocket, clientHello); if (!result) { @@ -246,7 +246,7 @@ void DtlsServer::doHandshake(DtlsConnection newConnection, const QByteArray &cli //! [11] //! [12] -void DtlsServer::decryptDatagram(DtlsConnection connection, const QByteArray &clientMessage) +void DtlsServer::decryptDatagram(QDtls *connection, const QByteArray &clientMessage) { Q_ASSERT(connection->isConnectionEncrypted()); @@ -266,10 +266,9 @@ void DtlsServer::decryptDatagram(DtlsConnection connection, const QByteArray &cl //! [14] void DtlsServer::shutdown() { - for (DtlsConnection &connection : knownClients) + for (const auto &connection : qExchange(knownClients, {})) connection->shutdown(&serverSocket); - knownClients.clear(); serverSocket.close(); } //! [14] diff --git a/examples/network/secureudpserver/server.h b/examples/network/secureudpserver/server.h index b720368e7b..1af8aef8a2 100644 --- a/examples/network/secureudpserver/server.h +++ b/examples/network/secureudpserver/server.h @@ -54,6 +54,7 @@ #include <QtNetwork> #include <vector> +#include <memory> QT_BEGIN_NAMESPACE @@ -86,9 +87,8 @@ private: void handleNewConnection(const QHostAddress &peerAddress, quint16 peerPort, const QByteArray &clientHello); - using DtlsConnection = QSharedPointer<QDtls>; - void doHandshake(DtlsConnection newConnection, const QByteArray &clientHello); - void decryptDatagram(DtlsConnection connection, const QByteArray &clientMessage); + void doHandshake(QDtls *newConnection, const QByteArray &clientHello); + void decryptDatagram(QDtls *connection, const QByteArray &clientMessage); void shutdown(); bool listening = false; @@ -96,7 +96,7 @@ private: QSslConfiguration serverConfiguration; QDtlsClientVerifier cookieSender; - QVector<DtlsConnection> knownClients; + std::vector<std::unique_ptr<QDtls>> knownClients; Q_DISABLE_COPY(DtlsServer) }; diff --git a/examples/network/torrent/ratecontroller.cpp b/examples/network/torrent/ratecontroller.cpp index 47b49dba30..96474806f5 100644 --- a/examples/network/torrent/ratecontroller.cpp +++ b/examples/network/torrent/ratecontroller.cpp @@ -96,8 +96,8 @@ void RateController::transfer() if (sockets.isEmpty()) return; - int msecs = 1000; - if (!stopWatch.isNull()) + qint64 msecs = 1000; + if (stopWatch.isValid()) msecs = qMin(msecs, stopWatch.elapsed()); qint64 bytesToWrite = (upLimit * msecs) / 1000; @@ -123,11 +123,11 @@ void RateController::transfer() qint64 writeChunk = qMax<qint64>(1, bytesToWrite / pendingSockets.size()); qint64 readChunk = qMax<qint64>(1, bytesToRead / pendingSockets.size()); - QSetIterator<PeerWireClient *> it(pendingSockets); - while (it.hasNext() && (bytesToWrite > 0 || bytesToRead > 0)) { - PeerWireClient *socket = it.next(); + for (auto it = pendingSockets.begin(), end = pendingSockets.end(); it != end && (bytesToWrite > 0 || bytesToRead > 0); /*erasing*/) { + auto current = it++; + PeerWireClient *socket = *current; if (socket->state() != QAbstractSocket::ConnectedState) { - pendingSockets.remove(socket); + pendingSockets.erase(current); continue; } @@ -156,7 +156,7 @@ void RateController::transfer() if (dataTransferred && socket->canTransferMore()) canTransferMore = true; else - pendingSockets.remove(socket); + pendingSockets.erase(current); } } while (canTransferMore && (bytesToWrite > 0 || bytesToRead > 0) && !pendingSockets.isEmpty()); diff --git a/examples/network/torrent/ratecontroller.h b/examples/network/torrent/ratecontroller.h index a4aa596ce1..f8bff0cc36 100644 --- a/examples/network/torrent/ratecontroller.h +++ b/examples/network/torrent/ratecontroller.h @@ -53,7 +53,7 @@ #include <QObject> #include <QSet> -#include <QTime> +#include <QElapsedTimer> class PeerWireClient; @@ -79,7 +79,7 @@ public slots: void scheduleTransfer(); private: - QTime stopWatch; + QElapsedTimer stopWatch; QSet<PeerWireClient *> sockets; int upLimit; int downLimit; diff --git a/examples/network/torrent/torrentclient.cpp b/examples/network/torrent/torrentclient.cpp index d01a5f3d9e..bddf3caa1a 100644 --- a/examples/network/torrent/torrentclient.cpp +++ b/examples/network/torrent/torrentclient.cpp @@ -720,9 +720,9 @@ QList<TorrentPeer *> TorrentClient::weighedFreePeers() const qint64 now = QDateTime::currentSecsSinceEpoch(); QList<TorrentPeer *> freePeers; QMap<QString, int> connectionsPerPeer; - for (TorrentPeer *peer : d->peers) { + for (TorrentPeer *peer : qAsConst(d->peers)) { bool busy = false; - for (PeerWireClient *client : d->connections) { + for (PeerWireClient *client : qAsConst(d->connections)) { if (client->state() == PeerWireClient::ConnectedState && client->peerAddress() == peer->address && client->peerPort() == peer->port) { @@ -874,8 +874,7 @@ void TorrentClient::removeClient() // Remove the client from RateController and all structures. RateController::instance()->removeSocket(client); d->connections.removeAll(client); - QMultiMap<PeerWireClient *, TorrentPiece *>::Iterator it = d->payloads.find(client); - while (it != d->payloads.end() && it.key() == client) { + for (auto it = d->payloads.find(client); it != d->payloads.end() && it.key() == client; /*erasing*/) { TorrentPiece *piece = it.value(); piece->inProgress = false; piece->requestedBlocks.fill(false); @@ -883,9 +882,12 @@ void TorrentClient::removeClient() } // Remove pending read requests. - QMapIterator<int, PeerWireClient *> it2(d->readIds); - while (it2.findNext(client)) - d->readIds.remove(it2.key()); + for (auto it = d->readIds.begin(), end = d->readIds.end(); it != end; /*erasing*/) { + if (it.value() == client) + it = d->readIds.erase(it); + else + ++it; + } // Delete the client later. disconnect(client, SIGNAL(disconnected()), this, SLOT(removeClient())); @@ -1084,25 +1086,25 @@ void TorrentClient::scheduleUploads() // seeding, we sort by upload speed. Seeds are left out; there's // no use in unchoking them. QList<PeerWireClient *> allClients = d->connections; - QMultiMap<int, PeerWireClient *> transferSpeeds; + QVector<QPair<qint64, PeerWireClient *>> transferSpeeds; for (PeerWireClient *client : qAsConst(allClients)) { if (client->state() == QAbstractSocket::ConnectedState && client->availablePieces().count(true) != d->pieceCount) { if (d->state == Seeding) { - transferSpeeds.insert(client->uploadSpeed(), client); + transferSpeeds.push_back({client->uploadSpeed(), client}); } else { - transferSpeeds.insert(client->downloadSpeed(), client); + transferSpeeds.push_back({client->downloadSpeed(), client}); } } } + std::sort(transferSpeeds.begin(), transferSpeeds.end()); + // Unchoke the top 'MaxUploads' downloaders (peers that we are // uploading to) and choke all others. int maxUploaders = MaxUploads; - QMapIterator<int, PeerWireClient *> it(transferSpeeds); - it.toBack(); - while (it.hasPrevious()) { - PeerWireClient *client = it.previous().value(); + for (auto rit = transferSpeeds.crbegin(), rend = transferSpeeds.crend(); rit != rend; ++rit) { + PeerWireClient *client = rit->second; bool interested = (client->peerWireState() & PeerWireClient::PeerIsInterested); if (maxUploaders) { @@ -1484,30 +1486,26 @@ void TorrentClient::addToPeerList(const QList<TorrentPeer> &peerList) // of the peers that have no (or low) activity. int maxPeers = ConnectionManager::instance()->maxConnections() * 3; if (d->peers.size() > maxPeers) { + auto tooMany = d->peers.size() - maxPeers; + // Find what peers are currently connected & active - QSet<TorrentPeer *> activePeers; - for (TorrentPeer *peer : qAsConst(d->peers)) { + const auto firstNInactivePeers = [&tooMany, this] (TorrentPeer *peer) { + if (!tooMany) + return false; for (const PeerWireClient *client : qAsConst(d->connections)) { if (client->peer() == peer && (client->downloadSpeed() + client->uploadSpeed()) > 1024) - activePeers << peer; + return false; } - } - + --tooMany; + return true; + }; // Remove inactive peers from the peer list until we're below // the max connections count. - QList<int> toRemove; - for (int i = 0; i < d->peers.size() && (d->peers.size() - toRemove.size()) > maxPeers; ++i) { - if (!activePeers.contains(d->peers.at(i))) - toRemove << i; - } - QListIterator<int> toRemoveIterator(toRemove); - toRemoveIterator.toBack(); - while (toRemoveIterator.hasPrevious()) - d->peers.removeAt(toRemoveIterator.previous()); - + d->peers.erase(std::remove_if(d->peers.begin(), d->peers.end(), + firstNInactivePeers), + d->peers.end()); // If we still have too many peers, remove the oldest ones. - while (d->peers.size() > maxPeers) - d->peers.takeFirst(); + d->peers.erase(d->peers.begin(), d->peers.begin() + tooMany); } if (d->state != Paused && d->state != Stopping && d->state != Idle) { |