OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
blocking_server.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_RPC_BLOCKING_SERVER_H_
7#define OPENCBDC_TX_SRC_RPC_BLOCKING_SERVER_H_
8
9#include "server.hpp"
10
11namespace cbdc::rpc {
21 template<typename Request,
22 typename Response,
23 typename InBuffer = buffer,
24 typename OutBuffer = buffer>
26 : public server<Request, Response, InBuffer, OutBuffer> {
27 public:
28 blocking_server() = default;
29 blocking_server(blocking_server&&) noexcept = default;
30 auto operator=(blocking_server&&) noexcept
31 -> blocking_server& = default;
33 auto operator=(const blocking_server&) -> blocking_server& = default;
34
35 ~blocking_server() override = default;
36
38
42 using callback_type = std::function<std::optional<Response>(Request)>;
43
49 m_callback = std::move(callback);
50 }
51
52 protected:
58 auto blocking_call(InBuffer request_buf) -> std::optional<OutBuffer> {
59 auto req = server_type::deserialize_request(request_buf);
60 if(!req.has_value()) {
61 return std::nullopt;
62 }
63 auto resp = std::optional<Response>();
64 if(m_callback) {
65 resp = m_callback(std::move(req.value().m_payload));
66 }
68 std::move(req.value().m_header),
69 std::move(resp));
70 }
71
72 private:
73 callback_type m_callback;
74
76 };
77}
78
79#endif
Generic synchronous RPC server.
auto blocking_call(InBuffer request_buf) -> std::optional< OutBuffer >
Synchronously deserializes an RPC request, calls the request handler function, then serializes and re...
static constexpr handler_type handler
std::function< std::optional< Response >(Request)> callback_type
Handler callback function type which accepts a request and returns a response, or returns std::nullop...
blocking_server(blocking_server &&) noexcept=default
void register_handler_callback(callback_type callback)
Register a handler callback function for processing requests and returning responses.
Generic RPC server.
auto serialize_response(header request_header, std::optional< R > response_payload) -> OutBuffer
Serialize a response into a buffer.
auto deserialize_request(BufType &request_buf) -> std::optional< request_type >
Deserializes a request from a buffer.
handler_type
Type to distinguish between servers that implement synchronous versus asynchronous request handling.
@ buffer
A singular RLP value (byte array)