2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "connectionmanager.h"
|
|
|
|
#include "peerwireclient.h"
|
|
|
|
#include "ratecontroller.h"
|
|
|
|
#include "torrentclient.h"
|
|
|
|
#include "torrentserver.h"
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC(TorrentServer, torrentServer)
|
|
|
|
|
|
|
|
TorrentServer *TorrentServer::instance()
|
|
|
|
{
|
|
|
|
return torrentServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentServer::addClient(TorrentClient *client)
|
|
|
|
{
|
|
|
|
clients << client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentServer::removeClient(TorrentClient *client)
|
|
|
|
{
|
|
|
|
clients.removeAll(client);
|
|
|
|
}
|
|
|
|
|
2012-01-11 15:06:14 +00:00
|
|
|
void TorrentServer::incomingConnection(qintptr socketDescriptor)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
PeerWireClient *client =
|
2014-01-13 14:48:44 +00:00
|
|
|
new PeerWireClient(ConnectionManager::instance()->clientId(), this);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (client->setSocketDescriptor(socketDescriptor)) {
|
|
|
|
if (ConnectionManager::instance()->canAddConnection() && !clients.isEmpty()) {
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(client, &PeerWireClient::infoHashReceived,
|
|
|
|
this, &TorrentServer::processInfoHash);
|
2020-02-07 14:29:33 +00:00
|
|
|
connect(client, &PeerWireClient::errorOccurred,
|
2019-11-01 12:21:32 +00:00
|
|
|
this, QOverload<>::of(&TorrentServer::removeClient));
|
2011-04-27 10:05:43 +00:00
|
|
|
RateController::instance()->addSocket(client);
|
|
|
|
ConnectionManager::instance()->addConnection(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client->abort();
|
|
|
|
delete client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentServer::removeClient()
|
|
|
|
{
|
|
|
|
PeerWireClient *peer = qobject_cast<PeerWireClient *>(sender());
|
|
|
|
RateController::instance()->removeSocket(peer);
|
|
|
|
ConnectionManager::instance()->removeConnection(peer);
|
|
|
|
peer->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentServer::processInfoHash(const QByteArray &infoHash)
|
|
|
|
{
|
|
|
|
PeerWireClient *peer = qobject_cast<PeerWireClient *>(sender());
|
2022-10-06 09:08:21 +00:00
|
|
|
for (TorrentClient *client : std::as_const(clients)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
if (client->state() >= TorrentClient::Searching && client->infoHash() == infoHash) {
|
2019-11-01 12:21:32 +00:00
|
|
|
peer->disconnect(peer, nullptr, this, nullptr);
|
2011-04-27 10:05:43 +00:00
|
|
|
client->setupIncomingConnection(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removeClient();
|
|
|
|
}
|