nlib
JsonRpcRequest.h
Go to the documentation of this file.
1 
2 /*---------------------------------------------------------------------------*
3 
4  Project: CrossRoad
5  Copyright (C)2012-2016 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
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"
27 
28 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
29 #undef NLIB_VIS_PUBLIC
30 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
31 #endif
32 
33 NLIB_NAMESPACE_BEGIN
34 namespace msgpack {
35 namespace jsonrpc {
36 
37 namespace fortest {
38 class JsonRpcErrorRequestTest;
39 } // namespace fortest
40 
41 typedef uint32_t reqid_t;
42 
44  public:
45  // NOTE:
46  // If id_ is 0, the request becomes notification(ID is not sent.).
47  JsonRpcRequest() NLIB_NOEXCEPT : id_(0), method_(NULL) {}
49  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
50  const char* GetMethod() const NLIB_NOEXCEPT { return method_; }
51  const MpObject& GetParams() const NLIB_NOEXCEPT { return params_; }
52  MpObject& GetParams() NLIB_NOEXCEPT { return params_; }
53 
54  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; }
55  bool SetMethod(const char* method) NLIB_NOEXCEPT;
56  void MoveParamsFrom(MpObject& params) NLIB_NOEXCEPT { // NOLINT
57  MpObject tmp;
58  tmp.swap(params);
59  tmp.swap(params_);
60  }
61 
62  private:
63  NLIB_VIS_HIDDEN JsonRpcResponse& GetResponse() NLIB_NOEXCEPT { return response_; }
64  NLIB_VIS_HIDDEN bool SetMethodRaw(const void* p, size_t n) NLIB_NOEXCEPT;
66 
67  private:
68  reqid_t id_;
69  char* method_;
70  MpObject params_;
71  JsonRpcResponse response_;
72 
73  friend NLIB_VIS_PUBLIC size_t JsonRpcServerExec(const void* p, size_t n,
75  friend class JsonRpcRequestReader;
76  friend class fortest::JsonRpcErrorRequestTest;
78 };
79 
81  public:
82  JsonRpcRequestWriter() NLIB_NOEXCEPT : prv_(NULL), is_msgpack_(false) {}
84  explicit JsonRpcRequestWriter(bool use_msgpack) NLIB_NOEXCEPT
85  : prv_(NULL), is_msgpack_(use_msgpack) {}
86  errno_t BeginWriteRequest(uint32_t n) NLIB_NOEXCEPT;
87  errno_t WriteRequest(const char* method, reqid_t id, const MpObject& params) NLIB_NOEXCEPT;
88  errno_t WriteNotification(const char* method, const MpObject& params) NLIB_NOEXCEPT {
89  return WriteRequest(method, 0, params);
90  }
91  errno_t WriteRequest(const JsonRpcRequest& req) NLIB_NOEXCEPT {
92  return WriteRequest(req.GetMethod(), req.GetId(), req.GetParams());
93  }
94  errno_t EndWriteRequest(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
95 
96  private:
97  struct JsonRpcRequestWriterPrivate;
98  JsonRpcRequestWriterPrivate* prv_;
99  bool is_msgpack_;
100 
102 };
103 
105  public:
107  static errno_t ReadRequest(const void* p, size_t n, ListType* request_list) NLIB_NOEXCEPT;
108 
109  private:
111 };
112 
113 } // namespace jsonrpc
114 } // namespace msgpack
115 NLIB_NAMESPACE_END
116 
117 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
118 #undef NLIB_VIS_PUBLIC
119 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
120 #endif
121 
122 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCREQUEST_H_
JsonRpcRequestWriter() noexcept
Instantiates the object with default parameters (default constructor).
Class for representing the JSON-RPC response.
JsonRpcRequest() noexcept
Instantiates the object with default parameters (default constructor).
errno_t WriteNotification(const char *method, const MpObject &params) noexcept
Writes the JSON-RPC notification.
const char * GetMethod() const noexcept
Gets the method name of the request.
#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:158
void SetId(reqid_t id) noexcept
Sets the request ID.
Class to serialize and write the JSON-RPC request to memory.
reqid_t GetId() const noexcept
Get the id of the request. If 0, the request is a notification.
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:109
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:86
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
Object created when MessagePack or JSON is read.
Definition: MpObject.h:95
JsonRpcRequestWriter(bool use_msgpack) noexcept
Sets the use of msgpack instead of JSON as the JSON description method.
Defines the class that resembles std::vector but can store objects that cannot be copied...
void swap(MpObject &rhs) noexcept
Swaps the content of the object.
Definition: MpObject.h:284
Class that reads the JSON-RPC request.
Class for representing a JSON-RPC request.
void MoveParamsFrom(MpObject &params) noexcept
Sets the JSON-RPC parameters.
Object created when MessagePack, JSON, or CSV is read.
Nlist< JsonRpcRequest > ListType
Type used to store the JSON-RPC request sequence restored from the byte string.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
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:32
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:32
const MpObject & GetParams() const noexcept
Gets the parameter of the request.
errno_t WriteRequest(const JsonRpcRequest &req) noexcept
Writes the JSON-RPC request.
size_t JsonRpcServerExec(const void *p, size_t n, ReallocOutputStream::UniquePtrType *ptr)
Creates the JSON-RPC response byte string by processing the JSON-RPC request.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:224
MpObject & GetParams() noexcept
Gets the parameter of the request.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37