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 
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
パラメータを省略したJSON-RPCのリクエストを書き込みます。
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:163
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_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
MessagePack又はJSONを読み込むことで作成されるオブジェクトです。
Definition: MpObject.h:95
JsonRpcRequestWriter(bool use_msgpack) noexcept
trueにすることでJSONの記述方式にJSONではなくmsgpackを利用します。
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
void swap(MpObject &rhs) noexcept
オブジェクトの中身をスワップします。
Definition: MpObject.h:310
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:99
開発環境別の設定が書かれるファイルです。
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:229
MpObject & GetParams() noexcept
このリクエストのパラメータを取得します。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37