mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -49,12 +49,21 @@ concept Requirement = requires(T a) {
|
||||
*/
|
||||
// clang-format off
|
||||
template <typename T>
|
||||
concept Handler = requires(T a, typename T::Input in, typename T::Output out) {
|
||||
concept HandlerWithInput = requires(T a, typename T::Input in, typename T::Output out) {
|
||||
{ a.spec() } -> std::same_as<RpcSpecConstRef>;
|
||||
{ a.process(in) } -> std::same_as<HandlerReturnType<decltype(out)>>;
|
||||
}
|
||||
&& boost::json::has_value_from<typename T::Output>::value
|
||||
&& boost::json::has_value_to<typename T::Input>::value;
|
||||
{ a.process(in) } -> std::same_as<HandlerReturnType<decltype(out)>>;}
|
||||
&& boost::json::has_value_to<typename T::Input>::value;
|
||||
|
||||
template <typename T>
|
||||
concept HandlerWithoutInput = requires(T a, typename T::Output out) {
|
||||
{ a.process() } -> std::same_as<HandlerReturnType<decltype(out)>>;};
|
||||
|
||||
template <typename T>
|
||||
concept Handler =
|
||||
(HandlerWithInput<T>
|
||||
||
|
||||
HandlerWithoutInput<T>)
|
||||
&& boost::json::has_value_from<typename T::Output>::value;
|
||||
// clang-format on
|
||||
|
||||
} // namespace RPCng
|
||||
|
||||
@@ -53,4 +53,14 @@ struct FieldSpec;
|
||||
|
||||
using RpcSpecConstRef = RpcSpec const&;
|
||||
|
||||
struct VoidOutput
|
||||
{
|
||||
};
|
||||
|
||||
inline void
|
||||
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput)
|
||||
{
|
||||
jv = boost::json::object{};
|
||||
}
|
||||
|
||||
} // namespace RPCng
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
|
||||
namespace RPCng::detail {
|
||||
|
||||
template <typename>
|
||||
static constexpr bool unsupported_handler_v = false;
|
||||
|
||||
template <Handler HandlerType>
|
||||
struct DefaultProcessor final
|
||||
{
|
||||
@@ -33,19 +36,35 @@ struct DefaultProcessor final
|
||||
{
|
||||
using boost::json::value_from;
|
||||
using boost::json::value_to;
|
||||
if constexpr (HandlerWithInput<HandlerType>)
|
||||
{
|
||||
// first we run validation
|
||||
auto const spec = handler.spec();
|
||||
if (auto const ret = spec.validate(value); not ret)
|
||||
return Error{ret.error()}; // forward Status
|
||||
|
||||
// first we run validation
|
||||
auto const spec = handler.spec();
|
||||
if (auto const ret = spec.validate(value); not ret)
|
||||
return Error{ret.error()}; // forward Status
|
||||
auto const inData = value_to<typename HandlerType::Input>(value);
|
||||
|
||||
auto const inData = value_to<typename HandlerType::Input>(value);
|
||||
|
||||
// real handler is given expected Input, not json
|
||||
if (auto const ret = handler.process(inData); not ret)
|
||||
return Error{ret.error()}; // forward Status
|
||||
// real handler is given expected Input, not json
|
||||
if (auto const ret = handler.process(inData); not ret)
|
||||
return Error{ret.error()}; // forward Status
|
||||
else
|
||||
return value_from(ret.value());
|
||||
}
|
||||
else if constexpr (HandlerWithoutInput<HandlerType>)
|
||||
{
|
||||
// no input to pass, ignore the value
|
||||
if (auto const ret = handler.process(); not ret)
|
||||
return Error{ret.error()}; // forward Status
|
||||
else
|
||||
return value_from(ret.value());
|
||||
}
|
||||
else
|
||||
return value_from(ret.value());
|
||||
{
|
||||
// when concept HandlerWithInput and HandlerWithoutInput not cover
|
||||
// all Handler case
|
||||
static_assert(unsupported_handler_v<HandlerType>);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
38
src/rpc/ngHandlers/Ping.h
Normal file
38
src/rpc/ngHandlers/Ping.h
Normal file
@@ -0,0 +1,38 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <rpc/common/Types.h>
|
||||
|
||||
namespace RPCng {
|
||||
|
||||
class PingHandler
|
||||
{
|
||||
public:
|
||||
using Output = VoidOutput;
|
||||
using Result = HandlerReturnType<Output>;
|
||||
|
||||
Result
|
||||
process() const
|
||||
{
|
||||
return Output{};
|
||||
}
|
||||
};
|
||||
} // namespace RPCng
|
||||
Reference in New Issue
Block a user