1 Star 0 Fork 16

黄新宇/qipmsg

forked from caowai/qipmsg 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
filetransworker.cpp 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
caowai 提交于 2019-01-09 13:36 . init version
#include "filetransworker.h"
#include <QFile>
#include <QThread>
#include <QHostAddress>
#include <QFileInfo>
FileTransWorker::FileTransWorker(QObject *parent) : QObject(parent)
{
}
void FileTransWorker::fileTransStart()
{
QByteArray data;
qint64 ret = 0;
int wr_len = 0;
QFile file;
qint64 fileSend = 0;
qint64 fileSize = 0;
QTcpSocket *mSock = new QTcpSocket();
mSock->setSocketDescriptor(mHandle);
qDebug()<<"will trans File"<<mFilename<<"Sock"<<mHandle<<mSock->peerAddress().toString()<<mSock->peerPort();
if(nullptr != mSock
&& mSock->isValid()
&& false == mFileTranStopFlag
&& true == QFileInfo(mFilename).isFile()
)
{
//qDebug()<<"File"<<mFilename<<"Sock"<<mSock->peerAddress().toString()<<mSock->peerPort();
file.setFileName(mFilename);
fileSize = file.size();
if(file.open(QFile::ReadOnly))
{
while(false == mFileTranStopFlag
&& false == file.atEnd()
&& mSock->isValid())
{
data.clear();
data.resize(0);
data = file.read(8192);
if(data.length()>0)
{
wr_len = 0;
while(wr_len!=data.length())
{
ret = mSock->write(data.right(data.length()-wr_len),data.length()-wr_len);
if( -1 != ret)
{
wr_len+=ret;
fileSend+=ret;
if((fileSend*100/fileSize)!=mProgress)
{
mProgress = (fileSend*100/fileSize);
emit fileTransProgress(mFileId,mProgress);
}
mSock->waitForBytesWritten();
continue;
}
else
{
break;
}
}
if( -1 == ret)
{
emit fileTransError(mFileId,mProgress);
break;
}
}
else
{
emit fileTransError(mFileId,mProgress);
break;
}
}
file.close();
emit fileTransFinished(mFileId);
}
}
else
{
emit fileTransError(mFileId,mProgress);
}
if(mSock->state() == QTcpSocket::ConnectedState)
{
mSock->disconnectFromHost();
}
if(mSock->state() != QTcpSocket::UnconnectedState)
{
mSock->waitForDisconnected();
}
mSock->close();
delete mSock;
return;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/flydilong/qipmsg.git
[email protected]:flydilong/qipmsg.git
flydilong
qipmsg
qipmsg
master

搜索帮助