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 | * @private |
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 | * @see connectToRemote, listenForRemote |
50 | */ |
51 | explicit Connection(Type type, QObject *parent = nullptr); |
52 | ~Connection() override; |
53 | |
54 | /** |
55 | * Connects to the remote address. |
56 | * @param address a local:// or tcp:// URL. |
57 | */ |
58 | void connectToRemote(const QUrl &address); |
59 | |
60 | /// Closes the connection. |
61 | void close(); |
62 | |
63 | QString errorString() const; |
64 | |
65 | bool isConnected() const; |
66 | |
67 | /** |
68 | * Checks whether the connection has been initialized. |
69 | * @return true if the initialized |
70 | * @see init() |
71 | */ |
72 | bool inited() const; |
73 | |
74 | /** |
75 | * Sends/queues the given command to be sent. |
76 | * @param cmd the command to set |
77 | * @param arr the bytes to send |
78 | * @return true if successful, false otherwise |
79 | */ |
80 | bool send(int cmd, const QByteArray &arr = QByteArray()); |
81 | |
82 | /** |
83 | * Sends the given command immediately. |
84 | * @param _cmd the command to set |
85 | * @param data the bytes to send |
86 | * @return true if successful, false otherwise |
87 | */ |
88 | bool sendnow(int _cmd, const QByteArray &data); |
89 | |
90 | /** |
91 | * Returns true if there are packets to be read immediately, |
92 | * false if waitForIncomingTask must be called before more data |
93 | * is available. |
94 | */ |
95 | bool hasTaskAvailable() const; |
96 | |
97 | /** |
98 | * Waits for one more command to be handled and ready. |
99 | * |
100 | * @param ms the time to wait in milliseconds |
101 | * @returns true if one command can be read, false if we timed out |
102 | */ |
103 | bool waitForIncomingTask(int ms = 30000); |
104 | |
105 | /** |
106 | * Receive data. |
107 | * |
108 | * @param _cmd the received command will be written here |
109 | * @param data the received data will be written here |
110 | |
111 | * @return >=0 indicates the received data size upon success |
112 | * -1 indicates error |
113 | */ |
114 | int read(int *_cmd, QByteArray &data); |
115 | |
116 | /** |
117 | * Don't handle incoming data until resumed. |
118 | */ |
119 | void suspend(); |
120 | |
121 | /** |
122 | * Resume handling of incoming data. |
123 | */ |
124 | void resume(); |
125 | |
126 | /** |
127 | * Returns status of connection. |
128 | * @return true if suspended, false otherwise |
129 | */ |
130 | bool suspended() const; |
131 | |
132 | void setReadMode(ReadMode mode); |
133 | |
134 | Q_SIGNALS: |
135 | void readyRead(); |
136 | |
137 | private: |
138 | friend class ConnectionPrivate; |
139 | friend class ConnectionServer; |
140 | std::unique_ptr<class ConnectionPrivate> const d; |
141 | Type m_type; |
142 | }; |
143 | |
144 | // Separated from Connection only for historical reasons - they are both private now |
145 | class ConnectionPrivate |
146 | { |
147 | public: |
148 | inline ConnectionPrivate() |
149 | : backend(nullptr) |
150 | , q(nullptr) |
151 | , suspended(false) |
152 | , readMode(Connection::ReadMode::EventDriven) |
153 | { |
154 | } |
155 | |
156 | void dequeue(); |
157 | void commandReceived(const Task &task); |
158 | void disconnected(); |
159 | void setBackend(ConnectionBackend *b); |
160 | |
161 | QList<Task> outgoingTasks; |
162 | QList<Task> incomingTasks; |
163 | ConnectionBackend *backend; |
164 | Connection *q; |
165 | bool suspended; |
166 | Connection::ReadMode readMode; |
167 | }; |
168 | |
169 | class ConnectionServerPrivate; |
170 | } |
171 | |
172 | #endif |
173 | |