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 "client.h"
|
|
|
|
#include "connection.h"
|
|
|
|
#include "peermanager.h"
|
|
|
|
|
2023-05-16 10:58:47 +00:00
|
|
|
#include <QHostInfo>
|
|
|
|
|
2023-05-23 10:31:40 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Client::Client()
|
2023-05-16 11:17:18 +00:00
|
|
|
: peerManager(new PeerManager(this))
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
peerManager->setServerPort(server.serverPort());
|
|
|
|
peerManager->startBroadcasting();
|
|
|
|
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(peerManager, &PeerManager::newConnection,
|
|
|
|
this, &Client::newConnection);
|
|
|
|
connect(&server, &Server::newConnection,
|
|
|
|
this, &Client::newConnection);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::sendMessage(const QString &message)
|
|
|
|
{
|
|
|
|
if (message.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2022-10-06 09:08:21 +00:00
|
|
|
for (Connection *connection : std::as_const(peers))
|
2011-04-27 10:05:43 +00:00
|
|
|
connection->sendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Client::nickName() const
|
|
|
|
{
|
2018-01-18 01:12:27 +00:00
|
|
|
return peerManager->userName() + '@' + QHostInfo::localHostName()
|
2011-04-27 10:05:43 +00:00
|
|
|
+ ':' + QString::number(server.serverPort());
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
bool Client::hasConnection(const QByteArray &peerUniqueId) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2023-06-14 16:48:19 +00:00
|
|
|
return peers.contains(peerUniqueId);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::newConnection(Connection *connection)
|
|
|
|
{
|
2023-06-14 16:48:19 +00:00
|
|
|
connection->setGreetingMessage(peerManager->userName(), peerManager->uniqueId());
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(connection, &Connection::readyForUse, this, &Client::readyForUse);
|
2023-06-15 08:18:11 +00:00
|
|
|
connect(connection, &Connection::errorOccurred, connection, &QObject::deleteLater);
|
|
|
|
connect(connection, &Connection::disconnected, connection, &QObject::deleteLater);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::readyForUse()
|
|
|
|
{
|
|
|
|
Connection *connection = qobject_cast<Connection *>(sender());
|
2023-06-14 16:48:19 +00:00
|
|
|
if (!connection || hasConnection(connection->uniqueId())) {
|
|
|
|
if (connection) {
|
|
|
|
connection->disconnectFromHost();
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
2023-06-14 16:48:19 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-06-15 08:18:11 +00:00
|
|
|
connect(connection, &Connection::errorOccurred, this, &Client::connectionError);
|
|
|
|
connect(connection, &Connection::disconnected, this, &Client::disconnected);
|
|
|
|
connect(connection, &Connection::newMessage, this, &Client::newMessage);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
peers.insert(connection->uniqueId(), connection);
|
2011-04-27 10:05:43 +00:00
|
|
|
QString nick = connection->name();
|
|
|
|
if (!nick.isEmpty())
|
|
|
|
emit newParticipant(nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::disconnected()
|
|
|
|
{
|
|
|
|
if (Connection *connection = qobject_cast<Connection *>(sender()))
|
|
|
|
removeConnection(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::connectionError(QAbstractSocket::SocketError /* socketError */)
|
|
|
|
{
|
|
|
|
if (Connection *connection = qobject_cast<Connection *>(sender()))
|
|
|
|
removeConnection(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::removeConnection(Connection *connection)
|
|
|
|
{
|
2023-06-15 08:18:11 +00:00
|
|
|
if (peers.remove(connection->uniqueId())) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QString nick = connection->name();
|
|
|
|
if (!nick.isEmpty())
|
|
|
|
emit participantLeft(nick);
|
|
|
|
}
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|