Skip to content

Commit

Permalink
chore: move readpipedatatask logic to waylandcopyclient
Browse files Browse the repository at this point in the history
move the logic to waylandcopyclient, seems it will reduced when the
signal cannot received by task

Log:
  • Loading branch information
Decodetalkers committed Aug 4, 2023
1 parent f877f4c commit a14f691
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 156 deletions.
102 changes: 0 additions & 102 deletions dde-clipboard-daemon/readpipedatatask.cpp

This file was deleted.

49 changes: 0 additions & 49 deletions dde-clipboard-daemon/readpipedatatask.h

This file was deleted.

91 changes: 86 additions & 5 deletions dde-clipboard-daemon/waylandcopyclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "waylandcopyclient.h"
#include "readpipedatatask.h"

#include <QEventLoop>
#include <QMimeData>
Expand All @@ -22,6 +21,11 @@
#include <DWayland/Client/datacontroloffer.h>

#include <unistd.h>
#include <mutex>

static std::mutex PIPELINE_GUARD;

constexpr int BYTE_MAX = 1024 * 4;

static const QString ApplicationXQtImageLiteral QStringLiteral("application/x-qt-image");

Expand Down Expand Up @@ -167,6 +171,7 @@ void WaylandCopyClient::init()
m_connectionThread->start();
m_connectionThreadObject->initConnection();
connect(this, &WaylandCopyClient::dataCopied, this, &WaylandCopyClient::onDataCopied);
connect(this, &WaylandCopyClient::dataReady, this, &WaylandCopyClient::taskDataReady);
}

void WaylandCopyClient::setupRegistry(Registry *registry)
Expand Down Expand Up @@ -217,13 +222,89 @@ void WaylandCopyClient::onDataOffered(KWayland::Client::DataControlOfferV1* offe
// NOTE: no thread, this should be block
void WaylandCopyClient::execTask(const QStringList &mimeTypes, DataControlOfferV1 *offer)
{
m_pipeIsForcedClosed = false;
for (const QString &mimeType : mimeTypes) {
ReadPipeDataTask *task = new ReadPipeDataTask(m_connectionThreadObject, offer, mimeType, this);
connect(this, &WaylandCopyClient::dataOfferedNew, task, &ReadPipeDataTask::forceClosePipeLine);
connect(task, &ReadPipeDataTask::dataReady, this, &WaylandCopyClient::taskDataReady);
onDataOfferedTask(offer, mimeType);
}
}

task->run();
void WaylandCopyClient::onDataOfferedTask(DataControlOfferV1 *offer, const QString &mimeType)
{
if (!m_connectionThreadObject || !offer || mimeType.isEmpty()) {
return;
}
int pipeFds[2];

if (pipe(pipeFds) != 0) {
qWarning() << "Create pipe failed.";

// 避免返回数据量少
Q_EMIT dataReady((qint64)offer, mimeType, QByteArray());
return;
}

// 根据mime类取数据,写入pipe中
offer->receive(mimeType, pipeFds[1]);
m_connectionThreadObject->roundtrip();
close(pipeFds[1]);

// force to close the piepline
connect(this, &WaylandCopyClient::dataOfferedNew, this, [pipeFds, this] {
std::lock_guard<std::mutex> guard(PIPELINE_GUARD);
m_pipeIsForcedClosed = true;
close(pipeFds[0]);
});

qint64 offerId = (qint64)offer;

// FIXME: in QtConcurrent run , when signal is emit, sometimes it cannot be recepted
// So sometimes copy will not succcessed
QtConcurrent::run([this, pipeFds, mimeType, offerId] {
QByteArray data;
bool is_successed = readData(pipeFds[0], data);
std::lock_guard<std::mutex> guard(PIPELINE_GUARD);
if (m_pipeIsForcedClosed) {
qDebug() << "pipeline is block here;" << "mimetype is: " << mimeType;
m_pipeIsForcedClosed = false;
return;
}

if (!is_successed) {
qDebug() << "Cannot open pipeline";
return;
}

Q_EMIT dataReady(offerId, mimeType, data);
m_pipeIsForcedClosed = false;
close(pipeFds[0]);
});

}

bool WaylandCopyClient::readData(int fd, QByteArray &data)
{
QFile readPipe;
if (!readPipe.open(fd, QIODevice::ReadOnly)) {
return false;
}

if (!readPipe.isReadable()) {
qWarning() << "Pipe is not readable";
readPipe.close();
return false;
}

int retCount = BYTE_MAX;
do {
QByteArray bytes = readPipe.read(BYTE_MAX);
retCount = bytes.count();
if (!bytes.isEmpty())
data.append(bytes);
} while (retCount == BYTE_MAX);

readPipe.close();

return true;
}

void WaylandCopyClient::taskDataReady(qint64 offer, const QString &mimeType, const QByteArray &data)
Expand Down
5 changes: 5 additions & 0 deletions dde-clipboard-daemon/waylandcopyclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class WaylandCopyClient : public QObject
void dataCopied();
// when a new dataOffer request in ,then send it
void dataOfferedNew();
void dataReady(qint64, const QString &, const QByteArray &);

protected slots:
void onSendDataRequest(const QString &mimeType, qint32 fd) const;
Expand All @@ -73,6 +74,9 @@ protected slots:
void execTask(const QStringList &mimeTypes, DataControlOfferV1 *offer);
void taskDataReady(qint64, const QString &mimeType, const QByteArray &data);

void onDataOfferedTask(DataControlOfferV1 *offer, const QString &mimeType);
bool readData(int fd, QByteArray &data);

private:
QThread *m_connectionThread;
ConnectionThread *m_connectionThreadObject;
Expand All @@ -85,6 +89,7 @@ protected slots:

qint64 m_curOffer;
QStringList m_curMimeTypes;
bool m_pipeIsForcedClosed = false;
};

#endif // COPYCLIENT_H

0 comments on commit a14f691

Please sign in to comment.