Qt TCP 断开重连

#include "TCPConnectEngine.h"
#include "AnalysisJson.h"
#include "SpeechFusion.h"
#include "Config.h"
TCPConnectEngine * TCPConnectEngine::GetInstance()
{
	static TCPConnectEngine *pInstance = NULL;
	if (pInstance == NULL)
	{
		pInstance = new TCPConnectEngine();
	}
	return pInstance;
}

TCPConnectEngine::TCPConnectEngine(QObject *parent)
	: QObject(parent)
{
	isConnected = false;
	m_times = 0;
	iBagSize = 0;
	tcpClient = new QTcpSocket(this);
	connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readMessage()));
	connect(tcpClient, SIGNAL(disconnected()), this, SLOT(outLine()));
	timerConnectFailed = new QTimer(this);//连接失败重新连接
	connect(timerConnectFailed, SIGNAL(timeout()), this, SLOT(slotKeepConnection()));
	
	Config pc ;
	pc.readFile();
	m_strIP = pc.getIp();
	m_strPort = pc.getPort();
	newConnect();
	
}

TCPConnectEngine::~TCPConnectEngine()
{

}
void TCPConnectEngine::keepConnectHost()
{
	QString sendString = QString("sg\r\n");
	QByteArray arraymixer = sendString.toLatin1();
	tcpClient->write(arraymixer);
}

void TCPConnectEngine::disConnectHost()
{
	if (isConnected)
	{
		tcpClient->disconnectFromHost();
		//tcpClient->waitForDisconnected();
	}
	isConnected = false;
	m_times = 0;
	timerConnectFailed->stop();
	SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("连接已断开..."),false);

}

void TCPConnectEngine::newConnect()
{
	if (!isConnected)
	{
		tcpClient->abort();
		
		tcpClient->connectToHost(m_strIP, m_strPort.toInt());
		bool bret = this->tcpClient->waitForConnected(1000);
		if (bret)
		{
		
			isConnected = true;
			SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("连接成功"),true);
		}
		else
		{
			isConnected = false;
			timerConnectFailed->start(3000);
			SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("连接失败 正在重新连接..."), false);
		}
	}
}

void TCPConnectEngine::readMessage()
{
	QTcpSocket *obj = (QTcpSocket*)sender();
	QByteArray array = obj->readAll();
	m_Avaliarray.append(array);
	QString strBag = QString::fromUtf8(m_Avaliarray);
	iBagSize = strBag.mid(0, 16).toInt();
	int curBagsize = m_Avaliarray.size();
	//完整的包
	if (m_Avaliarray.size()-16 == iBagSize)
	{
		QByteArray arrayMID = m_Avaliarray.mid(16, iBagSize);

		AnalysisJson::GetInstance()->analysisJson(arrayMID);
		m_Avaliarray.clear();
	}
	else if (m_Avaliarray.size() - 16 >= iBagSize)//处理粘包
	{
		int mBagSize = 0;
		for (int i= 0;i<curBagsize;i+= mBagSize +16)
		{
			QString strBagm = QString::fromUtf8(m_Avaliarray);
			mBagSize = strBagm.mid(0, 16).toInt();
			if (m_Avaliarray.size() - 16 == mBagSize)
			{
				QByteArray arrayMID = m_Avaliarray.mid(16, mBagSize);

				AnalysisJson::GetInstance()->analysisJson(arrayMID);
				m_Avaliarray.clear();
			}
			else if(m_Avaliarray.size() - 16 < mBagSize){

			}
			else if (m_Avaliarray.size() - 16 >= mBagSize)//处理粘包
			{
				QByteArray arrayMID = m_Avaliarray.mid(16, mBagSize);
				AnalysisJson::GetInstance()->analysisJson(arrayMID);
				m_Avaliarray.remove(0, mBagSize + 16);
			}
			
		}

// 		for (int i = 0; i < m_Avaliarray.size(); i++)
// 		{
// 			//m_Avaliarray.mid(iBagSize, m_Avaliarray.size());
// 			QByteArray arrayMID = m_Avaliarray.mid(16, iBagSize);
// 			AnalysisJson::GetInstance()->analysisJson(arrayMID);
// 			m_Avaliarray.remove(0, iBagSize);
// 		}
		

	}
	else if (m_Avaliarray.size() - 16 < iBagSize)
	{
	}

}

void TCPConnectEngine::outLine()//断开连接
{
	isConnected = false;
	SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("连接已断开..."), false);
}

void TCPConnectEngine::sendTo(QByteArray array)
{
	tcpClient->write(array);
}

void TCPConnectEngine::slotKeepConnection()
{
	if (m_times == 3)
	{
		timerConnectFailed->stop();
		SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("连接失败..."), false);
		return;
	}
	tcpClient->abort();
	tcpClient->connectToHost(m_strIP, m_strPort.toInt());

	if (!tcpClient->waitForConnected(2000))
	{
		isConnected = false;

		m_times++;
		SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("尝试连接失败 正在重新连接...%1").arg(4-m_times), false);
	}
	else
	{
		isConnected = true;
		m_times = 0;
		timerConnectFailed->stop();
		SpeechFusion::GetInstance()->updateStatus(QString::fromLocal8Bit("连接成功"),true);

	}
}

void TCPConnectEngine::slotConfigIP(QString strip, QString strport)
{
	if (!isConnected)
	{
		timerConnectFailed->stop();
		m_times = 0;

	}
	m_strIP = strip;
	m_strPort = strport;
	newConnect();
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值