nlib
JsonRpcRequest.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
5 
6 #include <iterator>
7 
8 #include "nn/nlib/Config.h"
9 #include "nn/nlib/UniquePtr.h"
10 #include "nn/nlib/Nlist.h"
14 
15 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
16 #undef NLIB_VIS_PUBLIC
17 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
18 #endif
19 
20 NLIB_NAMESPACE_BEGIN
21 namespace msgpack {
22 namespace jsonrpc {
23 
24 namespace fortest {
25 class JsonRpcErrorRequestTest;
26 } // namespace fortest
27 
28 typedef uint32_t reqid_t;
29 
31  public:
32  // NOTE:
33  // If id_ is 0, the request becomes notification(ID is not sent.).
34  JsonRpcRequest() NLIB_NOEXCEPT : id_(0), method_(NULL) {}
36  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
37  const char* GetMethod() const NLIB_NOEXCEPT { return method_; }
38  const MpObject& GetParams() const NLIB_NOEXCEPT { return params_; }
39  MpObject& GetParams() NLIB_NOEXCEPT { return params_; }
40 
41  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; }
42  bool SetMethod(const char* method) NLIB_NOEXCEPT;
43  void MoveParamsFrom(MpObject& params) NLIB_NOEXCEPT { // NOLINT
44  MpObject tmp;
45  tmp.swap(params);
46  tmp.swap(params_);
47  }
48 
49  private:
50  NLIB_VIS_HIDDEN JsonRpcResponse& GetResponse() NLIB_NOEXCEPT { return response_; }
51  NLIB_VIS_HIDDEN bool SetMethodRaw(const void* p, size_t n) NLIB_NOEXCEPT;
53 
54  private:
55  reqid_t id_;
56  char* method_;
57  MpObject params_;
58  JsonRpcResponse response_;
59 
60  friend NLIB_VIS_PUBLIC size_t JsonRpcServerExec(const void* p, size_t n,
62  friend class JsonRpcRequestReader;
63  friend class fortest::JsonRpcErrorRequestTest;
65 };
66 
68  public:
69  JsonRpcRequestWriter() NLIB_NOEXCEPT : prv_(NULL), is_msgpack_(false) {}
71  explicit JsonRpcRequestWriter(bool use_msgpack) NLIB_NOEXCEPT
72  : prv_(NULL), is_msgpack_(use_msgpack) {}
73  errno_t BeginWriteRequest(uint32_t n) NLIB_NOEXCEPT;
74  errno_t WriteRequest(const char* method, reqid_t id, const MpObject& params) NLIB_NOEXCEPT;
75  errno_t WriteNotification(const char* method, const MpObject& params) NLIB_NOEXCEPT {
76  return WriteRequest(method, 0, params);
77  }
78  errno_t WriteRequest(const JsonRpcRequest& req) NLIB_NOEXCEPT {
79  return WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
80  }
81  errno_t EndWriteRequest(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
82 
83  private:
84  struct JsonRpcRequestWriterPrivate;
85  JsonRpcRequestWriterPrivate* prv_;
86  bool is_msgpack_;
87 
89 };
90 
92  public:
94  static errno_t ReadRequest(const void* p, size_t n, ListType* request_list) NLIB_NOEXCEPT;
95 
96  private:
98 };
99 
100 } // namespace jsonrpc
101 } // namespace msgpack
102 NLIB_NAMESPACE_END
103 
104 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
105 #undef NLIB_VIS_PUBLIC
106 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
107 #endif
108 
109 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
JsonRpcRequestWriter() noexcept
Instantiates the object with default parameters (default constructor).
Class for representing the JSON-RPC response.
JsonRpcRequest() noexcept
Instantiates the object with default parameters (default constructor).
errno_t WriteNotification(const char *method, const MpObject &params) noexcept
Writes the JSON-RPC notification.
const char * GetMethod() const noexcept
Gets the method name of the request.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
void SetId(reqid_t id) noexcept
Sets the request ID.
Class to serialize and write the JSON-RPC request to memory.
reqid_t GetId() const noexcept
Get the id of the request. If 0, the request is a notification.
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
Object created when MessagePack or JSON is read.
Definition: MpObject.h:82
JsonRpcRequestWriter(bool use_msgpack) noexcept
Sets the use of msgpack instead of JSON as the JSON description method.
Defines the class that resembles std::vector but can store objects that cannot be copied...
void swap(MpObject &rhs) noexcept
Swaps the content of the object.
Definition: MpObject.h:271
Class that reads the JSON-RPC request.
Class for representing a JSON-RPC request.
void MoveParamsFrom(MpObject &params) noexcept
Sets the JSON-RPC parameters.
Object created when MessagePack, JSON, or CSV is read.
Nlist< JsonRpcRequest > ListType
Type used to store the JSON-RPC request sequence restored from the byte string.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.
uint32_t reqid_t
id type for the JSON-RPC request.
Definition: JsonRpcClient.h:19
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:19
const MpObject & GetParams() const noexcept
Gets the parameter of the request.
errno_t WriteRequest(const JsonRpcRequest &req) noexcept
Writes the JSON-RPC request.
size_t JsonRpcServerExec(const void *p, size_t n, ReallocOutputStream::UniquePtrType *ptr)
Creates the JSON-RPC response byte string by processing the JSON-RPC request.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
MpObject & GetParams() noexcept
Gets the parameter of the request.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24