1 Star 0 Fork 0

王泓/deb_maker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
PrivilegedCmdDispatcher.cpp 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
oosman 提交于 2020-06-24 12:28 . init
#include "PrivilegedCmdDispatcher.h"
#include <QDebug>
#include <QTcpSocket>
PrivilegedCmdDispatcher::PrivilegedCmdDispatcher(QObject *parent)
: QObject(parent)
, m_socket(std::make_shared<QTcpSocket>(this))
{
connect(m_socket.get(), &QTcpSocket::connected,
this, &PrivilegedCmdDispatcher::connected);
connect(m_socket.get(), &QTcpSocket::disconnected,
this, &PrivilegedCmdDispatcher::disconnected);
connect(m_socket.get(), &QIODevice::readyRead,
this, &PrivilegedCmdDispatcher::onReadSocket);
}
PrivilegedCmdDispatcher::~PrivilegedCmdDispatcher()
{
}
void PrivilegedCmdDispatcher::connectToServer()
{
const int DEFAULT_PORT = 55555;
m_socket->connectToHost("127.0.0.1", DEFAULT_PORT);
}
void PrivilegedCmdDispatcher::onReadSocket()
{
if(!m_socket)
{
return;
}
m_readBuffer.append(m_socket->read(m_socket->bytesAvailable()));
QStringList lines = QString(m_readBuffer).split("\n");
for(auto line : lines)
{
qDebug() << line;
if(line == "done")
{
emit cmdProcessed(true);
break;
}
}
m_readBuffer.clear();
}
void PrivilegedCmdDispatcher::onDisconnected()
{
qDebug() << "onDisconnected";
closeConnection();
emit cmdProcessed(false);
}
void PrivilegedCmdDispatcher::sendCmd(const QByteArray &cmd)
{
if(!m_socket || !m_socket->isWritable())
{
return;
}
qDebug() << "sendCmd: " << QString(cmd);
m_socket->write(cmd);
}
void PrivilegedCmdDispatcher::closeConnection()
{
if(m_socket)
{
m_socket->close();
}
m_socket.reset();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kuletco/deb_maker.git
git@gitee.com:kuletco/deb_maker.git
kuletco
deb_maker
deb_maker
master

搜索帮助