Skip to content

Commit 33090a8

Browse files
committed
chore: move readpipedatatask logic to waylandcopyclient
move the logic to waylandcopyclient, seems it will reduced when the signal cannot received by task Log:
1 parent f877f4c commit 33090a8

File tree

4 files changed

+92
-156
lines changed

4 files changed

+92
-156
lines changed

dde-clipboard-daemon/readpipedatatask.cpp

-102
This file was deleted.

dde-clipboard-daemon/readpipedatatask.h

-49
This file was deleted.

dde-clipboard-daemon/waylandcopyclient.cpp

+87-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

55
#include "waylandcopyclient.h"
6-
#include "readpipedatatask.h"
76

87
#include <QEventLoop>
98
#include <QMimeData>
@@ -22,6 +21,11 @@
2221
#include <DWayland/Client/datacontroloffer.h>
2322

2423
#include <unistd.h>
24+
#include <mutex>
25+
26+
static std::mutex PIPELINE_GUARD;
27+
28+
constexpr int BYTE_MAX = 1024 * 4;
2529

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

@@ -140,6 +144,7 @@ WaylandCopyClient::WaylandCopyClient(QObject *parent)
140144
, m_mimeData(new DMimeData())
141145
, m_seat(nullptr)
142146
, m_curOffer(0)
147+
, m_pipeIsForcedClosed(false)
143148
{
144149

145150
}
@@ -167,6 +172,7 @@ void WaylandCopyClient::init()
167172
m_connectionThread->start();
168173
m_connectionThreadObject->initConnection();
169174
connect(this, &WaylandCopyClient::dataCopied, this, &WaylandCopyClient::onDataCopied);
175+
connect(this, &WaylandCopyClient::dataReady, this, &WaylandCopyClient::taskDataReady);
170176
}
171177

172178
void WaylandCopyClient::setupRegistry(Registry *registry)
@@ -217,13 +223,89 @@ void WaylandCopyClient::onDataOffered(KWayland::Client::DataControlOfferV1* offe
217223
// NOTE: no thread, this should be block
218224
void WaylandCopyClient::execTask(const QStringList &mimeTypes, DataControlOfferV1 *offer)
219225
{
226+
m_pipeIsForcedClosed = false;
220227
for (const QString &mimeType : mimeTypes) {
221-
ReadPipeDataTask *task = new ReadPipeDataTask(m_connectionThreadObject, offer, mimeType, this);
222-
connect(this, &WaylandCopyClient::dataOfferedNew, task, &ReadPipeDataTask::forceClosePipeLine);
223-
connect(task, &ReadPipeDataTask::dataReady, this, &WaylandCopyClient::taskDataReady);
228+
onDataOfferedTask(offer, mimeType);
229+
}
230+
}
224231

225-
task->run();
232+
void WaylandCopyClient::onDataOfferedTask(DataControlOfferV1 *offer, const QString &mimeType)
233+
{
234+
if (!m_connectionThreadObject || !offer || mimeType.isEmpty()) {
235+
return;
226236
}
237+
int pipeFds[2];
238+
239+
if (pipe(pipeFds) != 0) {
240+
qWarning() << "Create pipe failed.";
241+
242+
// 避免返回数据量少
243+
Q_EMIT dataReady((qint64)offer, mimeType, QByteArray());
244+
return;
245+
}
246+
247+
// 根据mime类取数据,写入pipe中
248+
offer->receive(mimeType, pipeFds[1]);
249+
m_connectionThreadObject->roundtrip();
250+
close(pipeFds[1]);
251+
252+
// force to close the piepline
253+
connect(this, &WaylandCopyClient::dataOfferedNew, this, [pipeFds, this] {
254+
std::lock_guard<std::mutex> guard(PIPELINE_GUARD);
255+
m_pipeIsForcedClosed = true;
256+
close(pipeFds[0]);
257+
});
258+
259+
qint64 offerId = (qint64)offer;
260+
261+
// FIXME: in QtConcurrent run , when signal is emit, sometimes it cannot be recepted
262+
// So sometimes copy will not succcessed
263+
QtConcurrent::run([this, pipeFds, mimeType, offerId] {
264+
QByteArray data;
265+
bool is_successed = readData(pipeFds[0], data);
266+
std::lock_guard<std::mutex> guard(PIPELINE_GUARD);
267+
if (m_pipeIsForcedClosed) {
268+
qDebug() << "pipeline is block here;" << "mimetype is: " << mimeType;
269+
m_pipeIsForcedClosed = false;
270+
return;
271+
}
272+
273+
if (!is_successed) {
274+
qDebug() << "Cannot open pipeline";
275+
return;
276+
}
277+
278+
Q_EMIT dataReady(offerId, mimeType, data);
279+
m_pipeIsForcedClosed = false;
280+
close(pipeFds[0]);
281+
});
282+
283+
}
284+
285+
bool WaylandCopyClient::readData(int fd, QByteArray &data)
286+
{
287+
QFile readPipe;
288+
if (!readPipe.open(fd, QIODevice::ReadOnly)) {
289+
return false;
290+
}
291+
292+
if (!readPipe.isReadable()) {
293+
qWarning() << "Pipe is not readable";
294+
readPipe.close();
295+
return false;
296+
}
297+
298+
int retCount = BYTE_MAX;
299+
do {
300+
QByteArray bytes = readPipe.read(BYTE_MAX);
301+
retCount = bytes.count();
302+
if (!bytes.isEmpty())
303+
data.append(bytes);
304+
} while (retCount == BYTE_MAX);
305+
306+
readPipe.close();
307+
308+
return true;
227309
}
228310

229311
void WaylandCopyClient::taskDataReady(qint64 offer, const QString &mimeType, const QByteArray &data)

dde-clipboard-daemon/waylandcopyclient.h

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class WaylandCopyClient : public QObject
6363
void dataCopied();
6464
// when a new dataOffer request in ,then send it
6565
void dataOfferedNew();
66+
void dataReady(qint64, const QString &, const QByteArray &);
6667

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

77+
void onDataOfferedTask(DataControlOfferV1 *offer, const QString &mimeType);
78+
bool readData(int fd, QByteArray &data);
79+
7680
private:
7781
QThread *m_connectionThread;
7882
ConnectionThread *m_connectionThreadObject;
@@ -85,6 +89,7 @@ protected slots:
8589

8690
qint64 m_curOffer;
8791
QStringList m_curMimeTypes;
92+
bool m_pipeIsForcedClosed;
8893
};
8994

9095
#endif // COPYCLIENT_H

0 commit comments

Comments
 (0)