1#ifndef joedb_rpc_Server_declared
2#define joedb_rpc_Server_declared
20 const std::vector<Signature> &signatures;
21 const std::vector<Procedure *> &procedures;
22 const int64_t max_message_size;
31 boost::asio::awaitable<bool> handshake()
37 const bool correct_hash = hash ==
get_hash(get_server().signatures);
44 log(correct_hash ?
"correct hash" :
"incorrect hash");
47 co_return correct_hash;
51 boost::asio::awaitable<void> call()
56 const size_t id =
static_cast<size_t>(
buffer.
read<int64_t>());
62 if (
id >= get_server().procedures.size())
65 auto &signature = get_server().signatures[
id];
66 auto &procedure = *get_server().procedures[
id];
69 log(
"procedure[" + std::to_string(
id) +
"]: " + signature.name);
76 if (until < int64_t(signature.prolog.size()) || until > get_server().max_message_size)
81 data.reserve(
size_t(until));
82 data = signature.prolog;
84 int64_t remaining = until - file.
get_size();
104 procedure.execute(file);
106 catch (
const std::exception &e)
108 const std::string_view message(e.what());
111 log(
"error: " + std::string(message));
113 const size_t n = std::min(message.size(),
buffer.
size - 9);
131 size_t offset = size_t(until);
132 size_t remaining = file.
get_data().size() - offset;
152 boost::asio::local::stream_protocol::socket &&
socket
159 boost::asio::awaitable<void>
run() override
162 if (!
co_await handshake())
195 boost::asio::local::stream_protocol::socket &&socket
198 return std::make_unique<Session>(*
this, std::move(socket));
210 const std::vector<Signature> &signatures,
211 const std::vector<Procedure *> &procedures,
212 int64_t max_message_size = 1 << 24
221 signatures(signatures),
222 procedures(procedures),
223 max_message_size(max_message_size)
char data[size+extra_size]
static constexpr size_t size
size_t pread(char *buffer, size_t size, int64_t offset) const override
Read a range of bytes.
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
std::array< uint32_t, 8 > Hash
boost::asio::awaitable< void > write_buffer()
boost::asio::awaitable< size_t > read_buffer(size_t offset, size_t size)
void log(beman::cstring_view message) noexcept override
boost::asio::local::stream_protocol::socket socket
Superclass for asio servers.
const std::string endpoint_path
int get_log_level() const
boost::asio::awaitable< void > run() override
std::unique_ptr< joedb::asio::Server::Session > new_session(boost::asio::local::stream_protocol::socket &&socket) override
SHA_256::Hash get_hash(const std::vector< Signature > &signatures)
Compute hash code for a collection of procedure signatures.