1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qiopipe.h"
#include <QtCore/private/qobject_p.h>
#include <QtCore/qpointer.h>
#include <QDebug>
#include <memory>
QT_BEGIN_NAMESPACE
class QPipeEndPoint : public QIODevice
{
Q_OBJECT
public:
bool isSequential() const override;
qint64 bytesAvailable() const override;
void setRemoteEndPoint(QPipeEndPoint *other);
protected:
qint64 readData(char *data, qint64 maxlen) override;
qint64 writeData(const char *data, qint64 len) override;
private:
QByteArray m_buffer;
QPointer<QPipeEndPoint> m_remoteEndPoint;
};
class QIOPipePrivate final : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QIOPipe)
public:
QIOPipePrivate();
~QIOPipePrivate() override;
std::unique_ptr<QPipeEndPoint> end1;
std::unique_ptr<QPipeEndPoint> end2;
};
QIOPipe::QIOPipe(QObject *parent) :
QObject(*(new QIOPipePrivate()), parent)
{
}
bool QIOPipe::open(QIODevice::OpenMode mode)
{
Q_D(QIOPipe);
if (!d->end1->open(mode))
return false;
switch (mode & QIODevice::ReadWrite) {
case QIODevice::WriteOnly:
case QIODevice::ReadOnly:
return d->end2->open(mode ^ QIODevice::ReadWrite);
default:
return d->end2->open(mode);
}
}
QIODevice *QIOPipe::end1() const
{
Q_D(const QIOPipe);
return d->end1.get();
}
QIODevice *QIOPipe::end2() const
{
Q_D(const QIOPipe);
return d->end2.get();
}
QIOPipePrivate::QIOPipePrivate() :
end1(std::make_unique<QPipeEndPoint>()),
end2(std::make_unique<QPipeEndPoint>())
{
end1->setRemoteEndPoint(end2.get());
end2->setRemoteEndPoint(end1.get());
}
QIOPipePrivate::~QIOPipePrivate()
{
}
bool QPipeEndPoint::isSequential() const
{
return true;
}
qint64 QPipeEndPoint::bytesAvailable() const
{
return m_buffer.size() + QIODevice::bytesAvailable();
}
void QPipeEndPoint::setRemoteEndPoint(QPipeEndPoint *other)
{
m_remoteEndPoint = other;
}
qint64 QPipeEndPoint::readData(char *data, qint64 maxlen)
{
maxlen = qMin(maxlen, static_cast<qint64>(m_buffer.size()));
if (maxlen <= 0)
return 0;
Q_ASSERT(maxlen > 0);
Q_ASSERT(maxlen <= std::numeric_limits<int>::max());
memcpy(data, m_buffer.data(), static_cast<size_t>(maxlen));
m_buffer = m_buffer.mid(static_cast<int>(maxlen));
return maxlen;
}
qint64 QPipeEndPoint::writeData(const char *data, qint64 len)
{
if (!m_remoteEndPoint)
return -1;
if (len <= 0)
return 0;
QByteArray &buffer = m_remoteEndPoint->m_buffer;
const qint64 prevLen = buffer.size();
Q_ASSERT(prevLen >= 0);
len = qMin(len, std::numeric_limits<int>::max() - prevLen);
if (len == 0)
return 0;
Q_ASSERT(len > 0);
Q_ASSERT(prevLen + len > 0);
Q_ASSERT(prevLen + len <= std::numeric_limits<int>::max());
buffer.resize(static_cast<int>(prevLen + len));
memcpy(buffer.data() + prevLen, data, static_cast<size_t>(len));
emit bytesWritten(len);
emit m_remoteEndPoint->readyRead();
return len;
}
QT_END_NAMESPACE
#include <qiopipe.moc>
|