spark-qt-research/MultiplethreadDownload/downloadworker.h

71 lines
1.6 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>
#include <QMutex>
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 00:25:54 +08:00
public slots:
void doWork();
void dataReady();
2021-02-16 02:08:00 +08:00
void slotFinish();
2021-02-16 00:25:54 +08:00
signals:
void resultReady(int identifier, QByteArray data);
void testSignals();
private:
int identifier;
QString url;
2021-02-16 02:08:00 +08:00
// QPair<qint64, qint64> range;
qint64 startPos;
qint64 endPos;
qint64 receivedPos = 0;
2021-02-16 00:25:54 +08:00
QNetworkReply *reply;
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);
~DownloadController();
void setFilename(QString filename);
void setThreadNum(int threadNum);
void startDownload(const QString &url, qint64 fileSize);
void paruseDownload();
void stopDownload();
public slots:
void handleResults(int identifier, QByteArray data);
// void handleTest();
signals:
void operate(const QString &url, const QPair<qint64, qint64> &downloadRange);
void errorOccur(const QString& msg);
void receivedProcess(qint64 number);
private:
int threadNum;
QString filename;
QVector<QPair<qint64, qint64>> ranges;
2021-02-16 02:08:00 +08:00
QFile *file;
2021-02-16 00:25:54 +08:00
qint64 bytesReceived;
QVector<qint64> writePosList;
QMutex mutex;
};
#endif // FILEDOWNLOADWORKER_H