Browse Source

Merge pull request #10963 from glassez/peeraddress

Extract PeerAddress class into separate file
adaptive-webui-19844
Vladimir Golovnev 6 years ago committed by GitHub
parent
commit
431ab095e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/base/CMakeLists.txt
  2. 2
      src/base/base.pri
  3. 58
      src/base/bittorrent/peeraddress.cpp
  4. 44
      src/base/bittorrent/peeraddress.h
  5. 20
      src/base/bittorrent/peerinfo.cpp
  6. 10
      src/base/bittorrent/peerinfo.h
  7. 1
      src/base/bittorrent/torrenthandle.cpp
  8. 1
      src/gui/properties/peerlistwidget.cpp
  9. 29
      src/gui/properties/peersadditiondialog.cpp
  10. 2
      src/gui/properties/peersadditiondialog.h
  11. 1
      src/webui/api/synccontroller.cpp

2
src/base/CMakeLists.txt

@ -7,6 +7,7 @@ bittorrent/cachestatus.h @@ -7,6 +7,7 @@ bittorrent/cachestatus.h
bittorrent/downloadpriority.h
bittorrent/infohash.h
bittorrent/magneturi.h
bittorrent/peeraddress.h
bittorrent/peerinfo.h
bittorrent/private/bandwidthscheduler.h
bittorrent/private/filterparserthread.h
@ -81,6 +82,7 @@ unicodestrings.h @@ -81,6 +82,7 @@ unicodestrings.h
bittorrent/downloadpriority.cpp
bittorrent/infohash.cpp
bittorrent/magneturi.cpp
bittorrent/peeraddress.cpp
bittorrent/peerinfo.cpp
bittorrent/private/bandwidthscheduler.cpp
bittorrent/private/filterparserthread.cpp

2
src/base/base.pri

@ -6,6 +6,7 @@ HEADERS += \ @@ -6,6 +6,7 @@ HEADERS += \
$$PWD/bittorrent/downloadpriority.h \
$$PWD/bittorrent/infohash.h \
$$PWD/bittorrent/magneturi.h \
$$PWD/bittorrent/peeraddress.h \
$$PWD/bittorrent/peerinfo.h \
$$PWD/bittorrent/private/bandwidthscheduler.h \
$$PWD/bittorrent/private/filterparserthread.h \
@ -80,6 +81,7 @@ SOURCES += \ @@ -80,6 +81,7 @@ SOURCES += \
$$PWD/bittorrent/downloadpriority.cpp \
$$PWD/bittorrent/infohash.cpp \
$$PWD/bittorrent/magneturi.cpp \
$$PWD/bittorrent/peeraddress.cpp \
$$PWD/bittorrent/peerinfo.cpp \
$$PWD/bittorrent/private/bandwidthscheduler.cpp \
$$PWD/bittorrent/private/filterparserthread.cpp \

58
src/base/bittorrent/peeraddress.cpp

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Vladimir Golovnev <glassez@yandex.ru>
*
* 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.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "peeraddress.h"
#include <QString>
#include <QStringList>
BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(QString peerAddressStr)
{
PeerAddress addr;
QStringList ipPort;
if ((peerAddressStr[0] == '[') && (peerAddressStr.indexOf("]:") != -1)) // IPv6
ipPort = peerAddressStr.remove(QChar('[')).split("]:");
else if (peerAddressStr.indexOf(':') != -1) // IPv4
ipPort = peerAddressStr.split(':');
else
return addr;
QHostAddress ip(ipPort[0]);
if (ip.isNull())
return addr;
bool ok;
int port = ipPort[1].toInt(&ok);
if (!ok || (port < 1) || (port > 65535))
return addr;
addr.ip = ip;
addr.port = port;
return addr;
}

44
src/base/bittorrent/peeraddress.h

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Vladimir Golovnev <glassez@yandex.ru>
*
* 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.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#pragma once
#include <QHostAddress>
class QString;
namespace BitTorrent
{
struct PeerAddress
{
QHostAddress ip;
ushort port = 0;
static PeerAddress parse(QString peerAddressStr);
};
}

20
src/base/bittorrent/peerinfo.cpp

@ -34,24 +34,10 @@ @@ -34,24 +34,10 @@
#include "base/net/geoipmanager.h"
#include "base/unicodestrings.h"
#include "base/utils/string.h"
#include "peeraddress.h"
using namespace BitTorrent;
// PeerAddress
PeerAddress::PeerAddress()
: port(0)
{
}
PeerAddress::PeerAddress(const QHostAddress &ip, ushort port)
: ip(ip)
, port(port)
{
}
// PeerInfo
PeerInfo::PeerInfo(const TorrentHandle *torrent, const lt::peer_info &nativeInfo)
: m_nativeInfo(nativeInfo)
{
@ -183,8 +169,8 @@ bool PeerInfo::isPlaintextEncrypted() const @@ -183,8 +169,8 @@ bool PeerInfo::isPlaintextEncrypted() const
PeerAddress PeerInfo::address() const
{
return PeerAddress(QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string())),
m_nativeInfo.ip.port());
return {QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string()))
, m_nativeInfo.ip.port()};
}
QString PeerInfo::client() const

10
src/base/bittorrent/peerinfo.h

@ -39,15 +39,7 @@ class QBitArray; @@ -39,15 +39,7 @@ class QBitArray;
namespace BitTorrent
{
class TorrentHandle;
struct PeerAddress
{
QHostAddress ip;
ushort port;
PeerAddress();
PeerAddress(const QHostAddress &ip, ushort port);
};
struct PeerAddress;
class PeerInfo
{

1
src/base/bittorrent/torrenthandle.cpp

@ -65,6 +65,7 @@ @@ -65,6 +65,7 @@
#include "base/utils/fs.h"
#include "base/utils/string.h"
#include "downloadpriority.h"
#include "peeraddress.h"
#include "peerinfo.h"
#include "session.h"
#include "trackerentry.h"

1
src/gui/properties/peerlistwidget.cpp

@ -40,6 +40,7 @@ @@ -40,6 +40,7 @@
#include <QTableView>
#include <QWheelEvent>
#include "base/bittorrent/peeraddress.h"
#include "base/bittorrent/peerinfo.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h"

29
src/gui/properties/peersadditiondialog.cpp

@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
#include <QHostAddress>
#include <QMessageBox>
#include "base/bittorrent/peeraddress.h"
#include "base/global.h"
#include "ui_peersadditiondialog.h"
@ -63,7 +64,7 @@ void PeersAdditionDialog::validateInput() @@ -63,7 +64,7 @@ void PeersAdditionDialog::validateInput()
return;
}
for (const QString &peer : asConst(m_ui->textEditPeers->toPlainText().trimmed().split('\n'))) {
BitTorrent::PeerAddress addr = parsePeer(peer);
const BitTorrent::PeerAddress addr = BitTorrent::PeerAddress::parse(peer);
if (!addr.ip.isNull()) {
m_peersList.append(addr);
}
@ -77,29 +78,3 @@ void PeersAdditionDialog::validateInput() @@ -77,29 +78,3 @@ void PeersAdditionDialog::validateInput()
}
accept();
}
BitTorrent::PeerAddress PeersAdditionDialog::parsePeer(QString peer)
{
BitTorrent::PeerAddress addr;
QStringList ipPort;
if ((peer[0] == '[') && (peer.indexOf("]:") != -1)) // IPv6
ipPort = peer.remove(QChar('[')).split("]:");
else if (peer.indexOf(':') != -1) // IPv4
ipPort = peer.split(':');
else
return addr;
QHostAddress ip(ipPort[0]);
if (ip.isNull())
return addr;
bool ok;
int port = ipPort[1].toInt(&ok);
if (!ok || (port < 1) || (port > 65535))
return addr;
addr.ip = ip;
addr.port = port;
return addr;
}

2
src/gui/properties/peersadditiondialog.h

@ -53,8 +53,6 @@ protected slots: @@ -53,8 +53,6 @@ protected slots:
void validateInput();
private:
BitTorrent::PeerAddress parsePeer(QString peer);
Ui::PeersAdditionDialog *m_ui;
QList<BitTorrent::PeerAddress> m_peersList;
};

1
src/webui/api/synccontroller.cpp

@ -34,6 +34,7 @@ @@ -34,6 +34,7 @@
#include <QMetaObject>
#include <QThread>
#include "base/bittorrent/peeraddress.h"
#include "base/bittorrent/peerinfo.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h"

Loading…
Cancel
Save