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
|
|
|
|
|
|
|
#ifndef CONNECTION_H
|
|
|
|
#define CONNECTION_H
|
|
|
|
|
2018-01-18 02:27:09 +00:00
|
|
|
#include <QCborStreamReader>
|
|
|
|
#include <QCborStreamWriter>
|
|
|
|
#include <QElapsedTimer>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QHostAddress>
|
|
|
|
#include <QString>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
class Connection : public QTcpSocket
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum ConnectionState {
|
|
|
|
WaitingForGreeting,
|
|
|
|
ReadingGreeting,
|
2023-06-14 16:48:19 +00:00
|
|
|
ProcessingGreeting,
|
2011-04-27 10:05:43 +00:00
|
|
|
ReadyForUse
|
|
|
|
};
|
|
|
|
enum DataType {
|
|
|
|
PlainText,
|
|
|
|
Ping,
|
|
|
|
Pong,
|
|
|
|
Greeting,
|
|
|
|
Undefined
|
|
|
|
};
|
|
|
|
|
2023-05-16 11:18:43 +00:00
|
|
|
explicit Connection(QObject *parent = nullptr);
|
|
|
|
explicit Connection(qintptr socketDescriptor, QObject *parent = nullptr);
|
2018-01-18 02:27:09 +00:00
|
|
|
~Connection();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QString name() const;
|
2023-06-14 16:48:19 +00:00
|
|
|
void setGreetingMessage(const QString &message, const QByteArray &uniqueId);
|
2011-04-27 10:05:43 +00:00
|
|
|
bool sendMessage(const QString &message);
|
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
QByteArray uniqueId() const;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
signals:
|
|
|
|
void readyForUse();
|
|
|
|
void newMessage(const QString &from, const QString &message);
|
|
|
|
|
|
|
|
protected:
|
2016-06-15 08:12:35 +00:00
|
|
|
void timerEvent(QTimerEvent *timerEvent) override;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void processReadyRead();
|
|
|
|
void sendPing();
|
|
|
|
void sendGreetingMessage();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool hasEnoughData();
|
2018-01-18 02:27:09 +00:00
|
|
|
void processGreeting();
|
2011-04-27 10:05:43 +00:00
|
|
|
void processData();
|
|
|
|
|
2018-01-18 02:27:09 +00:00
|
|
|
QCborStreamReader reader;
|
|
|
|
QCborStreamWriter writer;
|
2023-05-16 11:17:18 +00:00
|
|
|
QString greetingMessage = tr("undefined");
|
|
|
|
QString username = tr("unknown");
|
2011-04-27 10:05:43 +00:00
|
|
|
QTimer pingTimer;
|
2018-01-18 02:27:09 +00:00
|
|
|
QElapsedTimer pongTime;
|
|
|
|
QString buffer;
|
2023-06-14 16:48:19 +00:00
|
|
|
QByteArray localUniqueId;
|
|
|
|
QByteArray peerUniqueId;
|
2023-05-16 11:17:18 +00:00
|
|
|
ConnectionState state = WaitingForGreeting;
|
|
|
|
DataType currentDataType = Undefined;
|
|
|
|
int transferTimerId = -1;
|
|
|
|
bool isGreetingMessageSent = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|