#include "network_comm.h" #include "WorkThread/remote_ctrl.h" Network_Comm::Network_Comm(Remote_Ctrl *rctl) { remoteCtrl = rctl; workThread = remoteCtrl->workThread; } Network_Comm::~Network_Comm() { } void Network_Comm::readClient() { QByteArray barray = clientConnection->readAll(); quint8 *data = (quint8 *)barray.data(); /* quint8 tmp = data[3]; data[3] = data[0]; data[0] = tmp; tmp = data[2]; data[2] = data[1]; data[1] = tmp; */ //qDebug("barray.length = %d",barray.length()); //for(int n=0;nremoteState.conn_type; if(contype == 2){ //not ble,can not work at the same time return; } if(remoteCtrl->processRxData(recvframe,&sendata,1)){ quint8 *txdata = (quint8 *)(&sendata); /* tmp = txdata[3]; txdata[3] = txdata[0]; txdata[0] = tmp; tmp = txdata[2]; txdata[2] = txdata[1]; txdata[1] = tmp; sendata.CRC = CRC16::CalCRC16(&sendata,sendata.Len); */ /* qDebug("------- write data ---------"); for(int n=0;nstate()){ quint64 wn = clientConnection->write((char *)txdata, sendata.Len); //clientConnection->waitForBytesWritten(1000); if(wn != sendata.Len) { qDebug("write error:"+clientConnection->errorString().toLocal8Bit()); } } else{ qDebug("write error: QAbstractSocket::UnconnectedState"); } } } void Network_Comm::clientService() { clientConnection = new QTcpSocket(); int timeOutCnt = 0; quint8 tcp_type = 0; while(1) { QString hostip = workThread->sysParamXml.server_ip; quint16 port = workThread->sysParamXml.server_port; clientConnection->connectToHost(QHostAddress(hostip), port); //qDebug("connectToHost...."); if( clientConnection->waitForConnected(10000) ) { while(1) { if(clientConnection->waitForReadyRead(10000)) //10S { timeOutCnt = 0; readClient(); } else { timeOutCnt++; if(timeOutCnt >= 6){ //1min break; } //qDebug("error-2:"+clientConnection->errorString().toLocal8Bit()); } if(QAbstractSocket::ConnectedState != clientConnection->state()){ //qDebug()<<"QAbstractSocket::UnconnectedState"; break; } tcp_type = workThread->sysParamXml.tcp_type; if(1 == tcp_type){ break; } } clientConnection->close(); } else{ //qDebug("error-1:"+clientConnection->errorString().toLocal8Bit()); msleep(5000); } if(0 == tcp_type){ tcp_type = workThread->sysParamXml.tcp_type; } if(1 == tcp_type){ if(0 != clientConnection){ clientConnection->close(); delete clientConnection; clientConnection = 0; qDebug("delete clientConnection"); } break; } } } void Network_Comm::serverService() { socketServer = new QTcpServer(); hostPort = 9600; socketServer->listen(QHostAddress::Any, hostPort); socketServer->setMaxPendingConnections(1); bool timeOut = false; int timeOutCnt = 0; quint8 tcp_type = 1; while(1) { //qDebug("1--Wait For Connection!"); if( socketServer->waitForNewConnection(10000, &timeOut) ) { clientConnection = socketServer->nextPendingConnection(); if(NULL != clientConnection) { //qDebug("2--Read Data From Connection!"); timeOutCnt = 0; while(1) { if(clientConnection->waitForReadyRead(10000)) //10S { timeOutCnt = 0; readClient(); } else { timeOutCnt++; if(timeOutCnt >= 6){ //1min break; } //qDebug("error-1:"+clientConnection->errorString().toLocal8Bit()); } if(QAbstractSocket::ConnectedState != clientConnection->state()){ //qDebug()<<"QAbstractSocket::UnconnectedState"; break; } tcp_type = workThread->sysParamXml.tcp_type; if(0 == tcp_type){ break; } } clientConnection->close(); delete clientConnection; clientConnection = 0; qDebug("delete clientConnection"); } } else { //qDebug()<<"waitForNewConnection timeout: " << timeOut; } if(1 == tcp_type){ tcp_type = workThread->sysParamXml.tcp_type; } if(0 == tcp_type){ if(0 != clientConnection){ clientConnection->close(); delete clientConnection; clientConnection = 0; qDebug("delete clientConnection"); } if(0 != socketServer){ socketServer->close(); delete socketServer; socketServer = 0; qDebug("delete socketServer"); } break; } } } void Network_Comm::run() { while(1){ quint8 tcp_type = workThread->sysParamXml.tcp_type; if(0 == tcp_type){ clientService(); } else if(1 == tcp_type){ serverService(); } msleep(100); } }