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 <QHostInfo>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
Client::Client()
|
Client::Client()
|
||||||
: peerManager(new PeerManager(this))
|
: peerManager(new PeerManager(this))
|
||||||
{
|
{
|
||||||
@ -36,14 +39,15 @@ QString Client::nickName() const
|
|||||||
|
|
||||||
bool Client::hasConnection(const QHostAddress &senderIp, int senderPort) const
|
bool Client::hasConnection(const QHostAddress &senderIp, int senderPort) const
|
||||||
{
|
{
|
||||||
if (senderPort == -1)
|
auto [begin, end] = peers.equal_range(senderIp);
|
||||||
return peers.contains(senderIp);
|
if (begin == peers.constEnd())
|
||||||
|
|
||||||
if (!peers.contains(senderIp))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const QList<Connection *> connections = peers.values(senderIp);
|
if (senderPort == -1)
|
||||||
for (const Connection *connection : connections) {
|
return true;
|
||||||
|
|
||||||
|
for (; begin != end; ++begin) {
|
||||||
|
Connection *connection = *begin;
|
||||||
if (connection->peerPort() == senderPort)
|
if (connection->peerPort() == senderPort)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -90,8 +94,7 @@ void Client::connectionError(QAbstractSocket::SocketError /* socketError */)
|
|||||||
|
|
||||||
void Client::removeConnection(Connection *connection)
|
void Client::removeConnection(Connection *connection)
|
||||||
{
|
{
|
||||||
if (peers.contains(connection->peerAddress())) {
|
if (peers.remove(connection->peerAddress()) > 0) {
|
||||||
peers.remove(connection->peerAddress());
|
|
||||||
QString nick = connection->name();
|
QString nick = connection->name();
|
||||||
if (!nick.isEmpty())
|
if (!nick.isEmpty())
|
||||||
emit participantLeft(nick);
|
emit participantLeft(nick);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user