-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsethread.cpp
228 lines (199 loc) · 7.23 KB
/
fsethread.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "fsethread.h"
#include <QSqlDatabase>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDataStream>
#include <QMutexLocker>
#include <QVariant>
#include <QList>
#include <QDebug>
#include "sqlhelper.h"
#include "fseenum.h"
FSEThread::FSEThread(qintptr SocketDescriptor, QSqlDatabase *database,
QObject *parent )
: QThread(parent), socketDescriptor(SocketDescriptor), db(database)
{
}
void FSEThread::run()
{
QTcpSocket tcpSocket;
if(!tcpSocket.setSocketDescriptor(socketDescriptor))
{
emit error(tcpSocket.error());
emit errorString(tcpSocket.errorString());
}
emit debugString(tr("A new connection coming: ")
+ QString("%1").arg(tcpSocket.peerAddress().toString()));
const int timeout = 5 * 1000; // 5 sec
/*
* get length of message content from tcpSocket,
* the length has been stored in type 'quint16'(2 bytes)
*/
while(tcpSocket.bytesAvailable() < (int)sizeof(quint16))
if(!tcpSocket.waitForReadyRead(timeout)) {
emit error(tcpSocket.error());
emit errorString(tcpSocket.errorString());
return;
}
quint16 blkSize; // size of message content
QDataStream in(&tcpSocket);
in.setVersion(QDataStream::Qt_4_8);
in >> blkSize;
// get message content, its type is QList<QVariant>
while(tcpSocket.bytesAvailable() < (int)blkSize)
if(!tcpSocket.waitForReadyRead(timeout)) {
emit error(tcpSocket.error());
emit errorString(tcpSocket.errorString());
return;
}
QList<QVariant> message;
in >> message;
if(message.size() < 3) {
emit errorString(tr("Invalid connection from ")
+ tcpSocket.peerAddress().toString());
tcpSocket.disconnectFromHost();
return;
}
QString username = message.takeFirst().toString();
QString password = message.takeFirst().toString();
int uid = checkUser(username, password, db);
if( uid >= 0) {
int command = message.takeFirst().toInt();
switch (command) {
case LoginCMD:
emit debugString("[" + username + "]["
+ tcpSocket.peerAddress().toString()
+ "]" + tr("login successfully"));
processLogin(&tcpSocket, LoginSuccessState);
break;
case PushFilePasswordCMD:
emit debugString("[" + username + "]["
+ tcpSocket.peerAddress().toString()
+ "] " + tr("try to push file to server"));
processPushFilePWD(&tcpSocket, uid, message.takeFirst().toStringList());
break;
case PullFilePasswordCMD:
emit debugString("[" + username + "]["
+ tcpSocket.peerAddress().toString()
+ "] " + tr("try to pull file key from server"));
processPullFilePWD(&tcpSocket, uid, message.takeFirst().toStringList());
break;
case PullUserInfoCMD:
emit debugString("[" + username + "]["
+ tcpSocket.peerAddress().toString()
+ "] " + tr("try to pull user information"));
processPullUserInfo(&tcpSocket, uid);
break;
case PushUserInfoCMD:
emit debugString("[" + username + "]["
+ tcpSocket.peerAddress().toString()
+ "] " + tr("try to update user information"));
processPushUserInfo(&tcpSocket, uid, message.takeFirst().toStringList());
break;
default:
break;
}
}
else {
processLogin(&tcpSocket, LoginFailedState);
emit debugString("[" + username + "]["
+ tcpSocket.peerAddress().toString()
+ "]" + tr(" try to login, but failed"));
}
tcpSocket.disconnectFromHost();
}
void FSEThread::sendMessage(QTcpSocket *socket, const QList<QVariant> &block)
{
QByteArray bytes;
QDataStream out(&bytes, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_8);
out << (quint16)0 << block;
out.device()->seek(0);
out << (quint16) (bytes.size() - sizeof(quint16));
socket->write(bytes);
int timeout = 5 * 1000;
if(!socket->waitForBytesWritten(timeout)) {
emit error(socket->error());
emit errorString(socket->errorString());
}
}
void FSEThread::processLogin(QTcpSocket *socket, FSEState state)
{
QList<QVariant> block;
block << QVariant(state);
block << QVariant( state == LoginSuccessState ?
tr("Login Successfully") : tr("Wrong, please try again"));
sendMessage(socket, block);
}
void FSEThread::processPushFilePWD(QTcpSocket *socket, int uid, QStringList argument)
{
bool error = false;
QString description;
if(argument.size() < 3) {
emit debugString(tr("argument too few for adding file to database from ")
+ socket->peerAddress().toString());
error = true;
}
else {
QString filename = argument.takeFirst();
QString filemd5 = argument.takeFirst();
QString filepwd = argument.takeFirst();
int fid = addFile(filename, filemd5, filepwd, uid, db);
description = (fid == -1) ? tr("the file is already pushed") : tr("push successfully");
emit debugString(tr("add file to database: ") + filename);
}
if(error)
description = tr("argument too few");
QList<QVariant> block;
block << QVariant( error ? PushFilePWDFailedState : PushFilePWDSuccessState)
<< QVariant(description);
sendMessage(socket, block);
}
void FSEThread::processPullFilePWD(QTcpSocket *socket, int uid, QStringList argument)
{
bool error = false;
QString pwd;
if(argument.size() < 1)
error = true;
else {
QString filemd5 = argument.takeFirst();
pwd = getFilePWD(filemd5, uid, db);
if(pwd.isEmpty())
error = true;
}
QList<QVariant> block;
block << QVariant( error ? PullFilePWDFailedState : PullFilePWDSuccessState )
<< QVariant( error ? tr("File not exist") : pwd );
sendMessage(socket, block);
}
void FSEThread::processPullUserInfo(QTcpSocket *socket, int uid)
{
FSEState st = PullUserInfoSuccessState;
QStringList info = getUserInfo(uid, db);
if(info.size() == 0) {
st = PullUserInfoFailState;
}
QList<QVariant> block;
block << QVariant(st) << QVariant(info);
sendMessage(socket, block);
}
void FSEThread::processPushUserInfo(QTcpSocket *socket, int uid, QStringList argument)
{
QString description;
FSEState st = PushUserInfoSuccessState;
if(argument.size() < 5) {
st = PushUserInfoFailState;
description = tr("information is too few, update failed");
}
else {
bool isOK = modifyUserInfo(uid, argument, db);
if(!isOK) {
st = PushUserInfoFailState;
description = tr("update failed, database forbid");
}else description = tr("user information update successfully");
}
debugString(description);
QList<QVariant> block;
block << QVariant(st) << QVariant(description);
sendMessage(socket, block);
}