2023-10-30 06:33:08 +08:00
|
|
|
// 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>
|
|
|
|
|
|
|
|
static const int MaxBufferSize = 1024000;
|
|
|
|
|
|
|
|
class Connection : public QTcpSocket
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum ConnectionState {
|
|
|
|
WaitingForGreeting,
|
|
|
|
ReadingGreeting,
|
|
|
|
ReadyForUse
|
|
|
|
};
|
|
|
|
enum DataType {
|
|
|
|
PlainText,
|
|
|
|
Ping,
|
|
|
|
Pong,
|
|
|
|
Greeting,
|
|
|
|
Undefined
|
|
|
|
};
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
explicit Connection(QObject *parent = nullptr);
|
|
|
|
explicit Connection(qintptr socketDescriptor, QObject *parent = nullptr);
|
2023-10-30 06:33:08 +08:00
|
|
|
~Connection();
|
|
|
|
|
|
|
|
QString name() const;
|
|
|
|
void setGreetingMessage(const QString &message);
|
|
|
|
bool sendMessage(const QString &message);
|
|
|
|
|
|
|
|
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;
|
2023-11-02 05:23:55 +08:00
|
|
|
QString greetingMessage = tr("undefined");
|
|
|
|
QString username = tr("unknown");
|
2023-10-30 06:33:08 +08:00
|
|
|
QTimer pingTimer;
|
|
|
|
QElapsedTimer pongTime;
|
|
|
|
QString buffer;
|
2023-11-02 05:23:55 +08:00
|
|
|
ConnectionState state = WaitingForGreeting;
|
|
|
|
DataType currentDataType = Undefined;
|
|
|
|
int transferTimerId = -1;
|
|
|
|
bool isGreetingMessageSent = false;
|
2023-10-30 06:33:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|