Skip to content

Commit

Permalink
fix: Transition register_sender_rpc_handler over to request message f…
Browse files Browse the repository at this point in the history
…actory pattern
  • Loading branch information
Tradias committed Dec 12, 2024
1 parent 80582c6 commit d01059f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
27 changes: 8 additions & 19 deletions example/snippets/server_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,11 @@ class RPCHandlerWithArenaRequestMessageFactory
public:
explicit RPCHandlerWithArenaRequestMessageFactory(Handler handler) : handler_(std::move(handler)) {}

// unary and server-streaming rpcs
template <class ServerRPC, class Request>
decltype(auto) operator()(ServerRPC&& rpc, Request&& request, ArenaRequestMessageFactory&)
template <class... Args>
decltype(auto) operator()(Args&&... args)
{
return handler_(std::forward<ServerRPC>(rpc), std::forward<Request>(request));
}

// client-streaming and bidi-streaming rpcs
template <class ServerRPC>
decltype(auto) operator()(ServerRPC&& rpc)
{
return handler_(std::forward<ServerRPC>(rpc));
// for unary and server-streaming rpcs args are: ServerRPC&, Request&, ArenaRequestMessageFactory&
return handler_(std::forward<Args>(args)...);
}

ArenaRequestMessageFactory request_message_factory() { return {}; }
Expand All @@ -287,16 +280,12 @@ class RPCHandlerWithArenaRequestMessageFactory
Handler handler_;
};

template <class ServerRPC>
void register_rpc_handler(agrpc::GrpcContext& grpc_context, example::v1::Example::AsyncService& service)
template <class ServerRPC, class RPCHandler>
void register_rpc_handler(agrpc::GrpcContext& grpc_context, example::v1::Example::AsyncService& service,
RPCHandler&& handler)
{
agrpc::register_awaitable_rpc_handler<ServerRPC>(
grpc_context, service,
RPCHandlerWithArenaRequestMessageFactory{[&](ServerRPC&, typename ServerRPC::Request&) -> asio::awaitable<void>
{
// ...
co_return;
}},
grpc_context, service, RPCHandlerWithArenaRequestMessageFactory{std::forward<RPCHandler>(handler)},
asio::detached);
}
/* [server-rpc-handler-with-arena] */
17 changes: 10 additions & 7 deletions src/agrpc/detail/register_sender_rpc_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct RPCHandlerOperation
{
using Service = detail::ServerRPCServiceT<ServerRPC>;
using Traits = typename ServerRPC::Traits;
using Starter = detail::ServerRPCStarter<>;
using RequestMessageFactory = detail::ServerRPCRequestMessageFactoryT<ServerRPC, RPCHandler>;
using RPCHandlerInvokeResult = detail::RPCHandlerInvokeResultT<ServerRPC&, RPCHandler&, RequestMessageFactory&>;
using RegisterRPCHandlerSenderOperationBase =
Expand Down Expand Up @@ -206,10 +207,10 @@ struct RPCHandlerOperation
#endif
};

using StartOperationState = detail::InplaceWithFunctionWrapper<exec::connect_result_t<
decltype(detail::ServerRPCStarter<>::start(std::declval<ServerRPC&>(), std::declval<Service&>(),
std::declval<RequestMessageFactory&>(), agrpc::use_sender)),
StartReceiver>>;
using StartOperationState = detail::InplaceWithFunctionWrapper<
exec::connect_result_t<decltype(Starter::start(std::declval<ServerRPC&>(), std::declval<Service&>(),
std::declval<RequestMessageFactory&>(), agrpc::use_sender)),
StartReceiver>>;

template <class Action>
struct Receiver
Expand Down Expand Up @@ -270,7 +271,8 @@ struct RPCHandlerOperation
detail::InplaceWithFunction{},
[&]
{
return starter().start(rpc_, operation.service(), agrpc::use_sender).connect(StartReceiver{*this});
return Starter::start(rpc_, operation.service(), request_message_factory(), agrpc::use_sender)
.connect(StartReceiver{*this});
})
{
base().increment_ref_count();
Expand Down Expand Up @@ -304,7 +306,8 @@ struct RPCHandlerOperation
detail::InplaceWithFunction{},
[&]
{
return exec::connect(starter().invoke(rpc_handler(), rpc_), FinishReceiver{*this});
return exec::connect(Starter::invoke(rpc_handler(), rpc_, request_message_factory()),
FinishReceiver{*this});
});
return {};
}
Expand All @@ -331,7 +334,7 @@ struct RPCHandlerOperation

auto& rpc_handler() noexcept { return base().rpc_handler(); }

auto& starter() noexcept { return impl1_.second(); }
auto& request_message_factory() noexcept { return impl1_.second(); }

auto& operation_state() noexcept { return impl2_.first(); }

Expand Down

0 comments on commit d01059f

Please sign in to comment.