qt5base-lts/examples/network/network-chat/client.h
Mårten Nordheim 2a80adc2f6 Network-chat example: Use QHash for peers
We don't key it on IP address anymore so we can drop
the use of QMultiHash.
This also requires moving the connections for error and disconnected
to readyForUse so we don't remove an active connection when a second
connection attempt happens from the same peer process.
But since we still need to deallocate those connection attempts
if they error out or simply disconnect, we connect their signals to the
QObject::deleteLater slot. In some cases we might call deleteLater twice
but that's fine.

Change-Id: I48c27de1e51a52ef61cfb941a7a81b358ae9ce3f
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io>
2023-07-17 18:49:16 +02:00

46 lines
979 B
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef CLIENT_H
#define CLIENT_H
#include "server.h"
#include <QAbstractSocket>
#include <QHash>
#include <QHostAddress>
class PeerManager;
class Client : public QObject
{
Q_OBJECT
public:
Client();
void sendMessage(const QString &message);
QString nickName() const;
bool hasConnection(const QByteArray &peerUniqueId) const;
signals:
void newMessage(const QString &from, const QString &message);
void newParticipant(const QString &nick);
void participantLeft(const QString &nick);
private slots:
void newConnection(Connection *connection);
void connectionError(QAbstractSocket::SocketError socketError);
void disconnected();
void readyForUse();
private:
void removeConnection(Connection *connection);
PeerManager *peerManager;
Server server;
QHash<QByteArray, Connection *> peers;
};
#endif