-
Notifications
You must be signed in to change notification settings - Fork 15
/
lqtutils_net.cpp
151 lines (127 loc) · 3.88 KB
/
lqtutils_net.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <QFile>
#include <QBuffer>
#include "lqtutils_net.h"
//#define DEBUG_LOGS
namespace lqt {
DownloaderPriv::DownloaderPriv(const QUrl& url, QIODevice* io, QObject* parent) :
QObject(parent)
, m_manager(new QNetworkAccessManager(this))
, m_reply(nullptr)
, m_url(url)
, m_io(io) {}
DownloaderPriv::~DownloaderPriv()
{
#ifdef DEBUG_LOGS
qDebug() << Q_FUNC_INFO;
#endif
}
void DownloaderPriv::download()
{
set_state(LQTDownloaderState::S_DOWNLOADING);
m_reply = m_manager->get(QNetworkRequest(m_url));
connect(m_reply, &QNetworkReply::downloadProgress,
this, &DownloaderPriv::downloadProgress);
connect(m_reply, &QNetworkReply::readyRead, this, [this] {
if (m_state != LQTDownloaderState::S_DOWNLOADING)
return;
write(m_reply->readAll());
});
connect(m_reply, &QNetworkReply::finished, this, [this] {
if (m_state != LQTDownloaderState::S_DOWNLOADING)
return;
if (m_reply->error() != QNetworkReply::NoError) {
#ifdef DEBUG_LOGS
qWarning() << "Download error:" << m_reply->error() << m_reply->errorString();
#endif
set_state(LQTDownloaderState::S_NETWORK_FAILURE);
return;
}
m_io->close();
set_state(LQTDownloaderState::S_DONE);
});
}
void DownloaderPriv::abort()
{
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
set_state(LQTDownloaderState::S_ABORTED);
}
void DownloaderPriv::write(const QByteArray& data)
{
if (m_state != LQTDownloaderState::S_DOWNLOADING)
return;
if (!m_io->isOpen()) {
if (!m_io->open(QIODevice::WriteOnly)) {
set_state(LQTDownloaderState::S_IO_FAILURE);
return;
}
}
if (m_io->write(data) != data.size())
set_state(LQTDownloaderState::S_IO_FAILURE);
}
class LQTThread : public QThread
{
#ifdef DEBUG_LOGS
~LQTThread() {
qDebug() << Q_FUNC_INFO;
}
#endif
};
Downloader::Downloader(const QUrl& url, const QString& filePath, QObject* parent) :
Downloader(url, new QFile(filePath), parent)
{
connect(m_thread, &QThread::finished,
m_threadContext->ioDevice(), &QIODevice::deleteLater);
}
Downloader::Downloader(const QUrl &url, QByteArray* bucket, QObject *parent) :
Downloader(url, new QBuffer(bucket), parent)
{
connect(m_thread, &QThread::finished,
m_threadContext->ioDevice(), &QIODevice::deleteLater);
}
Downloader::Downloader(const QUrl& url, QIODevice* destIo, QObject* parent) :
QObject(parent)
, m_url(url)
, m_io(destIo)
, m_thread(new LQTThread)
, m_threadContext(new DownloaderPriv(url, destIo))
{
LQTDownloaderState::registerEnum("com.luke", 1, 0);
m_threadContext->moveToThread(m_thread);
m_thread->start();
connect(m_threadContext, &DownloaderPriv::stateChanged,
this, &Downloader::stateChanged);
connect(m_threadContext, &DownloaderPriv::downloadProgress,
this, &Downloader::downloadProgress);
connect(this, &Downloader::destroyed,
m_threadContext, &QObject::deleteLater);
connect(m_threadContext, &DownloaderPriv::destroyed,
m_thread, &QThread::quit);
connect(m_thread, &QThread::finished,
m_thread, &QThread::deleteLater);
}
Downloader::~Downloader()
{
#ifdef DEBUG_LOGS
qDebug() << Q_FUNC_INFO;
#endif
if (state() == LQTDownloaderState::S_DOWNLOADING)
abort();
}
void Downloader::download()
{
if (m_threadContext->state() != LQTDownloaderState::S_IDLE) {
qFatal("Do not re-use the downloader");
return;
}
QMetaObject::invokeMethod(m_threadContext, "download", Qt::BlockingQueuedConnection);
}
void Downloader::abort()
{
if (m_threadContext->state() == LQTDownloaderState::S_DOWNLOADING)
QMetaObject::invokeMethod(m_threadContext, "abort", Qt::BlockingQueuedConnection);
}
} // namespace