Class to serialize and write the JSON-RPC request to memory.
More...
#include "nn/nlib/msgpack/jsonrpc/JsonRpcRequest.h"
Class to serialize and write the JSON-RPC request to memory.
- Description
- This class is used by the client side to create the byte string that becomes the JSON-RPC request.
Serializes and converts the JSON-RPC request created by JsonRpcRequest
into a byte string that can be sent to the server.
The actual data transmission (send and receive) must be written by the user.
- Both individual and batch requests can be created.
- Code to send an individual request is written as follows.
SUCCEED_IF(w.BeginWriteRequest(1) == 0);
JsonRpcRequest req;
req.SetId(reqid);
req.SetMethod("subtract");
auto params = ToMpObject(R"([42,23])");
req.MoveParamsFrom(std::move(*params));
w.WriteRequest(req);
auto result = w.EndWriteRequest();
SUCCEED_IF(std::get<0>(result) == 0);
size_t bufsize = std::get<2>(result);
memcpy(tmp, std::get<1>(result).get(), bufsize);
tmp[bufsize] = '\0';
- To send a batch request, set the number of requests in
BeginWriteRequest
and execute multiple WriteRequest
.
Definition at line 84 of file JsonRpcRequest.h.
◆ JsonRpcRequestWriter()
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::JsonRpcRequestWriter |
( |
bool |
use_msgpack | ) |
|
|
inlineexplicitnoexcept |
Sets the use of msgpack
instead of JSON as the JSON description method.
- Parameters
-
[in] | use_msgpack | If true , msgpack is used. |
- Description
- The nlib JSON-RPC server can handle
msgpack
requests. In this case, the response is sent to the client in the msgpack
format.
Definition at line 112 of file JsonRpcRequest.h.
◆ BeginWriteRequest()
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::BeginWriteRequest |
( |
uint32_t |
n | ) |
|
|
noexcept |
Call once before writing the JSON-RPC request.
- Parameters
-
[in] | n | Number of requests to write. (n >= 2 for a batch request.) |
- Return values
-
0 | Success. |
ENOMEM | memory allocation failed. |
◆ EndWriteRequest() [1/2]
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::EndWriteRequest |
( |
UniquePtrType * |
ptr, |
|
|
size_t * |
n |
|
) |
| |
|
noexcept |
Finishes writing the JSON-RPC request.
- Parameters
-
[out] | ptr | Pointer to the byte string. |
[out] | n | Pointer where the byte count of the data is written to. |
- Return values
-
0 | Success. |
EINVAL | ptr or n is NULL . |
ERANGE | The request count does not match the number specified in BeginWriteRequest . |
ENOMEM | memory allocation failed. |
- Description
- When successful, n byte data is set to the ptr parameter. The client side JSON-RPC call is completed by sending this data to the server.
◆ EndWriteRequest() [2/2]
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::EndWriteRequest |
( |
| ) |
|
|
noexcept |
Completes writing a JSON-RPC request and returns a tupple of the error value, byte string, and the byte string size.
- Return values
-
0,buf,bufsize | Success. |
ERANGE,nullptr,0 | The request count does not match the number specified in BeginWriteRequest . |
ENOMEM,nullptr,0 | Indicates memory allocation failed. |
- Description
- The client side JSON-RPC call is completed by sending the returned byte string to the server. Only if the error value is 0, the byte string and the byte string size in the tupple are valid.
◆ WriteNotification()
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::WriteNotification |
( |
const char * |
method, |
|
|
const MpObject & |
params |
|
) |
| |
|
inlinenoexcept |
Writes the JSON-RPC notification.
- Parameters
-
[in] | method | Method name. |
[in] | params | Parameters. |
- Returns
- Returns
0
on success.
- Description
- This is equivalent to the code below.
Definition at line 120 of file JsonRpcRequest.h.
◆ WriteRequest() [1/2]
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::WriteRequest |
( |
const char * |
method, |
|
|
reqid_t |
id, |
|
|
const MpObject & |
params |
|
) |
| |
|
noexcept |
Writes the JSON-RPC request.
- Parameters
-
[in] | method | Method name. |
[in] | id | Request ID. |
[in] | params | Parameters. |
- Return values
-
0 | Success. |
EINVAL | method is NULL or an empty character string. |
ERANGE | The request count is larger than the number specified in BeginWriteRequest . |
ENOMEM | memory allocation failed. |
An | error occurred. Another JSON/msgpack write operation failed. |
◆ WriteRequest() [2/2]
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::WriteRequest |
( |
const JsonRpcRequest & |
req | ) |
|
|
inlinenoexcept |
Writes the JSON-RPC request.
- Parameters
-
- Returns
- Returns
0
on success.
- Description
- This is equivalent to the code below.
this->
WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
Definition at line 127 of file JsonRpcRequest.h.
The documentation for this class was generated from the following files: