2025-08-27 23:10:36 +08:00
|
|
|
#ifndef VRTCPCLIENT_H
|
|
|
|
|
#define VRTCPCLIENT_H
|
|
|
|
|
|
|
|
|
|
#include "IVrTcpClient.h"
|
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
|
|
class VrTcpClient : public IVrTcpClient
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit VrTcpClient(QObject *parent = nullptr);
|
|
|
|
|
~VrTcpClient() override;
|
|
|
|
|
|
|
|
|
|
// 接口实现
|
|
|
|
|
bool connectToServer(const QString &host, quint16 port) override;
|
|
|
|
|
bool connectToServer(const QHostAddress &address, quint16 port) override;
|
|
|
|
|
void disconnectFromServer() override;
|
|
|
|
|
qint64 sendData(const QByteArray &data) override;
|
|
|
|
|
qint64 sendData(const char *data, qint64 size) override;
|
|
|
|
|
bool isConnected() const override;
|
|
|
|
|
QHostAddress serverAddress() const override;
|
|
|
|
|
quint16 serverPort() const override;
|
|
|
|
|
|
|
|
|
|
// 额外功能:自动重连
|
|
|
|
|
void setAutoReconnect(bool autoReconnect);
|
|
|
|
|
bool autoReconnect() const;
|
|
|
|
|
|
|
|
|
|
void setReconnectInterval(int msec);
|
|
|
|
|
int reconnectInterval() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QTcpSocket *m_socket;
|
|
|
|
|
QTimer *m_reconnectTimer;
|
|
|
|
|
bool m_autoReconnect = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // VRTCPCLIENT_H
|