kio: KIO::SlaveInterface optimization

so it happens that not the whole data is send on data request sometimes..

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-29 08:59:33 +03:00
parent 61fdbf2495
commit 954d16ff9e
4 changed files with 17 additions and 54 deletions

View file

@ -335,7 +335,7 @@ void SlaveBase::dispatchLoop()
int ret = -1;
if (waitForIncommingTask(&d->appConnection, ms)) {
// dispatch application messages
int cmd;
int cmd = 0;
QByteArray data;
ret = kReadCommand(&d->appConnection, &cmd, data);

View file

@ -69,7 +69,6 @@ SlaveInterfacePrivate::SlaveInterfacePrivate(const QString &protocol)
m_pid(0),
m_port(0),
dead(false),
contact_started(time(0)),
m_idleSince(0),
m_refCount(1)
{
@ -93,7 +92,7 @@ SlaveInterface::SlaveInterface(const QString &protocol, QObject *parent)
// NOTE: using long/complex server name can cause reconnection issues
const QString serveraddress = QString::fromLatin1("kio_") + QString::number(qrand());
const QString serveraddress = QString::fromLatin1("kio_") + QString::number(quintptr(d_ptr), 16);
d_ptr->slaveconnserver = new QSocketServer(this);
d_ptr->slaveconnserver->init(serveraddress);
if (!d_ptr->slaveconnserver->listen()) {
@ -102,8 +101,6 @@ SlaveInterface::SlaveInterface(const QString &protocol, QObject *parent)
}
d_ptr->slavenotifier = new QSocketNotifier(d_ptr->slaveconnserver->socketDescriptor(), QSocketNotifier::Read, this);
connect(d_ptr->slavenotifier, SIGNAL(activated(int)), this, SLOT(accept()));
d_ptr->connection = new QSocketDevice(this);
}
SlaveInterface::~SlaveInterface()
@ -165,7 +162,9 @@ void SlaveInterface::deref()
Q_D(SlaveInterface);
d->m_refCount--;
if (!d->m_refCount) {
d->connection->disconnect(this);
if (d->connection) {
d->connection->disconnect(this);
}
this->disconnect();
deleteLater();
}
@ -598,14 +597,14 @@ void SlaveInterface::messageBox(int type, const QString &text, const QString &ca
void SlaveInterface::accept()
{
Q_D(SlaveInterface);
if (d->connection) {
// kWarning() << "there is already a connection";
delete d->connection;
}
Q_ASSERT(!d->connection);
d->connection = d->slaveconnserver->acceptConnection();
delete d->slavenotifier;
d->slavenotifier = nullptr;
connect(d->connection, SIGNAL(readyRead()), SLOT(gotInput()));
if (d->m_tasks.isEmpty()) {
kWarning() << "no tasks";
}
while (!d->m_tasks.isEmpty()) {
const KIO::Task task = d->m_tasks.dequeue();
kSendCommandNow(d->connection, task.cmd, task.data);
@ -621,6 +620,7 @@ void SlaveInterface::gotInput()
}
ref();
if (!dispatch()) {
Q_ASSERT(d->connection);
d->connection->close();
d->dead = true;
QString arg = d->m_protocol;
@ -637,43 +637,4 @@ void SlaveInterface::gotInput()
// Here we might be dead!!
}
void SlaveInterface::timeout()
{
Q_D(SlaveInterface);
if (d->dead) {
// already dead? then slaveDied was emitted and we are done
return;
}
if (d->connection->isOpen()) {
return;
}
kDebug(7002) << "slave failed to connect to application pid=" << d->m_pid
<< " protocol=" << d->m_protocol;
if (d->m_pid && (::kill(d->m_pid, 0) == 0)) {
int delta_t = (int) difftime(time(0), d->contact_started);
kDebug(7002) << "slave is slow... pid=" << d->m_pid << " t=" << delta_t;
if (delta_t < SLAVE_CONNECTION_TIMEOUT_MAX) {
QTimer::singleShot(1000*SLAVE_CONNECTION_TIMEOUT_MIN, this, SLOT(timeout()));
return;
}
}
kDebug(7002) << "Houston, we lost our slave, pid=" << d->m_pid;
d->connection->close();
d->dead = true;
QString arg = d->m_protocol;
if (!d->m_host.isEmpty()) {
arg += QString::fromLatin1("://") + d->m_host;
}
kDebug(7002) << "slave died pid = " << d->m_pid;
ref();
// Tell the job about the problem.
emit error(ERR_SLAVE_DIED, arg);
// Tell the scheduler about the problem.
emit slaveDied(this);
// After the above signal we're dead!!
deref();
}
#include "moc_slaveinterface.cpp"

View file

@ -262,7 +262,6 @@ protected Q_SLOTS:
void calcSpeed();
void accept();
void gotInput();
void timeout();
protected:
SlaveInterfacePrivate* const d_ptr;

View file

@ -44,6 +44,10 @@ static bool kSendCommandNow(QSocketDevice *socket, int cmd, const QByteArray &da
if (data.size() > 0xffffff) {
return false;
}
if (!socket) {
kWarning() << "null socket";
return false;
}
if (!socket->isOpen()) {
return false;
}
@ -58,7 +62,7 @@ static bool kSendCommandNow(QSocketDevice *socket, int cmd, const QByteArray &da
static bool kSendCommand(QSocketDevice *socket, int cmd, const QByteArray &data,
QQueue<KIO::Task> &queue, const bool suspended)
{
if (suspended || !socket->isOpen()) {
if (suspended || !socket || !socket->isOpen()) {
KIO::Task task;
task.cmd = cmd;
task.data = data;
@ -102,8 +106,8 @@ static int kReadCommand(QSocketDevice *socket, int* cmd, QByteArray &data)
if (readresult < 0) {
if (errno == EAGAIN) {
if (!socket->waitForReadyRead(500)) {
kWarning() << "kReadCommand data still not ariving" << readtotal << len << socket->errorString();
continue;
kWarning() << "kReadCommand data still not arriving" << readtotal << len << socket->errorString();;
return readtotal;
}
continue;
}
@ -146,7 +150,6 @@ public:
pid_t m_pid;
quint16 m_port;
bool dead;
time_t contact_started;
time_t m_idleSince;
int m_refCount;
};