morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
connection_writer.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <string>
8
9/// @file
10/// @brief What a transport hands `RemoteServer` to write to one connection,
11/// and the backlog bounds both sides apply.
12///
13/// Kept apart from `remote.hpp` so a transport's connection code, which both
14/// the client and the server role share, can use the bounds without compiling
15/// the server.
16
17namespace morph::backend {
18
19/// @brief Opaque id a transport uses to scope model registrations to one
20/// connection so `RemoteServer::closeConnection` can reclaim them.
21///
22/// `0` is reserved and means *unscoped*: it is what the two-argument
23/// `RemoteServer::handle()`/`handleInline()` overloads pass. Non-zero values
24/// are minted by `RemoteServer::openConnection()`.
25using ConnectionId = std::uint64_t;
26
27/// @brief A transport's outbound side of one connection, for what the server
28/// sends unasked: events.
29///
30/// Handed to `RemoteServer::setConnectionWriter`. Both callables run on the
31/// server strand, so neither may block: `write` queues the frame on the
32/// transport's own thread, as a reply is queued.
34 /// @brief Queues one encoded envelope for the connection. May throw when
35 /// the connection can no longer be written; the server catches it.
36 std::function<void(std::string)> write;
37 /// @brief Bytes of encoded envelopes handed to the transport for this
38 /// connection, replies and events alike, and not yet written.
39 /// Empty reads as `0`. May throw; the server catches it and writes
40 /// nothing.
41 std::function<std::size_t()> backlog;
42};
43
44/// @brief Backlog above which `RemoteServer::post` holds a connection's events
45/// back and owes it one `resync` instead: 1 MiB.
46inline constexpr std::size_t kEventBacklogHigh = std::size_t{1} << 20;
47
48/// @brief Backlog below which a transport reports a connection drained, by
49/// calling `RemoteServer::connectionDrained`: 256 KiB.
50///
51/// Well under `kEventBacklogHigh`, so a reader hovering at the bound does not
52/// flip between held back and drained on every frame.
53inline constexpr std::size_t kEventBacklogLow = std::size_t{256} << 10;
54
55} // namespace morph::backend
constexpr std::size_t kEventBacklogHigh
Backlog above which RemoteServer::post holds a connection's events back and owes it one resync instea...
std::uint64_t ConnectionId
Opaque id a transport uses to scope model registrations to one connection so RemoteServer::closeConne...
constexpr std::size_t kEventBacklogLow
Backlog below which a transport reports a connection drained, by calling RemoteServer::connectionDrai...
A transport's outbound side of one connection, for what the server sends unasked: events.
std::function< void(std::string)> write
Queues one encoded envelope for the connection. May throw when the connection can no longer be writte...
std::function< std::size_t()> backlog
Bytes of encoded envelopes handed to the transport for this connection, replies and events alike,...