#include"piecethread.h"
#include<QtNetwork>
#include <QtGui>
#include"mission.h"
PieceThread
::PieceThread(QObject *parent,
short thn, quint64 bp, quint64 ep,
QUrl u,
QFile *f
): QThread(parent
),threadNumber
(thn
),beginPos
(bp
),endPos
(ep
),url
(u
),
file(f
) {
qnam=NULL;
oldWorkingPos=workingPos=beginPos;
pauseFlag=false;
reply=NULL;
}
void PieceThread::run()
{
startTheThread();
}
QNetworkRequest PieceThread::setupTheRequest()
{
QNetworkRequest request(url);
request.setRawHeader("RANGE",range);
return request;
}
void PieceThread::startTheThread()
{
QNetworkRequest request=setupTheRequest();
qnam=new QNetworkAccessManager;
reply=qnam->get(request);
connect(reply,SIGNAL(readyRead()),this,SLOT(slotReadReady()));
connect(reply,SIGNAL(finished()),this,SLOT(slotFinished()));
this->exec();
}
void PieceThread::slotReadReady()
{
while(reply!=NULL&&!pauseFlag&&!reply->atEnd())
{
file->seek(workingPos);
workingPos+=file->write(reply->read(4096));
emit this->signalProgressIncrease(workingPos-oldWorkingPos);
oldWorkingPos=workingPos;
}
}
void PieceThread::slotFinished()
{
disconnect(reply,SIGNAL(readyRead()),this,SLOT(slotReadReady()));
disconnect(reply,SIGNAL(finished()),this,SLOT(slotFinished()));
reply->deleteLater();
reply=NULL;
qnam->deleteLater();
qnam=NULL;
if(pauseFlag==false)
{
qDebug
()<<
"piece "+QString::number(threadNumber
)+" download complete";
emit this->signalPieceFinished();
}
else
{
qDebug()<<"piece download abort(pause)";
emit this->signalPieceAbort();
}
}
void PieceThread::stopTheThread()
{
pauseFlag=true;
reply->abort();
qDebug()<<"piece download pause";
}
#include"piecethread.h"
#include<QtNetwork>
#include <QtGui>
#include"mission.h"
PieceThread::PieceThread(QObject *parent, short thn, quint64 bp, quint64 ep, QUrl u, QFile *f):
QThread(parent),threadNumber(thn),beginPos(bp),endPos(ep),url(u),file(f)
{
qnam=NULL;
oldWorkingPos=workingPos=beginPos;
pauseFlag=false;
reply=NULL;
}
void PieceThread::run()
{
startTheThread();
}
QNetworkRequest PieceThread::setupTheRequest()
{
QNetworkRequest request(url);
QByteArray range=QByteArray("bytes=")+
QByteArray::number(beginPos)+
QByteArray("-")+
QByteArray::number(endPos);
request.setRawHeader("RANGE",range);
return request;
}
void PieceThread::startTheThread()
{
QNetworkRequest request=setupTheRequest();
qnam=new QNetworkAccessManager;
reply=qnam->get(request);
connect(reply,SIGNAL(readyRead()),this,SLOT(slotReadReady()));
connect(reply,SIGNAL(finished()),this,SLOT(slotFinished()));
this->exec();
}
void PieceThread::slotReadReady()
{
while(reply!=NULL&&!pauseFlag&&!reply->atEnd())
{
QMutexLocker locker(&mutex);
file->seek(workingPos);
workingPos+=file->write(reply->read(4096));
emit this->signalProgressIncrease(workingPos-oldWorkingPos);
oldWorkingPos=workingPos;
}
}
void PieceThread::slotFinished()
{
disconnect(reply,SIGNAL(readyRead()),this,SLOT(slotReadReady()));
disconnect(reply,SIGNAL(finished()),this,SLOT(slotFinished()));
reply->deleteLater();
reply=NULL;
qnam->deleteLater();
qnam=NULL;
if(pauseFlag==false)
{
qDebug()<<"piece "+QString::number(threadNumber)+" download complete";
emit this->signalPieceFinished();
}
else
{
qDebug()<<"piece download abort(pause)";
emit this->signalPieceAbort();
}
}
void PieceThread::stopTheThread()
{
pauseFlag=true;
reply->abort();
qDebug()<<"piece download pause";
}
To copy to clipboard, switch view to plain text mode
Bookmarks