43 lines
1001 B
C
43 lines
1001 B
C
|
#ifndef MYTCPCLIENT_H
|
||
|
#define MYTCPCLIENT_H
|
||
|
|
||
|
#include <QTcpSocket>
|
||
|
#include <QTimer>
|
||
|
#include <QObject>
|
||
|
|
||
|
class MyTcpClient : public QObject {
|
||
|
Q_OBJECT
|
||
|
|
||
|
public:
|
||
|
explicit MyTcpClient(QObject *parent = nullptr);
|
||
|
~MyTcpClient();
|
||
|
|
||
|
void connectToServer(const QString &host, quint16 port);
|
||
|
int sendData(char* data,qint64 len);
|
||
|
void waitForRead();
|
||
|
void disconnectFromServer();
|
||
|
|
||
|
signals:
|
||
|
void connected();
|
||
|
void disconnected();
|
||
|
void dataReceived(const QByteArray &data);
|
||
|
void errorOccurred(const QString &errorMsg);
|
||
|
|
||
|
private slots:
|
||
|
void onConnected();
|
||
|
void onReadyRead();
|
||
|
void onDisconnected();
|
||
|
void onErrorOccurred(QAbstractSocket::SocketError socketError);
|
||
|
void onReconnect();
|
||
|
|
||
|
private:
|
||
|
QTcpSocket *socket;
|
||
|
QTimer reconnectTimer; // 自动重连定时器
|
||
|
QTimer heartbeatTimer; // 心跳包定时器
|
||
|
QString serverHost;
|
||
|
quint16 serverPort;
|
||
|
bool shouldReconnect; // 标记是否需要重连
|
||
|
};
|
||
|
|
||
|
#endif // MYTCPCLIENT_H
|