2e67f95017
The rationale is explained in https://planet.kde.org/friedrich-kossebau-2023-06-28-include-also-moc-files-of-headers/ In case of KDEConnect, it impressively speeds up compilation. Before it took 390 seconds on a clean build and with this change it took 330 seconds. This is due to the mocs_compilation having to include the header files and thus all their headers. Due to the lots of small plugins we have, this means that the same headers must be compiled plenty of times. When we include the moc files directly in the C++ file, they are already available.
103 lines
2.8 KiB
C++
103 lines
2.8 KiB
C++
/**
|
|
* SPDX-FileCopyrightText: 2019 Matthijs Tijink <matthijstijink@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
*/
|
|
|
|
#include "multiplexchannel.h"
|
|
#include "core_debug.h"
|
|
#include "multiplexchannelstate.h"
|
|
|
|
MultiplexChannel::MultiplexChannel(QSharedPointer<MultiplexChannelState> state)
|
|
: state{state}
|
|
{
|
|
QIODevice::open(QIODevice::ReadWrite);
|
|
|
|
connect(this, &QIODevice::aboutToClose, state.data(), &MultiplexChannelState::requestClose);
|
|
connect(state.data(), &MultiplexChannelState::readyRead, this, &QIODevice::readyRead);
|
|
connect(state.data(), &MultiplexChannelState::bytesWritten, this, &QIODevice::bytesWritten);
|
|
connect(state.data(), &MultiplexChannelState::disconnected, this, &MultiplexChannel::disconnect);
|
|
}
|
|
|
|
MultiplexChannel::~MultiplexChannel()
|
|
{
|
|
}
|
|
|
|
bool MultiplexChannel::atEnd() const
|
|
{
|
|
return !isOpen() || (!state->connected && state->read_buffer.isEmpty());
|
|
}
|
|
|
|
void MultiplexChannel::disconnect()
|
|
{
|
|
state->connected = false;
|
|
setOpenMode(QIODevice::ReadOnly);
|
|
Q_EMIT state->readyRead();
|
|
Q_EMIT state->requestClose();
|
|
if (state->read_buffer.isEmpty()) {
|
|
close();
|
|
}
|
|
}
|
|
|
|
qint64 MultiplexChannel::bytesAvailable() const
|
|
{
|
|
return state->read_buffer.size() + QIODevice::bytesAvailable();
|
|
}
|
|
|
|
qint64 MultiplexChannel::bytesToWrite() const
|
|
{
|
|
return state->write_buffer.size() + QIODevice::bytesToWrite();
|
|
}
|
|
|
|
qint64 MultiplexChannel::readData(char *data, qint64 maxlen)
|
|
{
|
|
if (maxlen <= state->read_buffer.size()) {
|
|
for (int i = 0; i < maxlen; ++i) {
|
|
data[i] = state->read_buffer[i];
|
|
}
|
|
state->read_buffer.remove(0, maxlen);
|
|
Q_EMIT state->readAvailable();
|
|
if (!state->connected && state->read_buffer.isEmpty()) {
|
|
close();
|
|
}
|
|
return maxlen;
|
|
} else if (state->read_buffer.size() > 0) {
|
|
auto num_to_read = state->read_buffer.size();
|
|
for (int i = 0; i < num_to_read; ++i) {
|
|
data[i] = state->read_buffer[i];
|
|
}
|
|
state->read_buffer.remove(0, num_to_read);
|
|
Q_EMIT state->readAvailable();
|
|
if (!state->connected && state->read_buffer.isEmpty()) {
|
|
close();
|
|
}
|
|
return num_to_read;
|
|
} else if (isOpen() && state->connected) {
|
|
if (state->requestedReadAmount < BUFFER_SIZE) {
|
|
Q_EMIT state->readAvailable();
|
|
}
|
|
return 0;
|
|
} else {
|
|
close();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
qint64 MultiplexChannel::writeData(const char *data, qint64 len)
|
|
{
|
|
state->write_buffer.append(data, len);
|
|
Q_EMIT state->writeAvailable();
|
|
return len;
|
|
}
|
|
|
|
bool MultiplexChannel::canReadLine() const
|
|
{
|
|
return isReadable() && (QIODevice::canReadLine() || state->read_buffer.contains('\n'));
|
|
}
|
|
|
|
bool MultiplexChannel::isSequential() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
#include "moc_multiplexchannel.cpp"
|