nlib
JsonRpcRequest.h
[詳解]
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
パラメータを省略したJSON-RPCのリクエストを書き込みます。
constexpr JsonRpcRequestWriter() noexcept
デフォルトコンストラクタです。
JSON-RPCのレスポンスを表すクラスです。
JsonRpcRequest() noexcept
デフォルトコンストラクタです。
errno_t WriteNotification(const char *method, const MpObject &params) noexcept
JSON-RPCのNotificationを書き込みます。
const char * GetMethod() const noexcept
このリクエストのメソッド名を取得します。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
void SetId(reqid_t id) noexcept
リクエストのidを設定します。
JSON-RPCのリクエストをシリアライズしてメモリに書きこむためのクラスです。
reqid_t GetId() const noexcept
このリクエストのid を取得します。0の場合はリクエストはnotificationとなります。
UniquePtrはポインタの所有権を保持し、UniquePtrがスコープから出るときにデストラクタでポインタをDELで指...
Definition: UniquePtr.h:109
std::unique_ptrに相当するクラスが定義されています。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:88
#define NLIB_DEPRECATED
関数等がdeprecatedになったことを示します。
Definition: Config.h:109
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
MessagePack又はJSONを読み込むことで作成されるオブジェクトです。
Definition: MpObject.h:95
JsonRpcRequestWriter(bool use_msgpack) noexcept
trueにすることでJSONの記述方式にJSONではなくmsgpackを利用します。
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:265
JSON-RPCリクエストを読み込むためのクラスです。
JSON-RPCのリクエストを表すクラスです。
void MoveParamsFrom(MpObject &params) noexcept
JSON-RPCのパラメータを設定します。
MessagePack, JSON及びCSVを読み込むと作成されるオブジェクトです。
errno_t WriteNotification(const char *method) noexcept
パラメータを省略したJSON-RPCのNotificationを書き込みます。
Nlist< JsonRpcRequest > ListType
バイト列から復元されるJSON-RPCリクエストのシーケンスを格納する型
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:107
開発環境別の設定が書かれるファイルです。
uint32_t reqid_t
JSON-RPCのリクエストに付与されるidの型です。
Definition: JsonRpcClient.h:32
std::vectorに似た、コピーコンストラクタを持たないオブジェクトを格納可能なコンテナ類似クラスです。 ...
Definition: Nlist.h:32
const MpObject & GetParams() const noexcept
このリクエストのパラメータを取得します。
errno_t WriteRequest(const JsonRpcRequest &req) noexcept
JSON-RPCのリクエストを書き込みます。
size_t JsonRpcServerExec(const void *p, size_t n, ReallocOutputStream::UniquePtrType *ptr)
JSON-RPCリクエストを処理してJSON-RPCレスポンスのバイト列を作成します。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
MpObject & GetParams() noexcept
このリクエストのパラメータを取得します。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37