summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/network/torrent/trackerclient.cpp37
-rw-r--r--examples/network/torrent/trackerclient.h13
2 files changed, 32 insertions, 18 deletions
diff --git a/examples/network/torrent/trackerclient.cpp b/examples/network/torrent/trackerclient.cpp
index c8525e983f..43c60ac9bd 100644
--- a/examples/network/torrent/trackerclient.cpp
+++ b/examples/network/torrent/trackerclient.cpp
@@ -45,6 +45,7 @@
#include "trackerclient.h"
#include <QtCore>
+#include <QNetworkRequest>
TrackerClient::TrackerClient(TorrentClient *downloader, QObject *parent)
: QObject(parent), torrentDownloader(downloader)
@@ -56,7 +57,7 @@ TrackerClient::TrackerClient(TorrentClient *downloader, QObject *parent)
lastTrackerRequest = false;
firstSeeding = true;
- connect(&http, SIGNAL(done(bool)), this, SLOT(httpRequestDone(bool)));
+ connect(&http, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpRequestDone(QNetworkReply*)));
}
void TrackerClient::start(const MetaInfo &info)
@@ -82,15 +83,13 @@ void TrackerClient::startSeeding()
void TrackerClient::stop()
{
lastTrackerRequest = true;
- http.abort();
fetchPeerList();
}
void TrackerClient::timerEvent(QTimerEvent *event)
{
if (event->timerId() == requestIntervalTimer) {
- if (http.state() == QHttp::Unconnected)
- fetchPeerList();
+ fetchPeerList();
} else {
QObject::timerEvent(event);
}
@@ -148,32 +147,35 @@ void TrackerClient::fetchPeerList()
if (!trackerId.isEmpty())
query += "&trackerid=" + trackerId;
- http.setHost(url.host(), url.port() == -1 ? 80 : url.port());
- if (!url.userName().isEmpty())
- http.setUser(url.userName(), url.password());
- http.get(query);
+ QNetworkRequest req(url);
+ if (!url.userName().isEmpty()) {
+ uname = url.userName();
+ pwd = url.password();
+ connect(&http, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
+ }
+ http.get(req);
}
-void TrackerClient::httpRequestDone(bool error)
+void TrackerClient::httpRequestDone(QNetworkReply *reply)
{
+ reply->deleteLater();
if (lastTrackerRequest) {
emit stopped();
return;
}
- if (error) {
- emit connectionError(http.error());
+ if (reply->error() != QNetworkReply::NoError) {
+ emit connectionError(reply->error());
return;
}
- QByteArray response = http.readAll();
- http.abort();
+ QByteArray response = reply->readAll();
+ reply->abort();
BencodeParser parser;
if (!parser.parse(response)) {
qWarning("Error parsing bencode response from tracker: %s",
qPrintable(parser.errorString()));
- http.abort();
return;
}
@@ -234,3 +236,10 @@ void TrackerClient::httpRequestDone(bool error)
emit peerListUpdated(peers);
}
}
+
+void TrackerClient::provideAuthentication(QNetworkReply *reply, QAuthenticator *auth)
+{
+ Q_UNUSED(reply);
+ auth->setUser(uname);
+ auth->setPassword(pwd);
+}
diff --git a/examples/network/torrent/trackerclient.h b/examples/network/torrent/trackerclient.h
index 02dd5f91e7..723b3aa4b7 100644
--- a/examples/network/torrent/trackerclient.h
+++ b/examples/network/torrent/trackerclient.h
@@ -45,7 +45,9 @@
#include <QList>
#include <QObject>
#include <QHostAddress>
-#include <QHttp>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QAuthenticator>
#include "metainfo.h"
#include "torrentclient.h"
@@ -64,7 +66,7 @@ public:
void startSeeding();
signals:
- void connectionError(QHttp::Error error);
+ void connectionError(QNetworkReply::NetworkError error);
void failure(const QString &reason);
void warning(const QString &message);
@@ -80,20 +82,23 @@ protected:
private slots:
void fetchPeerList();
- void httpRequestDone(bool error);
+ void httpRequestDone(QNetworkReply *reply);
+ void provideAuthentication(QNetworkReply *reply, QAuthenticator *auth);
private:
TorrentClient *torrentDownloader;
int requestInterval;
int requestIntervalTimer;
- QHttp http;
+ QNetworkAccessManager http;
MetaInfo metaInfo;
QByteArray trackerId;
QList<TorrentPeer> peers;
qint64 uploadedBytes;
qint64 downloadedBytes;
qint64 length;
+ QString uname;
+ QString pwd;
bool firstTrackerRequest;
bool lastTrackerRequest;