-
Notifications
You must be signed in to change notification settings - Fork 0
/
connservice.cpp
162 lines (156 loc) · 4.38 KB
/
connservice.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "connservice.h"
//void connService::disConnectService()
//{
// if(socket)
// {
// socket->disconnectFromHost();
// socket->close();
// socket->abort();
// }
//}
connService::connService(QObject *parent):
QObject(parent)
{
tport = 6666;
hostAddr = QHostAddress::LocalHost;
sizeTotal = 0;
sizeGet = 0;
fileNameSize = 0;
fileName.resize(0);
file = NULL;
isFile = false;
buf.resize(0);
iTimer = NULL;
socket = new QTcpSocket;
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
socket->abort();
socket->connectToHost(("127.0.0.1"), 6666);
//connect(socket, SIGNAL(disconnected()), this, SLOT(slotDisConnection()));
connect(socket, SIGNAL(readyRead()), this, SLOT(dataRead()));
}
connService::~connService()
{
if(socket != NULL)
{
socket->disconnectFromHost();
socket->close();
socket->abort();
delete socket;
}
}
void connService::dataWrite(const QString &str)
{
socket->disconnectFromHost();
socket->close();
socket->abort();
socket->connectToHost(("127.0.0.1"), 6666);
//connect(socket, SIGNAL(disconnected()), this, SLOT(slotDisConnection()));
connect(socket, SIGNAL(readyRead()), this, SLOT(dataRead()));
socket->write(str.toUtf8(),str.length() + 1);
qDebug() << "has write:" << str;
}
void connService::dataRead()
{
if(!isFile)
{
if(socket->bytesAvailable() > 0)
{
buf.resize(0);
buf = socket->readAll();
}
QStringList list = (static_cast<QString>(buf)).split('#');
if(list.at(0) == "file")
{
isFile = true;
sizeTotal = (list.at(1)).toInt();
fileName = list.at(2);
fileName ="C:\\pdfsys\\localFiles\\" + fileName;
file = new QFile(fileName);
if(file->exists())
file->remove();
if(!file->open(QFile::WriteOnly))
{
qDebug() << "无法写文件";
buf.resize(0);
return;
}
//设置定时器,当超过8s文件还未传输,成功者重新请求文件传输
iTimer = new QTimer(this);
iTimer->setInterval(5000); //5s内如果没有完全接受到文件,则按照接受失败处理
iTimer->setSingleShot(true);
iTimer->start();
connect(iTimer, SIGNAL(timeout()), this, SLOT(onTimerOut()));
//connect(iTimer, SIGNAL(timeout()), this, SLOT(deleteLater()));
} else
{
return;
}
} else {
//接受的数据小于总数据,则写入文件
if(sizeGet < sizeTotal)
{
buf.resize(0);
sizeGet += (socket->bytesAvailable());
buf = socket->readAll();
file->write(buf);
buf.resize(0);
}
//数据接受完毕
if(sizeGet >= sizeTotal)
{
//接受文件成功,关闭定时器
iTimer->stop();
delete iTimer;
iTimer = NULL;
isFile = false;
file->close();
file = NULL;
sizeGet = 0;
sizeTotal = 0;
fileNameSize = 0;
qDebug() << QString("接受文件 %1完毕.");
emit onAcceptFile(fileName);
fileName.resize(0);
buf.resize(0);
socket->disconnectFromHost();
socket->close();
socket->abort();
}
}
}
QByteArray connService::getBuf()
{
//阻塞等待socket获取到服务器的数据
socket->waitForReadyRead();
return buf;
}
bool connService::isConnected()
{
return (socket->state() == QTcpSocket::ConnectedState);
}
void connService::onTimerOut()
{
//接受文件失败,关闭定时器
iTimer->stop();
delete iTimer;
iTimer = NULL;
//重置变量,复位传输失败的部分文件的遗留信息
socket->abort();
isFile = false;
file->close();
file->remove();
file = NULL;
sizeGet = 0;
sizeTotal = 0;
fileNameSize = 0;
QString bookTitle = fileName;
bookTitle = bookTitle.right(bookTitle.size() - bookTitle.lastIndexOf('\\') - 1);
fileName.resize(0);
buf.resize(0);
//重新发送文件传输请求
dataWrite(tr("read #%1#%2").arg(bookTitle).arg(pwd));
}
void connService::setPWD(QString p)
{
pwd = p;
}