nlib
JsonRpcResponse.h
Go to the documentation of this file.
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
Stores the JSON-RPC response via UniquePtr.
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_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
#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
JsonRpcResponseWriter(bool msgpack)
Creates a msgpack byte string when true is specified as the argument.
The class for output streams written to memory expanded using nlib_realloc or another realloc functio...
bool IsError() const
Returns whether an error has occurred.
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
Defines that class that is corresponding to std::unique_ptr.
void SetError(int errcode, const char *msg)
Sets the JSON-RPC function error.
Class that reads the JSON-RPC response byte string.
int GetErrorCode() const
Returns an error code.
Object created when MessagePack or JSON is read.
Definition: MpObject.h:83
Defines the class that resembles std::vector but can store objects that cannot be copied...
reqid_t GetId() const
Returns the ID of the JSON-RPC response.
const MpObject & GetMpObject() const
Returns the JSON-RPC return value, or additional information about an error.
Object created when MessagePack, JSON, or CSV is read.
ErrorCode
Definition of the predefined error code contained in the JSON-RPC response.
A file that contains the configuration information for each development environment.
JsonRpcResponse()
Instantiates the object with default parameters (default constructor).
uint32_t reqid_t
id type for the JSON-RPC request.
Definition: JsonRpcClient.h:19
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:19
Class that writes the JSON-RPC response byte string.
JsonRpcResponseWriter()
Instantiates the object with default parameters (default constructor).
Nlist< ValueType > ListType
Type that stores the JSON-RPC response sequence.
Quickly accesses MessagePack expanded in memory.
Definition: MpWalker.h:17
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
const char * GetErrorMessage() const
Returns an error message.
MpObject & GetMpObject()
Returns the JSON-RPC return value, or additional information about an error.
Defines the class for reading MessagePack at high speed.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24