nlib
JsonRpcResponse.h
[詳解]
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/Swap.h"
8 #include "nn/nlib/Nlist.h"
10 #include "nn/nlib/UniquePtr.h"
13 
14 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
15 #undef NLIB_VIS_PUBLIC
16 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
17 #endif
18 
19 NLIB_NAMESPACE_BEGIN
20 namespace msgpack {
21 namespace jsonrpc {
22 typedef uint32_t reqid_t;
23 
25  public:
26  enum ErrorCode {
27  OK = 0,
28  PARSE_ERROR = -32700,
29  INVALID_REQUEST = -32600,
30  METHOD_NOT_FOUND = -32601,
31  INVALID_PARAMS = -32602,
32  INTERNAL_ERROR = -32603,
33  CLIENT_ABORT = -31000
34  };
35  JsonRpcResponse() : m_Id(0), m_IsError(false), m_ErrorCode(0) {}
36 
37  NLIB_VIS_PUBLIC void MoveResultFrom(MpObject& obj); // NOLINT
38  NLIB_VIS_PUBLIC void SetError(int errcode, const char* msg, MpObject& data); // NOLINT
39  void SetError(int errcode, const char* msg) {
40  MpObject data;
41  this->SetError(errcode, msg, data);
42  }
43 
44  bool IsError() const { return m_IsError; }
45  int GetErrorCode() const { return m_ErrorCode; }
46  reqid_t GetId() const { return m_Id; }
47  const char* GetErrorMessage() const {
48  const char* msg = m_ErrorMsg.get();
49  return msg ? msg : "";
50  }
51  MpObject& GetMpObject() { return m_Obj; }
52  const MpObject& GetMpObject() const { return m_Obj; }
53 
54  NLIB_SAFE_BOOL(JsonRpcResponse, !IsError());
55 
56  private:
57  void SetId(reqid_t id) { m_Id = id; } // from JsonRpcRequest::Init()
58  NLIB_VIS_HIDDEN errno_t Init(MpObject* rhs); // from JsonRpcResponseReader
59  NLIB_VIS_HIDDEN errno_t Init(MpWalker* walker); // from JsonRpcResponseReader
60 
61  private:
62  reqid_t m_Id;
63  bool m_IsError;
64  int m_ErrorCode;
65  UniquePtr<char[]> m_ErrorMsg;
66  MpObject m_Obj;
67 
68  friend class JsonRpcResponseReader;
69  friend class JsonRpcRequest;
70  NLIB_DISALLOW_COPY_AND_ASSIGN(JsonRpcResponse);
71 };
72 
74  public:
77  static errno_t ReadResponse(const void* p, size_t n, ListType* result_list);
78 
79  private:
80  NLIB_VIS_HIDDEN static errno_t ReadJsonResponse(const char* p, size_t n,
81  ListType* result_list);
82  NLIB_VIS_HIDDEN static errno_t ReadMsgpackResponse(const uint8_t* p, size_t n,
83  ListType* result_list);
84 };
85 
87  public:
88  JsonRpcResponseWriter() : m_IsMsgpack(false), m_Cur(0), m_Count(0) {}
89  explicit JsonRpcResponseWriter(bool msgpack) : m_IsMsgpack(msgpack), m_Cur(0), m_Count(0) {}
90  NLIB_VIS_PUBLIC errno_t BeginWriteResponse(uint32_t n);
91  NLIB_VIS_PUBLIC errno_t WriteResponse(JsonRpcResponse* result);
93  size_t* n);
94 
95  private:
96  NLIB_VIS_HIDDEN errno_t WriteJsonResult(JsonRpcResponse* result);
97  NLIB_VIS_HIDDEN errno_t WriteMsgpackResult(JsonRpcResponse* result);
98 
99  private:
100  bool m_IsMsgpack;
101  size_t m_Cur;
102  size_t m_Count;
103  ReallocOutputStream m_Ostr;
105 };
106 
107 } // namespace jsonrpc
108 } // namespace msgpack
109 NLIB_NAMESPACE_END
110 
111 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
112 #undef NLIB_VIS_PUBLIC
113 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
114 #endif
115 
116 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
UniquePtr< JsonRpcResponse > ValueType
UniquePtr経由でJSON-RPCのレスポンスを格納します。
JSON-RPCのレスポンスを表すクラスです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:126
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:141
JsonRpcResponseWriter(bool msgpack)
引数にtrueを指定するとmsgpackのバイト列を作成します。
nlib_realloc()等のrealloc関数を利用して拡張するメモリ領域に書きこむ出力ストリームです。 ...
bool IsError() const
エラーか発生しているかどうかを返します。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:50
std::unique_ptrに相当するクラスが定義されています。
void SetError(int errcode, const char *msg)
JSON-RPC関数のエラーを設定します。
JSON-RPCのレスポンスのバイト列を読み込むためのクラスです。
int GetErrorCode() const
エラーコードを返します。
MessagePack又はJSONを読み込むことで作成されるオブジェクトです。
Definition: MpObject.h:83
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
reqid_t GetId() const
JSON-RPCレスポンスのid を返します。
const MpObject & GetMpObject() const
JSON-RPCの戻り値か、エラーの付加情報を返します。
MessagePack, JSON及びCSVを読み込むと作成されるオブジェクトです。
ErrorCode
JSON-RPCのレスポンスに含まれる(事前定義の)エラーコードの定義です。
開発環境別の設定が書かれるファイルです。
JsonRpcResponse()
デフォルトコンストラクタです。
uint32_t reqid_t
JSON-RPCのリクエストに付与されるidの型です。
Definition: JsonRpcClient.h:19
std::vectorに似た、コピーコンストラクタを持たないオブジェクトを格納可能なコンテナ類似クラスです。 ...
Definition: Nlist.h:19
JSON-RPCのレスポンスのバイト列を書きこむためのクラスです。
JsonRpcResponseWriter()
デフォルトコンストラクタです。
Nlist< ValueType > ListType
JSON-RPCのレスポンスのシーケンスを格納する型です。
メモリ上に展開されたMessagePackのデータに高速にアクセスします。
Definition: MpWalker.h:17
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
const char * GetErrorMessage() const
エラーメッセージを返します。
MpObject & GetMpObject()
JSON-RPCの戻り値か、エラーの付加情報を返します。
MessagePackを高速に読み込むためのクラスが定義されています。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24