This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSeamlessServer.cpp
237 lines (192 loc) · 8.2 KB
/
SeamlessServer.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
229
230
231
232
233
234
235
236
237
/*
* Copyright (C) 2019 Veli Tasalı, created on 2/9/19
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <src/database/object/NetworkDevice.h>
#include <src/database/object/TransferGroup.h>
#include <src/database/object/TransferObject.h>
#include <QtCore/QFile>
#include <src/util/AppUtils.h>
SeamlessServer::SeamlessServer(QObject *parent)
: CSServer(QHostAddress::Any, PORT_SEAMLESS, TIMEOUT_SOCKET_DEFAULT, parent)
{
}
void SeamlessServer::connected(CSActiveConnection *connection)
{
TransferTask *thisTask = nullptr;
try {
const auto &mainRequest = connection->receive();
const auto &mainRequestJSON = mainRequest.asJson();
const QString &deviceId = mainRequestJSON.contains(KEYWORD_TRANSFER_DEVICE_ID)
? mainRequestJSON.value(KEYWORD_TRANSFER_DEVICE_ID).toString()
: nullptr;
groupid groupId = mainRequestJSON.value(KEYWORD_TRANSFER_GROUP_ID).toVariant().toUInt();
thisTask = new TransferTask(groupId, deviceId, TransferObject::Type::Outgoing);
gTaskMgr->attachTask(thisTask);
{
QJsonObject reply;
reply.insert(KEYWORD_RESULT, false);
// device might be null because this was introduced in build 91
if (deviceId != nullptr && !gDbSignal->contains(NetworkDevice(deviceId))) {
reply.insert(KEYWORD_ERROR, KEYWORD_ERROR_NOT_ALLOWED);
} else if (!gDbSignal->contains(TransferGroup(groupId))) {
reply.insert(KEYWORD_ERROR, KEYWORD_ERROR_NOT_FOUND);
} else {
reply.insert(KEYWORD_RESULT, true);
}
connection->reply(reply);
if (!reply.value(KEYWORD_RESULT).toBool(false))
return;
}
emit taskStarted(groupId, deviceId);
while (connection->socket()->isOpen()) {
const auto &response = connection->receive();
if (response.response == nullptr || response.length <= 0)
return;
const QJsonObject &request = response.asJson();
QJsonObject reply;
qDebug() << this << request;
{
if (request.contains(KEYWORD_RESULT) && !request.value(KEYWORD_RESULT).toBool(false)) {
if (request.contains(KEYWORD_TRANSFER_JOB_DONE)
&& request.value(KEYWORD_TRANSFER_JOB_DONE).toBool(false)) {
qDebug() << this << "Receiver notified that the transfer is done";
emit taskDone(groupId, deviceId);
break;
} else
thisTask->interrupt();
} else if (!thisTask->interrupted()) {
qDebug() << this << "Entering sending phase";
TransferObject transferObject(request.value(KEYWORD_TRANSFER_REQUEST_ID).toVariant().toUInt(),
deviceId, TransferObject::Type::Outgoing);
if (gDbSignal->reconstruct(transferObject)) {
try {
quint16 serverPort = static_cast<quint16>(request.value(KEYWORD_TRANSFER_SOCKET_PORT)
.toVariant()
.toUInt());
size_t skippedBytes = request.contains(KEYWORD_SKIPPED_BYTES)
? request.value(KEYWORD_SKIPPED_BYTES).toVariant().toUInt()
: 0;
qDebug() << this << "File sending about to begin" << serverPort << skippedBytes
<< transferObject.file;
QFile file(transferObject.file);
if (file.exists() && file.open(QFile::OpenModeFlag::ReadOnly)) {
transferObject.accessPort = serverPort;
transferObject.skippedBytes = skippedBytes;
auto fileSize = static_cast<size_t>(file.size());
reply.insert(KEYWORD_RESULT, true);
if (transferObject.fileSize != fileSize) {
reply.insert(KEYWORD_SIZE_CHANGED, file.size());
transferObject.fileSize = fileSize;
}
connection->reply(reply);
if (skippedBytes > 0)
if (!file.seek(static_cast<qint64>(skippedBytes)))
qDebug() << this << "File seek failed for byte" << skippedBytes;
qDebug() << this << "About to connect";
if (transferObject.flag != TransferObject::Flag::Running) {
transferObject.flag = TransferObject::Flag::Running;
gDbSignal->update(transferObject);
}
QTcpSocket socket;
socket.open(QTcpSocket::OpenModeFlag::WriteOnly);
{
// Establish the connection to the socket that will send the file
socket.connectToHost(connection->socket()->peerAddress(), serverPort);
while (QAbstractSocket::SocketState::ConnectingState == socket.state())
socket.waitForConnected(TIMEOUT_SOCKET_DEFAULT_LARGE);
if (QAbstractSocket::SocketState::ConnectedState != socket.state()) {
qDebug() << this << "Did not connect";
throw exception();
}
socket.setSocketOption(QTcpSocket::SocketOption::LowDelayOption, 1);
}
char *buffer = new char[BUFFER_LENGTH_DEFAULT];
try {
auto lastUpdated = time(nullptr);
while (!file.atEnd()) {
time_t currentTime = time(nullptr);
if (socket.bytesToWrite() == 0) {
//socket.write(file.read(BUFFER_LENGTH_DEFAULT));
qint64 readSize = file.read(buffer, BUFFER_LENGTH_DEFAULT);
socket.write(buffer, readSize);
socket.flush();
} else if (thisTask->interrupted()
|| (socket.bytesToWrite() > 0
&& !socket.waitForBytesWritten(TIMEOUT_SOCKET_DEFAULT))) {
qDebug() << this << "Timed out or interrupted:" << thisTask->interrupted();
throw exception();
}
if (currentTime - lastUpdated > 1) {
// only emit current file size
// also always update when a file status changes
qDebug() << this << "Notified update";
emit gTaskMgr->taskStatus(thisTask->m_groupId, thisTask->m_deviceId,
thisTask->m_type,
file.pos(),
transferObject);
lastUpdated = currentTime;
}
}
qDebug() << this << "I/O Completed";
transferObject.flag = TransferObject::Flag::Done;
} catch (...) {
qDebug() << this << "I/O Error occurred";
transferObject.flag = TransferObject::Flag::Interrupted;
}
delete[] buffer;
socket.close();
file.close();
} else {
connection->reply({
{KEYWORD_RESULT, false},
{KEYWORD_ERROR, KEYWORD_ERROR_NOT_ACCESSIBLE},
{KEYWORD_FLAG_GROUP_EXISTS, true}
});
transferObject.flag = TransferObject::Flag::Interrupted;
qDebug() << this << "File does not exist or did not open";
}
} catch (...) {
qDebug() << this << "Some error occurred";
emit gTaskMgr->taskError(thisTask->m_groupId, thisTask->m_deviceId, thisTask->m_type,
TransferTaskManager::TaskError::DuringTask);
}
gDbSignal->update(transferObject);
} else {
qDebug() << this << "Object does not exist, but the group does";
connection->reply({
{KEYWORD_RESULT, false},
{KEYWORD_ERROR, KEYWORD_ERROR_NOT_FOUND},
{KEYWORD_FLAG_GROUP_EXISTS, true}
});
}
} else if (thisTask->interrupted()) {
qDebug() << this << "Interrupted";
connection->reply({
{KEYWORD_RESULT, false},
{KEYWORD_TRANSFER_JOB_DONE, false}
});
}
}
}
} catch (...) {
qDebug() << this << "Error occurred";
}
if (thisTask != nullptr) {
gTaskMgr->detachTask(thisTask);
delete thisTask;
}
}