nlib
JsonRpcClient.h
[詳解]
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCCLIENT_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCCLIENT_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/Swap.h"
8 #include "nn/nlib/UniquePtr.h"
10 
11 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
12 #undef NLIB_VIS_PUBLIC
13 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
14 #endif
15 
16 NLIB_NAMESPACE_BEGIN
17 namespace msgpack {
18 namespace jsonrpc {
19 typedef uint32_t reqid_t;
20 
21 class JsonRpcResponseHandler;
22 class JsonRpcResponse;
23 
25  public:
26  static JsonRpcClient OpenClient(const char* name);
27  static JsonRpcClient SearchClient(const char* name);
28  static void CloseClient(const JsonRpcClient& client);
29  static void Shutdown();
30 
31  public:
32  typedef void (*NoTargetResponseHandler)(UniquePtr<JsonRpcResponse>& rhs); // NOLINT
33  JsonRpcClient() : m_Handle(0) {}
34  JsonRpcClient(const JsonRpcClient& rhs) : m_Handle(rhs.m_Handle) {}
36  m_Handle = rhs.m_Handle;
37  return *this;
38  }
39  void swap(JsonRpcClient& rhs) { // NOLINT
40  using std::swap;
41  swap(m_Handle, rhs.m_Handle);
42  }
43 
44  public:
45  reqid_t GenerateId();
46 
47  // future becomes valid after response or timeout
48  typedef ::nlib_ns::threading::Future<UniquePtr<JsonRpcResponse> > FutureType;
49  errno_t GetFutureForId(FutureType* future, reqid_t reqid);
50  errno_t ResolveResponse(const void* buf, size_t nbytes);
51  errno_t Abort(reqid_t reqid);
52  errno_t SetNoTargetResponseHandler(NoTargetResponseHandler handler);
53 
54  bool operator==(const JsonRpcClient& rhs) const { return m_Handle == rhs.m_Handle; }
55  bool operator!=(const JsonRpcClient& rhs) const { return m_Handle != rhs.m_Handle; }
56  NLIB_SAFE_BOOL(JsonRpcClient, m_Handle != 0);
57 
58  private:
59  explicit NLIB_VIS_HIDDEN JsonRpcClient(uint32_t handle) : m_Handle(handle) {}
60 
61  private:
62  uint32_t m_Handle;
63  friend class JsonRpcClientImpl;
64 };
65 
66 } // namespace jsonrpc
67 } // namespace msgpack
68 NLIB_NAMESPACE_END
69 
70 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::jsonrpc::JsonRpcClient)
71 
72 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
73 #undef NLIB_VIS_PUBLIC
74 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
75 #endif
76 
77 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCCLIENT_H_
JsonRpcClient(const JsonRpcClient &rhs)
コピーコンストラクタです。
Definition: JsonRpcClient.h:34
void swap(JsonRpcClient &rhs)
スワップです。
Definition: JsonRpcClient.h:39
JSON-RPCのレスポンスを表すクラスです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:141
bool operator==(const JsonRpcClient &rhs) const
クライアントが同一の場合にtrueを返します。
Definition: JsonRpcClient.h:54
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:50
UniquePtrはポインタの所有権を保持し、UniquePtrがスコープから出るときにデストラクタでポインタをDELで指...
Definition: UniquePtr.h:96
std::unique_ptrに相当するクラスが定義されています。
::nlib_ns::threading::Future< UniquePtr< JsonRpcResponse > > FutureType
それぞれにリクエストに対して割り当てられる、レスポンス(又はタイムアウト)があるとセットされるfutureの...
Definition: JsonRpcClient.h:48
JsonRpcClient()
デフォルトコンストラクタです。
Definition: JsonRpcClient.h:33
JsonRpcClient & operator=(const JsonRpcClient &rhs)
代入演算子です。
Definition: JsonRpcClient.h:35
開発環境別の設定が書かれるファイルです。
uint32_t reqid_t
JSON-RPCのリクエストに付与されるidの型です。
Definition: JsonRpcClient.h:19
JSON-RPCのクライアント側の管理を行うクラスです。
Definition: JsonRpcClient.h:24
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
マルチスレッドプログラミングのためのFutureパターンを実装しています。
bool operator!=(const JsonRpcClient &rhs) const
クライアントが同一の場合にfalseを返します。
Definition: JsonRpcClient.h:55
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24