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>
|
|
|
|
|
|
|
|
static const int MaxBufferSize = 1024000;
|
|
|
|
|
|
|
|
class Connection : public QTcpSocket
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum ConnectionState {
|
|
|
|
WaitingForGreeting,
|
|
|
|
ReadingGreeting,
|
|
|
|
ReadyForUse
|
|
|
|
};
|
|
|
|
enum DataType {
|
|
|
|
PlainText,
|
|
|
|
Ping,
|
|
|
|
Pong,
|
|
|
|
Greeting,
|
|
|
|
Undefined
|
|
|
|
};
|
|
|
|
|
2019-11-01 12:21:32 +00:00
|
|
|
Connection(QObject *parent = nullptr);
|
|
|
|
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;
|
|
|
|
void setGreetingMessage(const QString &message);
|
|
|
|
bool sendMessage(const QString &message);
|
|
|
|
|
|
|
|
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;
|
2011-04-27 10:05:43 +00:00
|
|
|
QString greetingMessage;
|
|
|
|
QString username;
|
|
|
|
QTimer pingTimer;
|
2018-01-18 02:27:09 +00:00
|
|
|
QElapsedTimer pongTime;
|
|
|
|
QString buffer;
|
2011-04-27 10:05:43 +00:00
|
|
|
ConnectionState state;
|
|
|
|
DataType currentDataType;
|
|
|
|
int transferTimerId;
|
|
|
|
bool isGreetingMessageSent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|