#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();
}