Network chat: avoid double lookups into hash
Task-number: QTBUG-108873 Change-Id: Iafa5a7a1ec99d2414f175f2d30fe03a90c6716f3 Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
parent
9db22a63a9
commit
6eac22e2ca
@ -7,6 +7,9 @@
|
||||
|
||||
#include <QHostInfo>
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
Client::Client()
|
||||
: peerManager(new PeerManager(this))
|
||||
{
|
||||
@ -36,14 +39,15 @@ QString Client::nickName() const
|
||||
|
||||
bool Client::hasConnection(const QHostAddress &senderIp, int senderPort) const
|
||||
{
|
||||
if (senderPort == -1)
|
||||
return peers.contains(senderIp);
|
||||
|
||||
if (!peers.contains(senderIp))
|
||||
auto [begin, end] = peers.equal_range(senderIp);
|
||||
if (begin == peers.constEnd())
|
||||
return false;
|
||||
|
||||
const QList<Connection *> connections = peers.values(senderIp);
|
||||
for (const Connection *connection : connections) {
|
||||
if (senderPort == -1)
|
||||
return true;
|
||||
|
||||
for (; begin != end; ++begin) {
|
||||
Connection *connection = *begin;
|
||||
if (connection->peerPort() == senderPort)
|
||||
return true;
|
||||
}
|
||||
@ -90,8 +94,7 @@ void Client::connectionError(QAbstractSocket::SocketError /* socketError */)
|
||||
|
||||
void Client::removeConnection(Connection *connection)
|
||||
{
|
||||
if (peers.contains(connection->peerAddress())) {
|
||||
peers.remove(connection->peerAddress());
|
||||
if (peers.remove(connection->peerAddress()) > 0) {
|
||||
QString nick = connection->name();
|
||||
if (!nick.isEmpty())
|
||||
emit participantLeft(nick);
|
||||
|
Loading…
x
Reference in New Issue
Block a user