| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org> |
| 4 | SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org> |
| 5 | SPDX-FileCopyrightText: 2024 Harald Sitter <sitter@kde.org> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #ifndef KIO_CONNECTION_P_H |
| 11 | #define KIO_CONNECTION_P_H |
| 12 | |
| 13 | #include "connectionbackend_p.h" |
| 14 | #include <QList> |
| 15 | #include <QObject> |
| 16 | #include <QString> |
| 17 | #include <QUrl> |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | namespace KIO |
| 22 | { |
| 23 | class ConnectionServer; |
| 24 | class ConnectionPrivate; |
| 25 | /*! |
| 26 | * \internal |
| 27 | * |
| 28 | * This class provides a simple means for IPC between two applications |
| 29 | * via a pipe. |
| 30 | * It handles a queue of commands to be sent which makes it possible to |
| 31 | * queue data before an actual connection has been established. |
| 32 | */ |
| 33 | class Connection : public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | public: |
| 38 | enum class ReadMode { |
| 39 | Polled, /// Any new tasks will be polled |
| 40 | EventDriven, /// We need to emit signals when we have pending events. Requires a working QEventLoop |
| 41 | }; |
| 42 | |
| 43 | enum class Type { |
| 44 | Application, /// This is the connection of the application side |
| 45 | Worker, /// This is the connection of the worker side |
| 46 | }; |
| 47 | /*! |
| 48 | * Creates a new connection. |
| 49 | * \sa connectToRemote, listenForRemote |
| 50 | */ |
| 51 | explicit Connection(Type type, QObject *parent = nullptr); |
| 52 | ~Connection() override; |
| 53 | |
| 54 | /*! |
| 55 | * Connects to the remote address. |
| 56 | * \a address a local:// or tcp:// URL. |
| 57 | */ |
| 58 | void connectToRemote(const QUrl &address); |
| 59 | |
| 60 | /// Closes the connection. |
| 61 | void close(); |
| 62 | |
| 63 | bool isConnected() const; |
| 64 | |
| 65 | /*! |
| 66 | * Checks whether the connection has been initialized. |
| 67 | * Returns true if the initialized |
| 68 | * \sa init() |
| 69 | */ |
| 70 | bool inited() const; |
| 71 | |
| 72 | /*! |
| 73 | * Sends/queues the given command to be sent. |
| 74 | * \a cmd the command to set |
| 75 | * \a arr the bytes to send |
| 76 | * Returns true if successful, false otherwise |
| 77 | */ |
| 78 | bool send(int cmd, const QByteArray &arr = QByteArray()); |
| 79 | |
| 80 | /*! |
| 81 | * Sends the given command immediately. |
| 82 | * \a _cmd the command to set |
| 83 | * \a data the bytes to send |
| 84 | * Returns true if successful, false otherwise |
| 85 | */ |
| 86 | bool sendnow(int _cmd, const QByteArray &data); |
| 87 | |
| 88 | /*! |
| 89 | * Returns true if there are packets to be read immediately, |
| 90 | * false if waitForIncomingTask must be called before more data |
| 91 | * is available. |
| 92 | */ |
| 93 | bool hasTaskAvailable() const; |
| 94 | |
| 95 | /*! |
| 96 | * Waits for one more command to be handled and ready. |
| 97 | * |
| 98 | * \a ms the time to wait in milliseconds |
| 99 | * Returns true if one command can be read, false if we timed out |
| 100 | */ |
| 101 | bool waitForIncomingTask(int ms = 30000); |
| 102 | |
| 103 | /*! |
| 104 | * Receive data. |
| 105 | * |
| 106 | * \a _cmd the received command will be written here |
| 107 | * \a data the received data will be written here |
| 108 | |
| 109 | * Returns >=0 indicates the received data size upon success |
| 110 | * -1 indicates error |
| 111 | */ |
| 112 | int read(int *_cmd, QByteArray &data); |
| 113 | |
| 114 | /*! |
| 115 | * Don't handle incoming data until resumed. |
| 116 | */ |
| 117 | void suspend(); |
| 118 | |
| 119 | /*! |
| 120 | * Resume handling of incoming data. |
| 121 | */ |
| 122 | void resume(); |
| 123 | |
| 124 | /*! |
| 125 | * Returns status of connection. |
| 126 | * Returns true if suspended, false otherwise |
| 127 | */ |
| 128 | bool suspended() const; |
| 129 | |
| 130 | void setReadMode(ReadMode mode); |
| 131 | |
| 132 | Q_SIGNALS: |
| 133 | void readyRead(); |
| 134 | |
| 135 | private: |
| 136 | friend class ConnectionPrivate; |
| 137 | friend class ConnectionServer; |
| 138 | std::unique_ptr<class ConnectionPrivate> const d; |
| 139 | Type m_type; |
| 140 | }; |
| 141 | |
| 142 | // Separated from Connection only for historical reasons - they are both private now |
| 143 | class ConnectionPrivate |
| 144 | { |
| 145 | public: |
| 146 | inline ConnectionPrivate() |
| 147 | : backend(nullptr) |
| 148 | , q(nullptr) |
| 149 | , suspended(false) |
| 150 | , readMode(Connection::ReadMode::EventDriven) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | void dequeue(); |
| 155 | void commandReceived(const Task &task); |
| 156 | void disconnected(); |
| 157 | void setBackend(ConnectionBackend *b); |
| 158 | |
| 159 | QList<Task> outgoingTasks; |
| 160 | QList<Task> incomingTasks; |
| 161 | ConnectionBackend *backend; |
| 162 | Connection *q; |
| 163 | bool suspended; |
| 164 | Connection::ReadMode readMode; |
| 165 | }; |
| 166 | |
| 167 | class ConnectionServerPrivate; |
| 168 | } |
| 169 | |
| 170 | #endif |
| 171 | |