Client.h
1/*
2 * Copyright (C) 2020-2026 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * MA 02110-1301 USA
28 */
29
30#ifndef PT_JSONRPC_CLIENT_H
31#define PT_JSONRPC_CLIENT_H
32
33#include <Pt/JsonRpc/Api.h>
34#include <Pt/JsonRpc/Fault.h>
35#include <Pt/JsonRpc/Formatter.h>
36#include <Pt/Remoting/Client.h>
37#include <Pt/Json/JsonReader.h>
38#include <Pt/Json/JsonWriter.h>
39#include <Pt/Composer.h>
40#include <Pt/Decomposer.h>
41#include <Pt/Utf8Codec.h>
42#include <Pt/TextStream.h>
43#include <Pt/NonCopyable.h>
44#include <Pt/Types.h>
45#include <string>
46
47namespace Pt {
48
49namespace JsonRpc {
50
52
55class PT_JSONRPC_API Client : public Remoting::Client
56{
57 public:
61
64 virtual ~Client();
65
68 bool isFailed() const;
69
76
77 protected:
78 virtual void onBeginCall(Composer& r, Remoting::RemoteCall& method, Decomposer** argv, unsigned argc);
79
80 virtual void onEndCall();
81
82 virtual void onCall(Composer& r, Remoting::RemoteCall& method, Decomposer** argv, unsigned argc);
83
84 virtual void onCancel();
85
90 virtual void onBeginInvoke() = 0;
91
94 virtual void onEndInvoke() = 0;
95
101 virtual void onInvoke() = 0;
102
103 protected:
106 void beginMessage(std::ostream& os);
107
113
117
120 void beginResult(std::istream& is);
121
127
130 void processResult(std::istream& is);
131
134 void setFault(int rc, const char* msg);
135
136 private:
138 bool advance(const Json::Node& node);
139
140 private:
141 enum State
142 {
143 OnBegin,
144 OnResponseObject,
145 OnResult,
146 OnError,
147 OnErrorObject,
148 OnErrorCode,
149 OnErrorMessage,
150 OnId,
151 OnEnd
152 };
153
154 const ProcedureDeclaration* _procedure;
155
156 Composer* _r;
157 Decomposer** _argv;
158 unsigned _argc;
159 Decomposer* _arg;
160 unsigned _argn;
161
162 Utf8Codec _utf8;
163 TextOStream _tos;
164 Json::JsonWriter _writer;
165 Formatter _formatter;
166
167 TextIStream _tis;
168 Json::JsonReader _reader;
169 State _state;
170
171 Fault _fault;
172 int _faultCode;
173 std::string _faultMessage;
174 bool _isFault;
175
176 long long _id;
177 long long _nextId;
178
179 Pt::varint_t _r1;
180 Pt::varint_t _r2;
181};
182
183} // namespace JsonRpc
184
185} // namespace Pt
186
187#endif // PT_JSONRPC_CLIENT_H
Composes types during serialization.
Definition Composer.h:101
Manages the decomposition of types during serialization.
Definition Decomposer.h:121
virtual void onBeginInvoke()=0
Begin an asynchronous invocation.
virtual void onEndInvoke()=0
End an asynchronous invocation.
void beginResult(std::istream &is)
Begin parsing a JSON-RPC response from a stream.
virtual void onCancel()
Cancels the remote procedure call.
void finishMessage()
Finish formatting the request.
void processResult(std::istream &is)
Parse the entire response synchronously.
bool advanceMessage()
Continue formatting the request.
void setProcedure(const ProcedureDeclaration *decl)
Set procedure declaration for named parameters.
virtual ~Client()
Destructor.
Client()
Constructor.
void setFault(int rc, const char *msg)
Mark the current procedure as failed.
virtual void onInvoke()=0
Perform a synchronous invocation.
bool parseResult()
Parse available response data.
bool isFailed() const
Indicates if the procedure has failed.
void beginMessage(std::ostream &os)
Begin formatting a JSON-RPC request to a stream.
JSON-RPC fault exception.
Definition Fault.h:44
Formatter for JSON-RPC message serialization and deserialization.
Definition Formatter.h:57
Maps parameter names to positional indices for named parameters.
Definition ProcedureDeclaration.h:49
Reads JSON as a Stream of Nodes.
Definition JsonReader.h:51
Writes JSON to a text stream.
Definition JsonWriter.h:46
JSON document node.
Definition Node.h:52
A client for remote procedure calls.
Definition Client.h:48
Convert between unicode and UTF-8.
Definition Utf8Codec.h:44
JSON-RPC 2.0 services and clients.
Core module.
Definition Allocator.h:33