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 #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
パラメータを省略したJSON-RPCのリクエストを書き込みます。
constexpr JsonRpcRequestWriter() noexcept
デフォルトコンストラクタです。
JsonRpcRequestWriter(JsonRpcRequestWriter &&rhs) 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:183
void SetId(reqid_t id) noexcept
リクエストのidを設定します。
JSON-RPCのリクエストをシリアライズしてメモリに書きこむためのクラスです。
C++11環境(エイリアステンプレートが可能な環境)においてはstd::unique_ptrにエイリアステンプレートされま...
Definition: UniquePtr.h:108
std::unique_ptrに相当するクラスが定義されています。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:86
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
MessagePack又はJSONを読み込むことで作成されるオブジェクトです。
Definition: MpObject.h:96
void MoveParamsFrom(MpObject &&params) noexcept
JSON-RPCのパラメータを設定します。
JsonRpcRequestWriter(bool use_msgpack) noexcept
trueにすることでJSONの記述方式にJSONではなくmsgpackを利用します。
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:270
JsonRpcRequestWriter & operator=(JsonRpcRequestWriter &&rhs) noexcept
ムーブ代入演算子です。
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リクエストのシーケンスを格納する型
nlib_thread_id GetId() noexcept
カレントスレッドのIDを取得します。
Definition: Thread.h:823
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:111
開発環境別の設定が書かれるファイルです。
uint32_t reqid_t
JSON-RPCのリクエストに付与されるidの型です。
Definition: JsonRpcClient.h:32
std::vectorに似た、コピーコンストラクタを持たないオブジェクトを格納可能なコンテナ類似クラスです。 ...
Definition: Nlist.h:32
const MpObject & GetParams() const noexcept
上記関数のconst修飾付き版です。
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:250
MpObject & GetParams() noexcept
このリクエストのパラメータを取得します。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37