226d06402b
The system was just treating IP (and optionally port) as a unique identifier, so if a peer had multiple possible paths to a client they would connect multiple times. This fixes that by generating using QUuid in each client. We then use this during broadcast, replacing the username we sent before (which was not used), and as part of the greeting. The greeting now is more complex, since we need to send both username and the ID. Change-Id: I6c6c2ffd5198406aad48445a68dd6aab36de69c0 Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
77 lines
1.7 KiB
C++
77 lines
1.7 KiB
C++
// Copyright (C) 2016 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
#ifndef CONNECTION_H
|
|
#define CONNECTION_H
|
|
|
|
#include <QCborStreamReader>
|
|
#include <QCborStreamWriter>
|
|
#include <QElapsedTimer>
|
|
#include <QHostAddress>
|
|
#include <QString>
|
|
#include <QTcpSocket>
|
|
#include <QTimer>
|
|
|
|
class Connection : public QTcpSocket
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
enum ConnectionState {
|
|
WaitingForGreeting,
|
|
ReadingGreeting,
|
|
ProcessingGreeting,
|
|
ReadyForUse
|
|
};
|
|
enum DataType {
|
|
PlainText,
|
|
Ping,
|
|
Pong,
|
|
Greeting,
|
|
Undefined
|
|
};
|
|
|
|
explicit Connection(QObject *parent = nullptr);
|
|
explicit Connection(qintptr socketDescriptor, QObject *parent = nullptr);
|
|
~Connection();
|
|
|
|
QString name() const;
|
|
void setGreetingMessage(const QString &message, const QByteArray &uniqueId);
|
|
bool sendMessage(const QString &message);
|
|
|
|
QByteArray uniqueId() const;
|
|
|
|
signals:
|
|
void readyForUse();
|
|
void newMessage(const QString &from, const QString &message);
|
|
|
|
protected:
|
|
void timerEvent(QTimerEvent *timerEvent) override;
|
|
|
|
private slots:
|
|
void processReadyRead();
|
|
void sendPing();
|
|
void sendGreetingMessage();
|
|
|
|
private:
|
|
bool hasEnoughData();
|
|
void processGreeting();
|
|
void processData();
|
|
|
|
QCborStreamReader reader;
|
|
QCborStreamWriter writer;
|
|
QString greetingMessage = tr("undefined");
|
|
QString username = tr("unknown");
|
|
QTimer pingTimer;
|
|
QElapsedTimer pongTime;
|
|
QString buffer;
|
|
QByteArray localUniqueId;
|
|
QByteArray peerUniqueId;
|
|
ConnectionState state = WaitingForGreeting;
|
|
DataType currentDataType = Undefined;
|
|
int transferTimerId = -1;
|
|
bool isGreetingMessageSent = false;
|
|
};
|
|
|
|
#endif
|