11#include "connection_p.h"
12#include "connectionbackend_p.h"
13#include "kiocoredebug.h"
20void ConnectionPrivate::dequeue()
22 if (!backend || suspended) {
26 for (
const Task &task : std::as_const(outgoingTasks)) {
29 outgoingTasks.clear();
31 if (!incomingTasks.isEmpty()) {
32 Q_EMIT q->readyRead();
36void ConnectionPrivate::commandReceived(
const Task &task)
39 if (!suspended && incomingTasks.isEmpty() && readMode == Connection::ReadMode::EventDriven) {
40 auto dequeueFunc = [
this]() {
45 incomingTasks.append(task);
48void ConnectionPrivate::disconnected()
51 if (readMode == Connection::ReadMode::EventDriven) {
56void ConnectionPrivate::setBackend(ConnectionBackend *b)
61 q->connect(backend, &ConnectionBackend::commandReceived, q, [
this](
const Task &task) {
62 commandReceived(task);
64 q->connect(backend, &ConnectionBackend::disconnected, q, [
this]() {
67 backend->setSuspended(suspended);
71Connection::Connection(Type type,
QObject *parent)
73 , d(new ConnectionPrivate)
79Connection::~Connection()
84void Connection::suspend()
89 d->backend->setSuspended(
true);
93void Connection::resume()
96 if (d->readMode == Connection::ReadMode::EventDriven) {
97 auto dequeueFunc = [
this]() {
104 d->suspended =
false;
106 d->backend->setSuspended(
false);
110void Connection::close()
113 d->backend->disconnect(
this);
114 d->backend->deleteLater();
115 d->backend =
nullptr;
117 d->outgoingTasks.clear();
118 d->incomingTasks.clear();
121bool Connection::isConnected()
const
123 return d->backend && d->backend->state == ConnectionBackend::Connected;
126bool Connection::inited()
const
131bool Connection::suspended()
const
136void Connection::connectToRemote(
const QUrl &address)
142 d->setBackend(
new ConnectionBackend(
this));
144 qCWarning(KIO_CORE) <<
"Unknown protocol requested:" << scheme <<
"(" <<
address <<
")";
150 if (!d->backend->connectToRemote(address)) {
153 d->backend =
nullptr;
160bool Connection::send(
int cmd,
const QByteArray &data)
166 if (m_type == Type::Worker && !inited()) {
167 qCWarning(KIO_CORE) <<
"Connection::send() called with connection not inited";
170 if (!inited() || !d->outgoingTasks.isEmpty()) {
174 d->outgoingTasks.
append(std::move(task));
177 return sendnow(cmd, data);
181bool Connection::sendnow(
int cmd,
const QByteArray &data)
184 qCWarning(KIO_CORE) <<
"Connection::sendnow has no backend";
188 if (data.
size() > 0xffffff) {
189 qCWarning(KIO_CORE) <<
"Connection::sendnow too much data";
193 if (!isConnected()) {
194 qCWarning(KIO_CORE) <<
"Connection::sendnow not connected";
199 return d->backend->sendCommand(cmd, data);
202bool Connection::hasTaskAvailable()
const
204 return !d->incomingTasks.
isEmpty();
207bool Connection::waitForIncomingTask(
int ms)
209 if (!isConnected()) {
214 return d->backend->waitForIncomingTask(ms);
219int Connection::read(
int *_cmd,
QByteArray &data)
222 if (d->incomingTasks.isEmpty()) {
226 const Task &
task = d->incomingTasks.constFirst();
231 d->incomingTasks.removeFirst();
234 if (!d->suspended && !d->incomingTasks.isEmpty() && d->readMode == Connection::ReadMode::EventDriven) {
235 auto dequeueFunc = [
this]() {
244void Connection::setReadMode(ReadMode readMode)
246 d->readMode = readMode;
249#include "moc_connection_p.cpp"
A namespace for KIO globals.
PostalAddress address(const QVariant &location)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QByteArray & append(QByteArrayView data)
qsizetype size() const const
QTaskBuilder< Task > task(Task &&task)