aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/message.h
diff options
context:
space:
mode:
authorLee Clagett <code@leeclagett.com>2019-11-07 05:45:06 +0000
committerLee Clagett <code@leeclagett.com>2020-03-05 14:20:56 +0000
commit0f78b06e8c578819f831a513490278a5f70b01af (patch)
tree2f940ff50b1676b92887a4a755b7eb1ba462c04c /src/rpc/message.h
parentMerge pull request #6057 (diff)
downloadmonero-0f78b06e8c578819f831a513490278a5f70b01af.tar.xz
Various improvements to the ZMQ JSON-RPC handling:
- Finding handling function in ZMQ JSON-RPC now uses binary search - Temporary `std::vector`s in JSON output now use `epee::span` to prevent allocations. - Binary -> hex in JSON output no longer allocates temporary buffer - C++ structs -> JSON skips intermediate DOM creation, and instead write directly to an output stream.
Diffstat (limited to '')
-rw-r--r--src/rpc/message.h49
1 files changed, 14 insertions, 35 deletions
diff --git a/src/rpc/message.h b/src/rpc/message.h
index 2b7b61ab3..4cbc84fe4 100644
--- a/src/rpc/message.h
+++ b/src/rpc/message.h
@@ -28,27 +28,12 @@
#pragma once
-#include "rapidjson/document.h"
-#include "rpc/message_data_structs.h"
+#include <rapidjson/document.h>
+#include <rapidjson/stringbuffer.h>
+#include <rapidjson/writer.h>
#include <string>
-/* I normally hate using macros, but in this case it would be untenably
- * verbose to not use a macro. This macro saves the trouble of explicitly
- * writing the below if block for every single RPC call.
- */
-#define REQ_RESP_TYPES_MACRO( runtime_str, type, reqjson, resp_message_ptr, handler) \
- \
- if (runtime_str == type::name) \
- { \
- type::Request reqvar; \
- type::Response *respvar = new type::Response(); \
- \
- reqvar.fromJson(reqjson); \
- \
- handler(reqvar, *respvar); \
- \
- resp_message_ptr = respvar; \
- }
+#include "rpc/message_data_structs.h"
namespace cryptonote
{
@@ -58,6 +43,9 @@ namespace rpc
class Message
{
+ virtual void doToJson(rapidjson::Writer<rapidjson::StringBuffer>& dest) const
+ {}
+
public:
static const char* STATUS_OK;
static const char* STATUS_RETRY;
@@ -69,9 +57,9 @@ namespace rpc
virtual ~Message() { }
- virtual rapidjson::Value toJson(rapidjson::Document& doc) const;
+ void toJson(rapidjson::Writer<rapidjson::StringBuffer>& dest) const;
- virtual void fromJson(rapidjson::Value& val);
+ virtual void fromJson(const rapidjson::Value& val);
std::string status;
std::string error_details;
@@ -87,27 +75,18 @@ namespace rpc
FullMessage(const std::string& json_string, bool request=false);
- std::string getJson();
-
std::string getRequestType() const;
- rapidjson::Value& getMessage();
+ const rapidjson::Value& getMessage() const;
rapidjson::Value getMessageCopy();
- rapidjson::Value& getID();
-
- void setID(rapidjson::Value& id);
+ const rapidjson::Value& getID() const;
cryptonote::rpc::error getError();
- static FullMessage requestMessage(const std::string& request, Message* message);
- static FullMessage requestMessage(const std::string& request, Message* message, rapidjson::Value& id);
-
- static FullMessage responseMessage(Message* message);
- static FullMessage responseMessage(Message* message, rapidjson::Value& id);
-
- static FullMessage* timeoutMessage();
+ static std::string getRequest(const std::string& request, const Message& message, unsigned id);
+ static std::string getResponse(const Message& message, const rapidjson::Value& id);
private:
FullMessage() = default;
@@ -121,7 +100,7 @@ namespace rpc
// convenience functions for bad input
std::string BAD_REQUEST(const std::string& request);
- std::string BAD_REQUEST(const std::string& request, rapidjson::Value& id);
+ std::string BAD_REQUEST(const std::string& request, const rapidjson::Value& id);
std::string BAD_JSON(const std::string& error_details);