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 QJSONRPCPROTOCOL_P_P_H |
5 | #define QJSONRPCPROTOCOL_P_P_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/private/qjsonrpcprotocol_p.h> |
19 | #include <QtJsonRpc/private/qjsonrpctransport_p.h> |
20 | |
21 | #include <QtCore/qjsondocument.h> |
22 | #include <QtCore/qjsonobject.h> |
23 | |
24 | #include <unordered_map> |
25 | #include <memory> |
26 | |
27 | QT_BEGIN_NAMESPACE |
28 | |
29 | template<typename T> |
30 | struct QHasher |
31 | { |
32 | using argument_type = T; |
33 | using result_type = size_t; |
34 | result_type operator()(const argument_type &value) const { return qHash(value); } |
35 | }; |
36 | |
37 | class QJsonRpcProtocolPrivate |
38 | { |
39 | public: |
40 | template<typename K, typename V> |
41 | using Map = std::unordered_map<K, V, QHasher<K>>; |
42 | |
43 | using ResponseHandler = QJsonRpcProtocol::ResponseHandler; |
44 | using MessageHandler = QJsonRpcProtocol::MessageHandler; |
45 | using OwnedMessageHandler = std::unique_ptr<MessageHandler>; |
46 | using MessageHandlerMap = Map<QString, OwnedMessageHandler>; |
47 | using ResponseMap = Map<QJsonValue, QJsonRpcProtocol::Handler<QJsonRpcProtocol::Response>>; |
48 | |
49 | void processMessage(const QJsonDocument &message, const QJsonParseError &error); |
50 | void processError(const QString &error); |
51 | |
52 | template<typename JSON> |
53 | void sendMessage(const JSON &value) |
54 | { |
55 | m_transport->sendMessage(packet: QJsonDocument(value)); |
56 | } |
57 | |
58 | void processRequest(const QJsonObject &object); |
59 | void processResponse(const QJsonObject &object); |
60 | void processNotification(const QJsonObject &object); |
61 | |
62 | MessageHandler *messageHandler(const QString &method) const |
63 | { |
64 | auto it = m_messageHandlers.find(x: method); |
65 | return it != m_messageHandlers.end() ? it->second.get() : m_defaultHandler.get(); |
66 | } |
67 | |
68 | void setMessageHandler(const QString &method, OwnedMessageHandler handler) |
69 | { |
70 | m_messageHandlers[method] = std::move(handler); |
71 | } |
72 | |
73 | MessageHandler *defaultMessageHandler() const { return m_defaultHandler.get(); } |
74 | void setDefaultMessageHandler(OwnedMessageHandler handler) |
75 | { |
76 | m_defaultHandler = std::move(handler); |
77 | } |
78 | |
79 | bool addPendingRequest(const QJsonValue &id, |
80 | const QJsonRpcProtocol::Handler<QJsonRpcProtocol::Response> &handler) |
81 | { |
82 | auto it = m_pendingRequests.find(x: id); |
83 | if (it == m_pendingRequests.end()) { |
84 | m_pendingRequests.insert(x: std::make_pair(x: id, y: handler)); |
85 | return true; |
86 | } |
87 | return false; |
88 | } |
89 | |
90 | void setTransport(QJsonRpcTransport *newTransport); |
91 | QJsonRpcTransport *transport() const { return m_transport; } |
92 | |
93 | ResponseHandler invalidResponseHandler() const { return m_invalidResponseHandler; } |
94 | void setInvalidResponseHandler(const ResponseHandler &handler) |
95 | { |
96 | m_invalidResponseHandler = handler; |
97 | } |
98 | |
99 | ResponseHandler protocolErrorHandler() const { return m_protocolErrorHandler; } |
100 | void setProtocolErrorHandler(const ResponseHandler &handler) |
101 | { |
102 | m_protocolErrorHandler = handler; |
103 | } |
104 | |
105 | QJsonRpcProtocol::MessagePreprocessor messagePreprocessor() const |
106 | { |
107 | return m_messagePreprocessor; |
108 | } |
109 | void installMessagePreprocessor(QJsonRpcProtocol::MessagePreprocessor preHandler) |
110 | { |
111 | m_messagePreprocessor = preHandler; |
112 | } |
113 | |
114 | private: |
115 | ResponseMap m_pendingRequests; |
116 | MessageHandlerMap m_messageHandlers; |
117 | |
118 | OwnedMessageHandler m_defaultHandler; |
119 | |
120 | QJsonRpcTransport *m_transport; |
121 | |
122 | ResponseHandler m_protocolErrorHandler; |
123 | ResponseHandler m_invalidResponseHandler; |
124 | QJsonRpcProtocol::MessagePreprocessor m_messagePreprocessor; |
125 | }; |
126 | |
127 | class QJsonRpcProtocol::BatchPrivate |
128 | { |
129 | public: |
130 | struct Item |
131 | { |
132 | QJsonValue id = QJsonValue::Undefined; |
133 | QString method; |
134 | QJsonValue params = QJsonValue::Undefined; |
135 | }; |
136 | |
137 | std::vector<Item> m_items; |
138 | }; |
139 | |
140 | QT_END_NAMESPACE |
141 | |
142 | #endif // QJSONRPCPROTOCOL_P_P_H |
143 | |