Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/server.h
Show All 20 Lines | |||||
#include <map> | #include <map> | ||||
#include <string> | #include <string> | ||||
#include <boost/noncopyable.hpp> | #include <boost/noncopyable.hpp> | ||||
#include <univalue.h> | #include <univalue.h> | ||||
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1; | static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1; | ||||
class ContextFreeRPCCommand; | class CRPCCommand; | ||||
namespace RPCServerSignals { | namespace RPCServerSignals { | ||||
void OnStarted(std::function<void()> slot); | void OnStarted(std::function<void()> slot); | ||||
void OnStopped(std::function<void()> slot); | void OnStopped(std::function<void()> slot); | ||||
} // namespace RPCServerSignals | } // namespace RPCServerSignals | ||||
class Config; | class Config; | ||||
▲ Show 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | |||||
void RPCRunLater(const std::string &name, std::function<void()> func, | void RPCRunLater(const std::string &name, std::function<void()> func, | ||||
int64_t nSeconds); | int64_t nSeconds); | ||||
typedef UniValue (*rpcfn_type)(Config &config, | typedef UniValue (*rpcfn_type)(Config &config, | ||||
const JSONRPCRequest &jsonRequest); | const JSONRPCRequest &jsonRequest); | ||||
typedef UniValue (*const_rpcfn_type)(const Config &config, | typedef UniValue (*const_rpcfn_type)(const Config &config, | ||||
const JSONRPCRequest &jsonRequest); | const JSONRPCRequest &jsonRequest); | ||||
class ContextFreeRPCCommand { | class CRPCCommand { | ||||
public: | public: | ||||
std::string category; | std::string category; | ||||
std::string name; | std::string name; | ||||
private: | private: | ||||
union { | union { | ||||
rpcfn_type fn; | rpcfn_type fn; | ||||
const_rpcfn_type cfn; | const_rpcfn_type cfn; | ||||
} actor; | } actor; | ||||
bool useConstConfig; | bool useConstConfig; | ||||
public: | public: | ||||
std::vector<std::string> argNames; | std::vector<std::string> argNames; | ||||
ContextFreeRPCCommand(std::string _category, std::string _name, | CRPCCommand(std::string _category, std::string _name, rpcfn_type _actor, | ||||
rpcfn_type _actor, std::vector<std::string> _argNames) | std::vector<std::string> _argNames) | ||||
: category{std::move(_category)}, name{std::move(_name)}, | : category{std::move(_category)}, name{std::move(_name)}, | ||||
useConstConfig{false}, argNames{std::move(_argNames)} { | useConstConfig{false}, argNames{std::move(_argNames)} { | ||||
actor.fn = _actor; | actor.fn = _actor; | ||||
} | } | ||||
/** | /** | ||||
* There are 2 constructors depending Config is const or not, so we | * There are 2 constructors depending Config is const or not, so we | ||||
* can call the command through the proper pointer. Casting constness | * can call the command through the proper pointer. Casting constness | ||||
* on parameters of function is undefined behavior. | * on parameters of function is undefined behavior. | ||||
*/ | */ | ||||
ContextFreeRPCCommand(std::string _category, std::string _name, | CRPCCommand(std::string _category, std::string _name, | ||||
const_rpcfn_type _actor, | const_rpcfn_type _actor, std::vector<std::string> _argNames) | ||||
std::vector<std::string> _argNames) | |||||
: category{std::move(_category)}, name{std::move(_name)}, | : category{std::move(_category)}, name{std::move(_name)}, | ||||
useConstConfig{true}, argNames{std::move(_argNames)} { | useConstConfig{true}, argNames{std::move(_argNames)} { | ||||
actor.cfn = _actor; | actor.cfn = _actor; | ||||
} | } | ||||
UniValue call(Config &config, const JSONRPCRequest &jsonRequest) const { | UniValue call(Config &config, const JSONRPCRequest &jsonRequest) const { | ||||
return useConstConfig ? (*actor.cfn)(config, jsonRequest) | return useConstConfig ? (*actor.cfn)(config, jsonRequest) | ||||
: (*actor.fn)(config, jsonRequest); | : (*actor.fn)(config, jsonRequest); | ||||
}; | }; | ||||
}; | }; | ||||
/** | /** | ||||
* Bitcoin RPC command dispatcher. | * Bitcoin RPC command dispatcher. | ||||
*/ | */ | ||||
class CRPCTable { | class CRPCTable { | ||||
private: | private: | ||||
std::map<std::string, const ContextFreeRPCCommand *> mapCommands; | std::map<std::string, const CRPCCommand *> mapCommands; | ||||
public: | public: | ||||
CRPCTable(); | CRPCTable(); | ||||
const ContextFreeRPCCommand *operator[](const std::string &name) const; | const CRPCCommand *operator[](const std::string &name) const; | ||||
std::string help(Config &config, const std::string &name, | std::string help(Config &config, const std::string &name, | ||||
const JSONRPCRequest &helpreq) const; | const JSONRPCRequest &helpreq) const; | ||||
/** | /** | ||||
* Execute a method. | * Execute a method. | ||||
* @param request The JSONRPCRequest to execute | * @param request The JSONRPCRequest to execute | ||||
* @returns Result of the call. | * @returns Result of the call. | ||||
* @throws an exception (UniValue) when an error happens. | * @throws an exception (UniValue) when an error happens. | ||||
*/ | */ | ||||
UniValue execute(Config &config, const JSONRPCRequest &request) const; | UniValue execute(Config &config, const JSONRPCRequest &request) const; | ||||
/** | /** | ||||
* Returns a list of registered commands | * Returns a list of registered commands | ||||
* @returns List of registered commands. | * @returns List of registered commands. | ||||
*/ | */ | ||||
std::vector<std::string> listCommands() const; | std::vector<std::string> listCommands() const; | ||||
/** | /** | ||||
* Appends a ContextFreeRPCCommand to the dispatch table. | * Appends a CRPCCommand to the dispatch table. | ||||
* | * | ||||
* Returns false if RPC server is already running (dump concurrency | * Returns false if RPC server is already running (dump concurrency | ||||
* protection). | * protection). | ||||
* | * | ||||
* Commands cannot be overwritten (returns false). | * Commands cannot be overwritten (returns false). | ||||
* | * | ||||
* Commands with different method names but the same callback function will | * Commands with different method names but the same callback function will | ||||
* be considered aliases, and only the first registered method name will | * be considered aliases, and only the first registered method name will | ||||
* show up in the help text command listing. Aliased commands do not have | * show up in the help text command listing. Aliased commands do not have | ||||
* to have the same behavior. Server and client code can distinguish | * to have the same behavior. Server and client code can distinguish | ||||
* between calls based on method name, and aliased commands can also | * between calls based on method name, and aliased commands can also | ||||
* register different names, types, and numbers of parameters. | * register different names, types, and numbers of parameters. | ||||
*/ | */ | ||||
bool appendCommand(const std::string &name, | bool appendCommand(const std::string &name, const CRPCCommand *pcmd); | ||||
const ContextFreeRPCCommand *pcmd); | |||||
}; | }; | ||||
bool IsDeprecatedRPCEnabled(ArgsManager &args, const std::string &method); | bool IsDeprecatedRPCEnabled(ArgsManager &args, const std::string &method); | ||||
extern CRPCTable tableRPC; | extern CRPCTable tableRPC; | ||||
/** | /** | ||||
* Utilities: convert hex-encoded values (throws error if not hex). | * Utilities: convert hex-encoded values (throws error if not hex). | ||||
Show All 24 Lines |