nlib
JsonRpcRequest.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
17 #define INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
18 
19 #include <iterator>
20 #include <utility>
21 
22 #include "nn/nlib/Config.h"
23 #include "nn/nlib/UniquePtr.h"
24 #include "nn/nlib/Nlist.h"
25 #include "nn/nlib/Swap.h"
29 #ifdef __cpp_rvalue_references
30 #include <tuple>
31 #endif
32 
33 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
34 #undef NLIB_VIS_PUBLIC
35 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
36 #endif
37 
38 NLIB_NAMESPACE_BEGIN
39 namespace msgpack {
40 namespace jsonrpc {
41 
42 namespace fortest {
43 class JsonRpcErrorRequestTest;
44 } // namespace fortest
45 
46 typedef uint32_t reqid_t;
47 
49  public:
50  // NOTE:
51  // If id_ is 0, the request becomes notification(ID is not sent.).
52  JsonRpcRequest() NLIB_NOEXCEPT : id_(0), method_(nullptr) {}
54  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
55  const char* GetMethod() const NLIB_NOEXCEPT { return method_; }
56  const MpObject& GetParams() const NLIB_NOEXCEPT { return params_; }
57  MpObject& GetParams() NLIB_NOEXCEPT { return params_; }
58 
59  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; }
60  bool SetMethod(const char* method) NLIB_NOEXCEPT;
61  void MoveParamsFrom(MpObject& params) NLIB_NOEXCEPT { params_.assign(params, move_tag()); }
62 #ifdef __cpp_rvalue_references
63  void MoveParamsFrom(MpObject&& params) NLIB_NOEXCEPT { params_ = std::move(params); }
64 #endif
65 
66  private:
67  NLIB_VIS_HIDDEN JsonRpcResponse& GetResponse() NLIB_NOEXCEPT { return response_; }
68  NLIB_VIS_HIDDEN bool SetMethodRaw(const void* p, size_t n) NLIB_NOEXCEPT;
70 
71  private:
72  reqid_t id_;
73  char* method_;
74  MpObject params_;
75  JsonRpcResponse response_;
76 
77  friend NLIB_VIS_PUBLIC size_t JsonRpcServerExec(const void* p, size_t n,
79  friend class JsonRpcRequestReader;
80  friend class fortest::JsonRpcErrorRequestTest;
81  NLIB_DISALLOW_COPY_AND_ASSIGN(JsonRpcRequest);
82 };
83 
85  public:
87  NLIB_CEXPR JsonRpcRequestWriter() NLIB_NOEXCEPT : prv_(nullptr), is_msgpack_(false) {}
89 #ifdef __cpp_rvalue_references
91  is_msgpack_(rhs.is_msgpack_) {
92  rhs.prv_ = nullptr;
93  }
95  prv_ = rhs.prv_;
96  is_msgpack_ = rhs.is_msgpack_;
97  rhs.prv_ = nullptr;
98  return *this;
99  }
100 #endif
102  : prv_(rhs.prv_),
103  is_msgpack_(rhs.is_msgpack_) {
104  rhs.prv_ = nullptr;
105  }
106  JsonRpcRequestWriter& assign(JsonRpcRequestWriter& rhs, move_tag) NLIB_NOEXCEPT {
107  prv_ = rhs.prv_;
108  is_msgpack_ = rhs.is_msgpack_;
109  rhs.prv_ = nullptr;
110  return *this;
111  }
112  explicit JsonRpcRequestWriter(bool use_msgpack) NLIB_NOEXCEPT : prv_(nullptr),
113  is_msgpack_(use_msgpack) {}
114  errno_t BeginWriteRequest(uint32_t n) NLIB_NOEXCEPT;
115  errno_t WriteRequest(const char* method, reqid_t id, const MpObject& params) NLIB_NOEXCEPT;
116  errno_t WriteRequest(const char* method, reqid_t id) NLIB_NOEXCEPT {
117  MpObject nil_param;
118  return WriteRequest(method, id, nil_param);
119  }
120  errno_t WriteNotification(const char* method, const MpObject& params) NLIB_NOEXCEPT {
121  return WriteRequest(method, 0, params);
122  }
124  MpObject nil_param;
125  return WriteRequest(method, 0, nil_param);
126  }
128  return WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
129  }
130  errno_t EndWriteRequest(UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
131 #ifdef __cpp_rvalue_references
132  std::tuple<errno_t, UniquePtrType, size_t> EndWriteRequest() NLIB_NOEXCEPT;
133 #endif
134 
135  private:
136  struct JsonRpcRequestWriterPrivate;
137  JsonRpcRequestWriterPrivate* prv_;
138  bool is_msgpack_;
140 };
141 
143  public:
145  static errno_t ReadRequest(const void* p, size_t n, ListType* request_list) NLIB_NOEXCEPT;
146 
147  private:
149 };
150 
151 } // namespace jsonrpc
152 } // namespace msgpack
153 NLIB_NAMESPACE_END
154 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::jsonrpc::JsonRpcRequestWriter)
155 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
156 #undef NLIB_VIS_PUBLIC
157 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
158 #endif
159 
160 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
errno_t WriteRequest(const char *method, reqid_t id) noexcept
Writes a JSON-RPC request that specifies no parameters.
constexpr JsonRpcRequestWriter() noexcept
Instantiates the object with default parameters (default constructor).
JsonRpcRequestWriter(JsonRpcRequestWriter &&rhs) noexcept
Instantiates the object (move 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:183
void SetId(reqid_t id) noexcept
Sets the request ID.
Class to serialize and write the JSON-RPC request to memory.
In the C++11 environment (which supports alias templates), std::unique_ptr is made an alias template...
Definition: UniquePtr.h:108
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:86
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
Object created when MessagePack or JSON is read.
Definition: MpObject.h:96
void MoveParamsFrom(MpObject &&params) noexcept
Sets the JSON-RPC parameters.
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...
An empty structure indicating that an argument to a function needs to be moved.
Definition: Config.h:270
JsonRpcRequestWriter & operator=(JsonRpcRequestWriter &&rhs) noexcept
Move assignment operator.
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.
errno_t WriteNotification(const char *method) noexcept
Writes a JSON-RPC Notification that specifies no parameters.
Nlist< JsonRpcRequest > ListType
Type used to store the JSON-RPC request sequence restored from the byte string.
nlib_thread_id GetId() noexcept
Gets the current thread ID.
Definition: Thread.h:823
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
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:32
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:32
const MpObject & GetParams() const noexcept
The const decoration version of the above function.
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:250
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:37