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 
30 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
31 #undef NLIB_VIS_PUBLIC
32 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
33 #endif
34 
35 NLIB_NAMESPACE_BEGIN
36 namespace msgpack {
37 namespace jsonrpc {
38 
39 namespace fortest {
40 class JsonRpcErrorRequestTest;
41 } // namespace fortest
42 
43 typedef uint32_t reqid_t;
44 
46  public:
47  // NOTE:
48  // If id_ is 0, the request becomes notification(ID is not sent.).
49  JsonRpcRequest() NLIB_NOEXCEPT : id_(0), method_(nullptr) {}
51  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
52  const char* GetMethod() const NLIB_NOEXCEPT { return method_; }
53  const MpObject& GetParams() const NLIB_NOEXCEPT { return params_; }
54  MpObject& GetParams() NLIB_NOEXCEPT { return params_; }
55 
56  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; }
57  bool SetMethod(const char* method) NLIB_NOEXCEPT;
58  void MoveParamsFrom(MpObject& params) NLIB_NOEXCEPT { // NOLINT
59  params_.assign(params, move_tag());
60  }
61 
62  private:
63  NLIB_VIS_HIDDEN JsonRpcResponse& GetResponse() NLIB_NOEXCEPT { return response_; }
64  NLIB_VIS_HIDDEN bool SetMethodRaw(const void* p, size_t n) NLIB_NOEXCEPT;
66 
67  private:
68  reqid_t id_;
69  char* method_;
70  MpObject params_;
71  JsonRpcResponse response_;
72 
73  friend NLIB_VIS_PUBLIC size_t JsonRpcServerExec(const void* p, size_t n,
75  friend class JsonRpcRequestReader;
76  friend class fortest::JsonRpcErrorRequestTest;
78 };
79 
81  public:
82  NLIB_CEXPR JsonRpcRequestWriter() NLIB_NOEXCEPT : prv_(nullptr), is_msgpack_(false) {}
84 #ifdef __cpp_rvalue_references
86  : prv_(rhs.prv_), is_msgpack_(rhs.is_msgpack_) {
87  rhs.prv_ = nullptr;
88  }
90  prv_ = rhs.prv_;
91  is_msgpack_ = rhs.is_msgpack_;
92  rhs.prv_ = nullptr;
93  return *this;
94  }
95 #endif
97  : prv_(rhs.prv_), is_msgpack_(rhs.is_msgpack_) {
98  rhs.prv_ = nullptr;
99  }
101  prv_ = rhs.prv_;
102  is_msgpack_ = rhs.is_msgpack_;
103  rhs.prv_ = nullptr;
104  return *this;
105  }
107  using std::swap;
108  swap(prv_, rhs.prv_);
109  swap(is_msgpack_, rhs.is_msgpack_);
110  }
111  explicit JsonRpcRequestWriter(bool use_msgpack) NLIB_NOEXCEPT
112  : prv_(nullptr), is_msgpack_(use_msgpack) {}
113  errno_t BeginWriteRequest(uint32_t n) NLIB_NOEXCEPT;
114  errno_t WriteRequest(const char* method, reqid_t id, const MpObject& params) NLIB_NOEXCEPT;
115  errno_t WriteRequest(const char* method, reqid_t id) NLIB_NOEXCEPT {
116  MpObject nil_param;
117  return WriteRequest(method, id, nil_param);
118  }
119  errno_t WriteNotification(const char* method, const MpObject& params) NLIB_NOEXCEPT {
120  return WriteRequest(method, 0, params);
121  }
122  errno_t WriteNotification(const char* method) NLIB_NOEXCEPT {
123  MpObject nil_param;
124  return WriteRequest(method, 0, nil_param);
125  }
126  errno_t WriteRequest(const JsonRpcRequest& req) NLIB_NOEXCEPT {
127  return WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
128  }
129  errno_t EndWriteRequest(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
130 
131  private:
132  struct JsonRpcRequestWriterPrivate;
133  JsonRpcRequestWriterPrivate* prv_;
134  bool is_msgpack_;
136 };
137 
139  public:
141  static errno_t ReadRequest(const void* p, size_t n, ListType* request_list) NLIB_NOEXCEPT;
142 
143  private:
145 };
146 
147 } // namespace jsonrpc
148 } // namespace msgpack
149 NLIB_NAMESPACE_END
150 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::jsonrpc::JsonRpcRequestWriter)
151 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
152 #undef NLIB_VIS_PUBLIC
153 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
154 #endif
155 
156 #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).
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:179
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:109
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:88
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
Object created when MessagePack or JSON is read.
Definition: MpObject.h:95
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:265
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.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
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
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:245
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