-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTcpConnection.cpp
131 lines (113 loc) · 3.39 KB
/
FTcpConnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "FTcpConnection.h"
FTcpConnection::FTcpConnection(QObject *parent) :
QObject(parent)
{
totalBytes = 0;
bytesReceived = 0;
fileNameSize = 0;
}
void FTcpConnection::updateServerProgress() //更新进度条,接收数据
{
QDataStream in(connection);
in.setVersion(QDataStream::Qt_4_6);
if(bytesReceived <= sizeof(qint64)*2)//Header
{
if((connection->bytesAvailable() >= sizeof(qint64)*2)&& (fileNameSize == 0))
{
in >> totalBytes >> fileNameSize;
bytesReceived += sizeof(qint64) * 2;
}
if((connection->bytesAvailable() >= fileNameSize)&& (fileNameSize != 0))
{
in >> fileName;
bytesReceived += fileNameSize;
if(userID.isEmpty())
{
localFile = new QFile(fileName);
}
else
{
qDebug()<<"mkdir"<<userID;
QDir folder;
folder.mkdir(userID);
localFile = new QFile(userID+"/"+fileName);
}
if(!localFile->open(QFile::WriteOnly))
{
qDebug() << "open file error!";
return;
}
}
else return;
}//Header End
if(bytesReceived < totalBytes)
{
bytesReceived += connection->bytesAvailable();
inBlock = connection->readAll();
localFile->write(inBlock);
inBlock.resize(0);
}
if(bytesReceived == totalBytes)
{
localFile->close();
delete localFile;
totalBytes = 0;
bytesReceived = 0;
fileNameSize = 0;
if(fileName=="userDiscern")
{
localFile = new QFile(fileName);
localFile->open(QIODevice::ReadWrite);
userID=localFile->readLine(64);
userID=userID.simplified();
localFile->remove();
localFile->close();
delete localFile;
qDebug()<<userID;
}
else if(fileName=="sync")
{
localFile = new QFile(userID+"/"+"fileListInfo");
if(!localFile->open(QIODevice::ReadOnly))
{
}
}
}
}
void FTcpConnection::displayError(QAbstractSocket::SocketError) //错误处理
{
}
void FTcpConnection::StartUpload(QString toSend) //实现文件大小等信息的发送
{
SendlocalFile = new QFile(toSend);
if(!localFile->open(QFile::ReadOnly))
{
qDebug() << "open file error!";
return;
}
totalBytes = SendlocalFile->size();
QDataStream sendOut(&outBlock,QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_4_6);
QString currentFileName = toSend.right(toSend.size() - toSend.lastIndexOf('/')-1);
sendOut << qint64(0) << qint64(0) << currentFileName;
totalBytes += outBlock.size();
sendOut.device()->seek(0);
sendOut<<totalBytes<<qint64((outBlock.size() - sizeof(qint64)*2));
bytesWritten+=connection->write(outBlock);
//qDebug()<<totalBytes;
while(bytesWritten<totalBytes)
{
//qDebug()<<bytesWritten;
outBlock.clear();
outBlock = SendlocalFile->read(loadSize);
connection->write(outBlock);
bytesWritten += outBlock.size();
}
if(bytesWritten == totalBytes) //发送完毕
{
qDebug()<<"Send file success";
SendlocalFile->close();
delete SendlocalFile;
}
outBlock.resize(0);
}