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/Swap.h"
10 #include "nn/nlib/UniquePtr.h"
12 #include "nn/nlib/Cstring.h"
16 
17 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
18 #undef NLIB_VIS_PUBLIC
19 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
20 #endif
21 
22 NLIB_NAMESPACE_BEGIN
23 namespace msgpack {
24 namespace jsonrpc {
25 
26 namespace fortest {
27 class JsonRpcErrorRequestTest;
28 } // namespace fortest
29 
30 typedef uint32_t reqid_t;
31 
33  public:
34  // NOTE:
35  // If m_Id is 0, the request becomes notification(ID is not sent.).
36  JsonRpcRequest() : m_Id(0) {}
37  reqid_t GetId() const { return m_Id; }
38  const char* GetMethod() const { return m_Method.get(); }
39  const MpObject& GetParams() const { return m_Params; }
40  MpObject& GetParams() { return m_Params; }
41 
42  void SetId(reqid_t id) { m_Id = id; }
43  NLIB_VIS_PUBLIC bool SetMethod(const char* method);
44  void MoveParamsFrom(MpObject& params) { // NOLINT
45  MpObject tmp;
46  tmp.swap(params);
47  tmp.swap(m_Params);
48  }
49 
50  private:
51  NLIB_VIS_HIDDEN JsonRpcResponse& GetResponse() { return m_Response; }
52  NLIB_VIS_HIDDEN bool SetMethodRaw(const void* p, size_t n);
53  NLIB_VIS_HIDDEN errno_t Init(MpObject* rhs);
54  NLIB_VIS_HIDDEN errno_t Init(MpWalker* walker);
55 
56  private:
57  reqid_t m_Id;
58  UniquePtr<char[]> m_Method;
59  MpObject m_Params;
60  JsonRpcResponse m_Response;
61 
62  friend NLIB_VIS_PUBLIC size_t JsonRpcServerExec(const void* p, size_t n,
64  friend class JsonRpcRequestReader;
65  friend class fortest::JsonRpcErrorRequestTest;
66  NLIB_DISALLOW_COPY_AND_ASSIGN(JsonRpcRequest);
67 };
68 
70  public:
71  JsonRpcRequestWriter() : m_IsMsgpack(false), m_Cur(0), m_Count(0) {}
72  explicit JsonRpcRequestWriter(bool use_msgpack)
73  : m_IsMsgpack(use_msgpack), m_Cur(0), m_Count(0) {}
74  NLIB_VIS_PUBLIC errno_t BeginWriteRequest(uint32_t n);
75  NLIB_VIS_PUBLIC errno_t WriteRequest(const char* method, reqid_t id, const MpObject& params);
76  errno_t WriteNotification(const char* method, const MpObject& params) {
77  return WriteRequest(method, 0, params);
78  }
80  return WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
81  }
82  NLIB_VIS_PUBLIC errno_t EndWriteRequest(ReallocOutputStream::UniquePtrType* ptr, size_t* n);
83 
84  private:
85  NLIB_VIS_HIDDEN errno_t WriteJsonRequest(const char* method, reqid_t id,
86  const MpObject& params);
87  NLIB_VIS_HIDDEN errno_t WriteMsgpackRequest(const char* method, reqid_t id,
88  const MpObject& params);
89 
90  private:
91  bool m_IsMsgpack;
92  size_t m_Cur;
93  size_t m_Count;
94  ReallocOutputStream m_Ostr;
96 };
97 
99  public:
101  static errno_t ReadRequest(const void* p, size_t n, ListType* request_list);
102 
103  private:
104  NLIB_VIS_HIDDEN static errno_t ReadReqJson(const char* p, size_t n,
105  ListType* request_list);
106  NLIB_VIS_HIDDEN static errno_t ReadReqMsgpack(const uint8_t* p, size_t n,
107  ListType* request_list);
108 
109  private:
111 };
112 
113 } // namespace jsonrpc
114 } // namespace msgpack
115 NLIB_NAMESPACE_END
116 
117 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
118 #undef NLIB_VIS_PUBLIC
119 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
120 #endif
121 
122 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
Class for representing the JSON-RPC response.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#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:126
Class to serialize and write the JSON-RPC request to memory.
The class for output streams written to memory expanded using nlib_realloc or another realloc functio...
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
Defines that class that is corresponding to std::unique_ptr.
Object created when MessagePack or JSON is read.
Definition: MpObject.h:83
reqid_t GetId() const
Get the id of the request. If 0, the request is a notification.
const char * GetMethod() const
Gets the method name of the request.
void swap(MpObject &rhs) noexcept
Swaps the content of the object.
Definition: MpObject.h:219
Class that reads the JSON-RPC request.
Class for representing a JSON-RPC request.
void MoveParamsFrom(MpObject &params)
Sets the JSON-RPC parameters.
Object created when MessagePack, JSON, or CSV is read.
errno_t WriteNotification(const char *method, const MpObject &params)
Writes the JSON-RPC notification.
JsonRpcRequestWriter(bool use_msgpack)
Sets the use of msgpack instead of JSON as the JSON description method.
Nlist< JsonRpcRequest > ListType
Type used to store the JSON-RPC request sequence restored from the byte string.
A file that contains the configuration information for each development environment.
const MpObject & GetParams() const
Gets the parameter of the request.
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
Wraps functions like strlen and strcpy so they can be safely used.
MpObject & GetParams()
Gets the parameter of the request.
Quickly accesses MessagePack expanded in memory.
Definition: MpWalker.h:17
JsonRpcRequestWriter()
Instantiates the object with default parameters (default constructor).
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
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.
errno_t WriteRequest(const JsonRpcRequest &req)
Writes the JSON-RPC request.
void SetId(reqid_t id)
Sets the request ID.
Defines the class for reading MessagePack at high speed.
JsonRpcRequest()
Instantiates the object with default parameters (default constructor).
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24