spark-qt-research/Components/MultiplethreadDownload/downloadworker.h

72 lines
1.5 KiB
C
Raw Normal View History

2021-02-16 00:25:54 +08:00
#ifndef DOWNLOADWORKER_H
#define DOWNLOADWORKER_H
#include <QObject>
#include <QList>
#include <QFile>
#include <QNetworkReply>
class DownloadWorker : public QObject
{
Q_OBJECT
public:
explicit DownloadWorker(QObject *parent = nullptr);
void setIdentifier(int identifier);
2021-02-16 02:08:00 +08:00
void setParamter(const QString &url, QPair<qint64, qint64> range, QFile *flle);
2021-02-16 20:18:33 +08:00
qint64 getReceivedPos();
2021-02-16 00:25:54 +08:00
public slots:
void doWork();
2021-02-16 20:18:33 +08:00
void doStop();
2021-02-16 00:25:54 +08:00
void dataReady();
2021-02-16 02:08:00 +08:00
void slotFinish();
2021-02-16 20:18:33 +08:00
void handleProcess(qint64, qint64);
2021-02-16 00:25:54 +08:00
signals:
void resultReady(int identifier, QByteArray data);
void testSignals();
2021-02-16 20:18:33 +08:00
void workFinished();
void downloadProcess();
2021-02-16 00:25:54 +08:00
private:
int identifier;
QString url;
2021-02-16 02:08:00 +08:00
qint64 startPos;
qint64 endPos;
qint64 receivedPos = 0;
2021-02-16 00:25:54 +08:00
QNetworkReply *reply;
2021-02-16 20:18:33 +08:00
QNetworkAccessManager *mgr;
2021-02-16 02:08:00 +08:00
QFile *file;
2021-02-16 00:25:54 +08:00
};
class DownloadController : public QObject
{
Q_OBJECT
public:
explicit DownloadController(QObject *parent = nullptr);
void setFilename(QString filename);
void setThreadNum(int threadNum);
void startDownload(const QString &url, qint64 fileSize);
void stopDownload();
public slots:
2021-02-16 20:18:33 +08:00
void handleProcess();
void chunkDownloadFinish();
2021-02-16 00:25:54 +08:00
signals:
void errorOccur(const QString& msg);
2021-02-16 20:18:33 +08:00
void downloadProcess(qint64, qint64);
void downloadFinished();
2021-02-16 00:25:54 +08:00
private:
int threadNum;
QString filename;
2021-02-16 20:18:33 +08:00
qint64 fileSize;
2021-02-16 00:25:54 +08:00
QVector<QPair<qint64, qint64>> ranges;
2021-02-16 02:08:00 +08:00
QFile *file;
2021-02-16 20:18:33 +08:00
QList<DownloadWorker*> workers;
int finish = 0;
2021-02-16 00:25:54 +08:00
};
#endif // FILEDOWNLOADWORKER_H