culturered_client/ProjectorDisplay/TcpClient.h
2024-09-07 11:34:44 +08:00

40 lines
743 B
C++

#pragma once
#include <QObject>
#include <QTcpSocket>
#include <QTimer>
class TcpClient : public QObject {
Q_OBJECT
public:
explicit TcpClient(QObject* parent = nullptr);
~TcpClient() override;
bool ConnectToServer(const QString& address);
void Disconnect();
bool IsConnected() const;
bool IsNeedConnect() const;
Q_SIGNALS:
void MessageReceivered(const QString& message);
protected:
void OnConnected();
void OnDisconnected();
void OnReadyRead();
void OnStateChanged(QAbstractSocket::SocketState state);
void OnError(QAbstractSocket::SocketError error);
void SendHeartbeat();
void OnConntectTimeout();
private:
QTcpSocket* m_socket;
QTimer* m_heartbeatTimer;
QString m_ip;
qint16 m_port;
QTimer* m_connectTimer;
};