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 
21 #include "nn/nlib/Config.h"
22 #include "nn/nlib/UniquePtr.h"
23 #include "nn/nlib/Nlist.h"
24 #include "nn/nlib/Swap.h"
28 
29 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
30 #undef NLIB_VIS_PUBLIC
31 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
32 #endif
33 
34 NLIB_NAMESPACE_BEGIN
35 namespace msgpack {
36 namespace jsonrpc {
37 
38 namespace fortest {
39 class JsonRpcErrorRequestTest;
40 } // namespace fortest
41 
42 typedef uint32_t reqid_t;
43 
45  public:
46  // NOTE:
47  // If id_ is 0, the request becomes notification(ID is not sent.).
48  JsonRpcRequest() NLIB_NOEXCEPT : id_(0), method_(NULL) {}
50  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
51  const char* GetMethod() const NLIB_NOEXCEPT { return method_; }
52  const MpObject& GetParams() const NLIB_NOEXCEPT { return params_; }
53  MpObject& GetParams() NLIB_NOEXCEPT { return params_; }
54 
55  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; }
56  bool SetMethod(const char* method) NLIB_NOEXCEPT;
57  void MoveParamsFrom(MpObject& params) NLIB_NOEXCEPT { // NOLINT
58  MpObject tmp;
59  tmp.swap(params);
60  tmp.swap(params_);
61  }
62 
63  private:
64  NLIB_VIS_HIDDEN JsonRpcResponse& GetResponse() NLIB_NOEXCEPT { return response_; }
65  NLIB_VIS_HIDDEN bool SetMethodRaw(const void* p, size_t n) NLIB_NOEXCEPT;
67 
68  private:
69  reqid_t id_;
70  char* method_;
71  MpObject params_;
72  JsonRpcResponse response_;
73 
74  friend NLIB_VIS_PUBLIC size_t JsonRpcServerExec(const void* p, size_t n,
76  friend class JsonRpcRequestReader;
77  friend class fortest::JsonRpcErrorRequestTest;
79 };
80 
82  public:
83  JsonRpcRequestWriter() NLIB_NOEXCEPT : prv_(NULL), is_msgpack_(false) {}
85  NLIB_MOVE_MEMBER_HELPER_1(JsonRpcRequestWriter, prv_);
86  explicit JsonRpcRequestWriter(bool use_msgpack) NLIB_NOEXCEPT
87  : prv_(NULL), is_msgpack_(use_msgpack) {}
88  errno_t BeginWriteRequest(uint32_t n) NLIB_NOEXCEPT;
89  errno_t WriteRequest(const char* method, reqid_t id, const MpObject& params) NLIB_NOEXCEPT;
90  errno_t WriteRequest(const char* method, reqid_t id) NLIB_NOEXCEPT {
91  MpObject nil_param;
92  return WriteRequest(method, id, nil_param);
93  }
94  errno_t WriteNotification(const char* method, const MpObject& params) NLIB_NOEXCEPT {
95  return WriteRequest(method, 0, params);
96  }
97  errno_t WriteNotification(const char* method) NLIB_NOEXCEPT {
98  MpObject nil_param;
99  return WriteRequest(method, 0, nil_param);
100  }
101  errno_t WriteRequest(const JsonRpcRequest& req) NLIB_NOEXCEPT {
102  return WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
103  }
104  errno_t EndWriteRequest(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
105  void swap(JsonRpcRequestWriter& rhs) NLIB_NOEXCEPT {
106  using std::swap;
107  swap(prv_, rhs.prv_);
108  swap(is_msgpack_, rhs.is_msgpack_);
109  }
110 
111  private:
112  struct JsonRpcRequestWriterPrivate;
113  JsonRpcRequestWriterPrivate* prv_;
114  bool is_msgpack_;
115 
117 };
118 
120  public:
122  static errno_t ReadRequest(const void* p, size_t n, ListType* request_list) NLIB_NOEXCEPT;
123 
124  private:
126 };
127 
128 } // namespace jsonrpc
129 } // namespace msgpack
130 NLIB_NAMESPACE_END
131 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::jsonrpc::JsonRpcRequestWriter)
132 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
133 #undef NLIB_VIS_PUBLIC
134 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
135 #endif
136 
137 #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.
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:163
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_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...
void swap(MpObject &rhs) noexcept
Swaps the content of the object.
Definition: MpObject.h:310
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:99
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:229
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