spark-qt-research/MultiplethreadDownload/downloadworker.h

66 lines
1.4 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);
void setParamter(const QString &url, QPair<qint64, qint64> range);
public slots:
void doWork();
void dataReady();
signals:
void resultReady(int identifier, QByteArray data);
void testSignals();
private:
int identifier;
QString url;
QPair<qint64, qint64> range;
QNetworkReply *reply;
};
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;
QFile file;
qint64 bytesReceived;
QVector<qint64> writePosList;
QMutex mutex;
};
#endif // FILEDOWNLOADWORKER_H