1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QJSONRPCTRANSPORT_H |
5 | #define QJSONRPCTRANSPORT_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtJsonRpc/qtjsonrpcglobal.h> |
19 | #include <QtCore/qjsondocument.h> |
20 | #include <functional> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | class Q_JSONRPC_EXPORT QJsonRpcTransport |
25 | { |
26 | Q_DISABLE_COPY_MOVE(QJsonRpcTransport) |
27 | public: |
28 | enum DiagnosticLevel { Warning, Error }; |
29 | |
30 | using MessageHandler = std::function<void(const QJsonDocument &, const QJsonParseError &)>; |
31 | using DataHandler = std::function<void(const QByteArray &)>; |
32 | using DiagnosticHandler = std::function<void(DiagnosticLevel, const QString &)>; |
33 | |
34 | QJsonRpcTransport() = default; |
35 | virtual ~QJsonRpcTransport() = default; |
36 | |
37 | // Parse data and call messageHandler for any messages found in it. |
38 | virtual void receiveData(const QByteArray &data) = 0; |
39 | |
40 | // serialize the message and call dataHandler for the resulting data. |
41 | // Needs to be guarded by a mutex if called by different threads |
42 | virtual void sendMessage(const QJsonDocument &packet) = 0; |
43 | |
44 | void setMessageHandler(const MessageHandler &handler) { m_messageHandler = handler; } |
45 | MessageHandler messageHandler() const { return m_messageHandler; } |
46 | |
47 | void setDataHandler(const DataHandler &handler) { m_dataHandler = handler; } |
48 | DataHandler dataHandler() const { return m_dataHandler; } |
49 | |
50 | void setDiagnosticHandler(const DiagnosticHandler &handler) { m_diagnosticHandler = handler; } |
51 | DiagnosticHandler diagnosticHandler() const { return m_diagnosticHandler; } |
52 | |
53 | private: |
54 | MessageHandler m_messageHandler; |
55 | DataHandler m_dataHandler; |
56 | DiagnosticHandler m_diagnosticHandler; |
57 | }; |
58 | |
59 | QT_END_NAMESPACE |
60 | |
61 | #endif // QJSONRPCTRANSPORT_H |
62 |