nlib
JsonRpcClient.h
Go to the documentation of this file.
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)
Copy constructor.
Definition: JsonRpcClient.h:34
void swap(JsonRpcClient &rhs)
A swap.
Definition: JsonRpcClient.h:39
Class for representing the JSON-RPC response.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:141
bool operator==(const JsonRpcClient &rhs) const
Returns true if the client is the same.
Definition: JsonRpcClient.h:54
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
::nlib_ns::threading::Future< UniquePtr< JsonRpcResponse > > FutureType
A future type set when a response (or timeout) is assigned to each request.
Definition: JsonRpcClient.h:48
JsonRpcClient()
Instantiates the object with default parameters (default constructor).
Definition: JsonRpcClient.h:33
JsonRpcClient & operator=(const JsonRpcClient &rhs)
Assignment operator.
Definition: JsonRpcClient.h:35
A file that contains the configuration information for each development environment.
uint32_t reqid_t
id type for the JSON-RPC request.
Definition: JsonRpcClient.h:19
Class to manage client side JSON-RPC.
Definition: JsonRpcClient.h:24
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
Implements the Future pattern for multithread programming.
bool operator!=(const JsonRpcClient &rhs) const
Returns false if the client is the same.
Definition: JsonRpcClient.h:55
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24