代码拉取完成,页面将自动刷新
#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();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。