forked from maz-1/dukto-qt5
-
Notifications
You must be signed in to change notification settings - Fork 8
/
duktoprotocol.cpp
229 lines (200 loc) · 6.82 KB
/
duktoprotocol.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
/* DUKTO - A simple, fast and multi-platform file transfer tool for LAN users
* Copyright (C) 2011 Emanuele Colombo
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "duktoprotocol.h"
#if defined(Q_OS_WIN)
#include <windows.h>
#endif
#include <QStringList>
#include <QFileInfo>
#include <QDir>
#include <QNetworkInterface>
#include <QNetworkProxy>
#include <QTimer>
#include "network/messenger.h"
#include "network/receiver.h"
#include "network/sender.h"
#define DEFAULT_UDP_PORT 4644
#define DEFAULT_TCP_PORT 4644
enum MSG_TYPE {
MSG_HELLO_BROADCAST = 0x01,
MSG_HELLO_UNICAST = 0x02,
MSG_GOODBYE = 0x03,
MSG_HELLO_PORT_BROADCAST = 0x04,
MSG_HELLO_PORT_UNICAST = 0x05
};
DuktoProtocol::DuktoProtocol(QObject *parent)
: QObject(parent), mLocalTcpPort(DEFAULT_TCP_PORT)
{
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::NoProxy));
}
DuktoProtocol::~DuktoProtocol()
{
closeServers();
delete mMessenger;
delete mTcpServer;
}
bool DuktoProtocol::setupUdpServer(quint16 port, QString &error)
{
if (mMessenger == nullptr) {
mMessenger = new Messenger(DEFAULT_UDP_PORT, this);
connect(mMessenger, &Messenger::buddyFound, this, &DuktoProtocol::peerListAdded, Qt::QueuedConnection);
connect(mMessenger, &Messenger::buddyGone, this, &DuktoProtocol::peerListRemoved, Qt::QueuedConnection);
}
if (mMessenger->start(port, error) == false) {
return false;
}
return true;
}
bool DuktoProtocol::setupTcpServer(quint16 port, QString &error)
{
if (mTcpServer == nullptr) {
mTcpServer = new QTcpServer(this);
}
if (mLocalTcpPort != port && mTcpServer->isListening()) {
mLocalTcpPort = port;
mTcpServer->close();
}
if (mTcpServer->isListening() == false && mTcpServer->listen(QHostAddress::AnyIPv4, mLocalTcpPort) == false) {
switch (mTcpServer->serverError()) {
case QAbstractSocket::AddressInUseError:
error = QStringLiteral("The TCP port %1 has been occupied by another application. Please quit that application and try again.").arg(QString::number(port));
break;
default:
error = QStringLiteral("Can not use TCP port %1. %2").arg(QString::number(port), mTcpServer->errorString());
}
return false;
}
connect(mTcpServer, &QTcpServer::newConnection, this, &DuktoProtocol::newIncomingConnection, Qt::UniqueConnection);
return true;
}
void DuktoProtocol::closeServers() {
if (mMessenger != nullptr) {
mMessenger->stop();
}
if (mTcpServer != nullptr) {
mTcpServer->close();
}
}
void DuktoProtocol::greeting() {
if (mMessenger != nullptr) {
mMessenger->sayHello();
}
}
// Richiesta connessione TCP in ingresso
void DuktoProtocol::newIncomingConnection()
{
// Recieve connection
QTcpSocket* s = mTcpServer->nextPendingConnection();
if(s == nullptr) return;
if (mReceiver != nullptr || mSender != nullptr) {
s->close();
return;
}
mReceiver = new Receiver(s, mDestDir, this);
connect(mReceiver, &Receiver::progress, this, &DuktoProtocol::transferStatusUpdate);
connect(mReceiver, &Receiver::dirReceived, this, &DuktoProtocol::receiveDirCompleted);
connect(mReceiver, &Receiver::fileReceived, this, &DuktoProtocol::receiveFileCompleted);
connect(mReceiver, &Receiver::textReceived, this, &DuktoProtocol::receiveTextCompleted);
connect(mReceiver, &Receiver::aborted, this, [this](const QString &error) {
mReceiver->deleteLater();
mReceiver = nullptr;
emit receiveAborted(error);
});
connect(mReceiver, &Receiver::completed, this, [this]() {
mReceiver->deleteLater();
mReceiver = nullptr;
emit receiveCompleted();
});
// Update GUI
emit receiveStarted(s->peerAddress().toString());
}
void DuktoProtocol::createSender(const QString &ipDest, qint16 port) {
mSender = new Sender(ipDest, port);
connect(mSender, &Sender::progress, this, &DuktoProtocol::transferStatusUpdate);
connect(mSender, &Sender::completed, this, [this]() {
mSender->deleteLater();
mSender = nullptr;
emit sendFileComplete();
});
connect(mSender, &Sender::aborted, this, [this](const QString &error) {
mSender->deleteLater();
mSender = nullptr;
emit sendFileError(error);
});
}
void DuktoProtocol::sendFile(const QString &ipDest, qint16 port, const QStringList &files)
{
// Check for default port
if (port == 0) port = DEFAULT_TCP_PORT;
// Verifica altre attività in corso
if (mReceiver != nullptr || mSender != nullptr) {
return;
}
createSender(ipDest, port);
mSender->sendFiles(files);
}
void DuktoProtocol::sendText(const QString &ipDest, qint16 port, const QString &text)
{
// Check for default port
if (port == 0) port = DEFAULT_TCP_PORT;
// Verifica altre attività in corso
if (mReceiver != nullptr || mSender != nullptr) return;
createSender(ipDest, port);
mSender->sendText(text);
}
void DuktoProtocol::sendScreen(const QString &ipDest, qint16 port, const QString &path)
{
// Check for default port
if (port == 0) port = DEFAULT_TCP_PORT;
// Verifica altre attività in corso
if (mReceiver != nullptr || mSender != nullptr) return;
createSender(ipDest, port);
mSender->sendFile(path, "Screenshot.jpg");
}
// Interrompe un trasferimento in corso (utilizzabile solo lato invio)
void DuktoProtocol::abortCurrentTransfer()
{
// Abort current connection
if (mSender != nullptr) {
mSender->abort();
emit sendFileAborted();
mSender = nullptr;
} else if (mReceiver != nullptr) {
mReceiver->abort();
emit receiveAborted(QString());
mReceiver = nullptr;
}
}
// Aggiorna il buddy name dell'utente locale
void DuktoProtocol::updateBuddy()
{
if (mMessenger != nullptr) {
mMessenger->sayGoodbye();
mMessenger->sayHello();
}
}
void DuktoProtocol::setDestDir(const QString &dir) {
#ifdef Q_OS_ANDROID
mDestDir = dir;
#else
mDestDir = QDir::cleanPath(dir);
if (!mDestDir.endsWith(QChar('/'))) {
mDestDir.append(QChar('/'));
}
#endif
}