nlib
nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter Class Referencefinal

Class to serialize and write the JSON-RPC request to memory. More...

#include "nn/nlib/msgpack/jsonrpc/JsonRpcRequest.h"

Public Member Functions

 JsonRpcRequestWriter () noexcept
 Instantiates the object with default parameters (default constructor).
 
 JsonRpcRequestWriter (bool use_msgpack) noexcept
 Sets the use of msgpack instead of JSON as the JSON description method. More...
 
errno_t BeginWriteRequest (uint32_t n) noexcept
 Call once before writing the JSON-RPC request. More...
 
errno_t WriteRequest (const char *method, reqid_t id, const MpObject &params) noexcept
 Writes the JSON-RPC request. More...
 
errno_t WriteRequest (const char *method, reqid_t id) noexcept
 Writes a JSON-RPC request that specifies no parameters.
 
errno_t WriteNotification (const char *method, const MpObject &params) noexcept
 Writes the JSON-RPC notification. More...
 
errno_t WriteNotification (const char *method) noexcept
 Writes a JSON-RPC Notification that specifies no parameters.
 
errno_t WriteRequest (const JsonRpcRequest &req) noexcept
 Writes the JSON-RPC request. More...
 
errno_t EndWriteRequest (ReallocOutputStream::UniquePtrType *ptr, size_t *n) noexcept
 Finishes writing the JSON-RPC request. More...
 

Detailed Description

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.
JsonRpcRequest& req = ...;
e = w.BeginWriteRequest(1);
if (nlib_is_error(e)) { Error; }
e = w.WriteRequest(req);
if (nlib_is_error(e)) { Error; }
size_t nBytes;
e = w.EndWriteRequest(&reqBytes, &nBytes);
if (nlib_is_error(e)) { Error; }
// Add code to actually send the content of reqBytes here.
Code to send a batch request is written as follows.
Seq<JsonRpcRequest>& seq = ...;
e = w.BeginWriteRequest(seq.size());
if (nlib_is_error(e)) { Error; }
foreach(item in seq) {
e = w.WriteRequest(item);
if (nlib_is_error(e)) { Error; }
}
size_t nBytes;
e = w.EndWriteRequest(&reqBytes, &nBytes);
if (nlib_is_error(e)) { Error; }
// Add code to actually send the content of reqBytes here.

Definition at line 81 of file JsonRpcRequest.h.

Constructor & Destructor Documentation

◆ 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_msgpackIf 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 86 of file JsonRpcRequest.h.

Member Function Documentation

◆ BeginWriteRequest()

nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::BeginWriteRequest ( uint32_t  n)
noexcept

Call once before writing the JSON-RPC request.

Parameters
[in]nNumber of requests to write. (n >= 2 for a batch request.)
Return values
0Success.
ENOMEMmemory allocation failed.

◆ EndWriteRequest()

nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::EndWriteRequest ( ReallocOutputStream::UniquePtrType ptr,
size_t *  n 
)
noexcept

Finishes writing the JSON-RPC request.

Parameters
[out]ptrPointer to the byte string.
[out]nPointer where the byte count of the data is written to.
Return values
0Success.
EINVALptr or n is NULL.
ERANGEThe request count does not match the number specified in BeginWriteRequest.
ENOMEMmemory 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.

◆ WriteNotification()

nn::nlib::msgpack::jsonrpc::JsonRpcRequestWriter::WriteNotification ( const char *  method,
const MpObject params 
)
inlinenoexcept

Writes the JSON-RPC notification.

Parameters
[in]methodMethod name.
[in]paramsParameters.
Returns
Returns 0 on success.
Description
This is equivalent to the code below.
this->WriteRequest(method, 0, params);

Definition at line 94 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]methodMethod name.
[in]idRequest ID.
[in]paramsParameters.
Return values
0Success.
EINVALmethod is NULL or an empty character string.
ERANGEThe request count is larger than the number specified in BeginWriteRequest.
ENOMEMmemory allocation failed.
Anerror 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
[in]reqJSON-RPC request.
Returns
Returns 0 on success.
Description
This is equivalent to the code below.
this->WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());

Definition at line 101 of file JsonRpcRequest.h.


The documentation for this class was generated from the following files: