加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ctaskproc.cpp 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
#include "ctaskproc.h"
CTaskProc::CTaskProc(int id,QTcpSocket* &client, QString ip, int port, QObject *parent) :
m_id(id),pLocalClient(client),m_ip(ip),m_port(port), QObject(parent)
{
m_valid = true;
connect(pLocalClient, SIGNAL(readyRead()), this , SLOT(recvClientData()));
connect(pLocalClient, SIGNAL(disconnected()), this, SLOT(diconnect()));
pToSvrClient = new QTcpSocket;
connect(pToSvrClient, SIGNAL(readyRead()),this, SLOT(recvSvrData()));
connect(pToSvrClient, SIGNAL(disconnected()),this, SLOT(diconnect()));
connect(pToSvrClient,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(procConnectErr(QAbstractSocket::SocketError)));
connect(pToSvrClient, SIGNAL(connected()), this, SLOT(procConnectOk()));
QHostAddress serverIP;
serverIP.setAddress(m_ip);
pToSvrClient->connectToHost(serverIP, m_port);
}
CTaskProc::~CTaskProc()
{
pToSvrClient->close();
pLocalClient->close();
delete pToSvrClient;
delete pLocalClient;
}
void CTaskProc::stop()
{
m_valid = false;
disconnect(pToSvrClient, SIGNAL(disconnected()),this, SLOT(diconnect()));
disconnect(pLocalClient, SIGNAL(disconnected()), this, SLOT(diconnect()));
pToSvrClient->close();
pLocalClient->close();
}
void CTaskProc::recvClientData()
{
while (pLocalClient->bytesAvailable()>0)
{
QByteArray datagram;
datagram.resize(pLocalClient->bytesAvailable());
pLocalClient->read(datagram.data(), datagram.size());
pToSvrClient->write(datagram);
emit sigReadLocalClientData(m_id,datagram);
}
}
void CTaskProc::recvSvrData()
{
while (pToSvrClient->bytesAvailable()>0)
{
QByteArray datagram;
datagram.resize(pToSvrClient->bytesAvailable());
pToSvrClient->read(datagram.data(), datagram.size());
pLocalClient->write(datagram);
emit sigReadSvrClientData(m_id,datagram);
}
}
void CTaskProc::procConnectErr(QAbstractSocket::SocketError)
{
qDebug()<<"to svr: "<<m_ip<<":"<<m_port<<": "<<pToSvrClient->errorString();
pLocalClient->close();
}
void CTaskProc::procConnectOk()
{
qDebug()<<"OnTaskStartOk: "<<m_id;
emit sigTaskStartOk(m_id);
}
void CTaskProc::diconnect()
{
if(m_valid)
{
m_valid = false;
pToSvrClient->close();
pLocalClient->close();
emit sigTaskEnd(m_id);
}
}
//发给svr的数据
void CTaskProc::sendToServer(QByteArray &data)
{
pToSvrClient->write(data);
}
//发给dev的数据
void CTaskProc::sendToDevice(QByteArray &data)
{
pLocalClient->write(data);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化