185 lines
4.9 KiB
C++
185 lines
4.9 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
#include <Void/Net/HttpServer.hpp>
|
|
#include <Void/RPC/Codec.hpp>
|
|
#include <Void/RPC/Types.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::RPC
|
|
{
|
|
using OwnedBytes = std::vector<std::byte>;
|
|
|
|
struct CallContext {
|
|
CallId call_id{};
|
|
MethodDescriptor method{};
|
|
PrincipalView principal{};
|
|
Void::Time::MonotonicClock::time_point deadline{};
|
|
const Void::Time::MonotonicClock* clock{
|
|
&Void::Time::SystemMonotonicClock::instance()};
|
|
std::vector<MetadataEntry> metadata{};
|
|
std::stop_token stop{};
|
|
|
|
[[nodiscard]] std::chrono::milliseconds
|
|
remaining_timeout() const noexcept
|
|
{
|
|
const auto rem = Void::Time::remaining_until(*clock, deadline);
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
rem);
|
|
}
|
|
};
|
|
|
|
struct CallOptions {
|
|
std::chrono::milliseconds timeout{std::chrono::seconds{10}};
|
|
std::vector<MetadataEntry> metadata{};
|
|
std::vector<Void::Net::Header> transport_headers{};
|
|
std::stop_token stop{};
|
|
std::uint32_t schema_version{1};
|
|
};
|
|
|
|
using UnaryHandler =
|
|
std::function<Void::Result::Result<OwnedBytes>(const CallContext&,
|
|
OwnedBytes)>;
|
|
|
|
struct AuthHook {
|
|
std::function<Void::Result::Result<PrincipalView>(
|
|
const std::vector<Void::Net::Header>& transport_headers,
|
|
const std::vector<MetadataEntry>& metadata)>
|
|
authenticate{};
|
|
|
|
std::function<Void::Result::Result<void>(
|
|
const MethodDescriptor& method, const PrincipalView& principal)>
|
|
authorize{};
|
|
};
|
|
|
|
/// Move-only RAII method registration handle. Unregistering stops new
|
|
/// admission for the method and drains already-admitted calls before
|
|
/// returning.
|
|
class Registration final
|
|
{
|
|
public:
|
|
|
|
Registration() noexcept = default;
|
|
~Registration() noexcept;
|
|
|
|
Registration(Registration&& other) noexcept;
|
|
Registration& operator=(Registration&& other) noexcept;
|
|
Registration(const Registration&) = delete;
|
|
Registration& operator=(const Registration&) = delete;
|
|
|
|
[[nodiscard]] bool active() const noexcept;
|
|
void unregister_and_drain() noexcept;
|
|
|
|
private:
|
|
|
|
friend class Server;
|
|
struct Entry;
|
|
std::shared_ptr<void> m_server_state{};
|
|
MethodId m_id{};
|
|
std::uint64_t m_reg_gen{0};
|
|
bool m_active{false};
|
|
};
|
|
|
|
struct ServerOptions {
|
|
Void::Net::HttpServerOptions http_options{};
|
|
RpcLimits limits{};
|
|
AuthHook auth{};
|
|
const Void::Time::MonotonicClock* clock{
|
|
&Void::Time::SystemMonotonicClock::instance()};
|
|
};
|
|
|
|
/// Ownership-safe unary HTTP/1.1 RPC server implementing `/void-rpc/v1/call`.
|
|
class Server final
|
|
{
|
|
public:
|
|
|
|
explicit Server(ServerOptions options = {});
|
|
~Server() noexcept;
|
|
|
|
Server(Server&&) noexcept;
|
|
Server& operator=(Server&&) noexcept;
|
|
Server(const Server&) = delete;
|
|
Server& operator=(const Server&) = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<Registration>
|
|
bind(MethodDescriptor descriptor, UnaryHandler handler);
|
|
|
|
/// Mount `/void-rpc/v1/call` onto an external HttpServer (or call
|
|
/// `start()` to run the owned HttpServer directly).
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
mount(Void::Net::HttpServer& http_server);
|
|
|
|
[[nodiscard]] Void::Result::Result<void> start();
|
|
|
|
void shutdown(std::chrono::milliseconds drain_timeout =
|
|
std::chrono::seconds{10}) noexcept;
|
|
|
|
[[nodiscard]] bool running() const noexcept;
|
|
[[nodiscard]] std::uint16_t bound_port() const noexcept;
|
|
[[nodiscard]] std::string base_url() const;
|
|
|
|
/// Direct in-process Wire v1 dispatch (useful for deterministic
|
|
/// unit/fault testing without TCP sockets).
|
|
[[nodiscard]] Void::Result::Result<OwnedBytes>
|
|
handle_raw_envelope(
|
|
std::span<const std::byte> request_bytes,
|
|
const std::vector<Void::Net::Header>& transport_headers =
|
|
{});
|
|
|
|
private:
|
|
|
|
friend class Registration;
|
|
struct State;
|
|
std::shared_ptr<State> m_state;
|
|
};
|
|
|
|
struct ClientOptions {
|
|
std::string endpoint_base_url{};
|
|
Void::Net::HttpClientOptions http_options{
|
|
.verify_tls = true,
|
|
};
|
|
RpcLimits limits{};
|
|
const Void::Time::MonotonicClock* clock{
|
|
&Void::Time::SystemMonotonicClock::instance()};
|
|
};
|
|
|
|
/// Unary HTTP/1.1 RPC client encoding Wire v1 envelopes over
|
|
/// `Void::Net::HttpClient`.
|
|
class Client final
|
|
{
|
|
public:
|
|
|
|
explicit Client(ClientOptions options);
|
|
~Client() noexcept;
|
|
|
|
Client(Client&&) noexcept;
|
|
Client& operator=(Client&&) noexcept;
|
|
Client(const Client&) = delete;
|
|
Client& operator=(const Client&) = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<OwnedBytes>
|
|
call(MethodId method, OwnedBytes payload,
|
|
const CallOptions& options = {});
|
|
|
|
[[nodiscard]] std::size_t in_flight_calls() const noexcept;
|
|
|
|
private:
|
|
|
|
struct State;
|
|
std::shared_ptr<State> m_state;
|
|
};
|
|
|
|
} // namespace Void::RPC
|