GrabBag/VrNets/tcpClient/Src/VrTcpClient.cpp

150 lines
3.6 KiB
C++
Raw Normal View History

#include "VrTcpClient.h"
#include <QTcpSocket>
#include <QTimer>
#include <QDebug>
VrTcpClient::VrTcpClient(QObject *parent)
: IVrTcpClient(parent)
, m_socket(new QTcpSocket(this))
, m_reconnectTimer(new QTimer(this))
{
// 设置重连定时器
m_reconnectTimer->setInterval(5000); // 5秒重连间隔
m_reconnectTimer->setSingleShot(true);
// 连接信号槽
connect(m_socket, &QTcpSocket::connected, this, [this]() {
setConnected(true);
m_reconnectTimer->stop();
emit connected();
});
connect(m_socket, &QTcpSocket::disconnected, this, [this]() {
setConnected(false);
emit disconnected();
// 如果设置了自动重连,则启动重连定时器
if (m_autoReconnect) {
m_reconnectTimer->start();
}
});
connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred),
this, [this](QAbstractSocket::SocketError error) {
setConnected(false);
emit connectionError(m_socket->errorString());
});
connect(m_socket, &QTcpSocket::readyRead, this, [this]() {
QByteArray data = m_socket->readAll();
if (!data.isEmpty()) {
emit dataReceived(data);
}
});
connect(m_socket, &QTcpSocket::bytesWritten, this, &IVrTcpClient::dataSent);
connect(m_reconnectTimer, &QTimer::timeout, this, [this]() {
if (m_autoReconnect && !m_serverAddress.isNull() && m_serverPort > 0) {
connectToServer(m_serverAddress, m_serverPort);
}
});
}
VrTcpClient::~VrTcpClient()
{
disconnectFromServer();
}
bool VrTcpClient::connectToServer(const QString &host, quint16 port)
{
m_serverAddress = QHostAddress(host);
m_serverPort = port;
if (m_serverAddress.isNull()) {
// 如果是主机名,直接连接
m_socket->connectToHost(host, port);
return true;
} else {
return connectToServer(m_serverAddress, port);
}
}
bool VrTcpClient::connectToServer(const QHostAddress &address, quint16 port)
{
m_serverAddress = address;
m_serverPort = port;
if (isConnected()) {
disconnectFromServer();
}
m_socket->connectToHost(address, port);
return true;
}
void VrTcpClient::disconnectFromServer()
{
m_reconnectTimer->stop();
if (m_socket->state() != QAbstractSocket::UnconnectedState) {
m_socket->disconnectFromHost();
if (m_socket->state() == QAbstractSocket::ConnectedState) {
m_socket->waitForDisconnected(3000);
}
}
setConnected(false);
}
qint64 VrTcpClient::sendData(const QByteArray &data)
{
if (!isConnected()) {
return -1;
}
return m_socket->write(data);
}
qint64 VrTcpClient::sendData(const char *data, qint64 size)
{
if (!isConnected()) {
return -1;
}
return m_socket->write(data, size);
}
bool VrTcpClient::isConnected() const
{
return m_connected && m_socket->state() == QAbstractSocket::ConnectedState;
}
QHostAddress VrTcpClient::serverAddress() const
{
return m_serverAddress;
}
quint16 VrTcpClient::serverPort() const
{
return m_serverPort;
}
void VrTcpClient::setAutoReconnect(bool autoReconnect)
{
m_autoReconnect = autoReconnect;
if (!autoReconnect) {
m_reconnectTimer->stop();
}
}
bool VrTcpClient::autoReconnect() const
{
return m_autoReconnect;
}
void VrTcpClient::setReconnectInterval(int msec)
{
m_reconnectTimer->setInterval(msec);
}
int VrTcpClient::reconnectInterval() const
{
return m_reconnectTimer->interval();
}