A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
wire_stream.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
15#ifndef A11_NET_WIRE_STREAM_H_
16#define A11_NET_WIRE_STREAM_H_
17
18#include <cstddef>
19#include <functional>
20#include <optional>
21#include <string>
22
23#include <absl/base/nullability.h>
24#include <absl/status/status.h>
25#include <absl/time/time.h>
26
28#include "a11/data/types.h"
29
30namespace a11::net {
31
33inline constexpr std::string_view kAbortStatusHeader = "x-a11-abort-status";
35inline constexpr size_t kMaxSingleMessageSize = 32 * 1024 * 1024;
36
50 size_t max_buffered_incoming_bytes = 32 * 1024 * 1024;
53 absl::Duration message_timeout = absl::InfiniteDuration();
55 absl::Time deadline = absl::InfiniteFuture();
56
58 absl::Status Validate() const;
59};
60
64using OnMessage =
65 std::function<a11::Task(std::optional<data::WireMessage> message)>;
67using OnDone = std::function<a11::Task()>;
68
90 public:
91 virtual ~WireStream() = default;
92
104 virtual absl::Status Send(data::WireMessage message) = 0;
105
113 virtual a11::Task Start(OnMessage on_message, OnDone on_done) = 0;
114
122 virtual a11::Task Accept(OnMessage on_message, OnDone on_done) = 0;
123
125 absl::Status HalfClose() { return HalfClose(data::ByteMap{}); }
126
137 virtual absl::Status HalfClose(data::ByteMap trailers) = 0;
138
145
151 virtual absl::Status Abort(absl::Status status) = 0;
152
154 absl::Status SetDeadline() { return SetDeadline(absl::InfiniteFuture()); }
155
160 virtual absl::Status SetDeadline(absl::Time deadline) = 0;
161
163 [[nodiscard]] virtual absl::Time deadline() const = 0;
165 [[nodiscard]] virtual absl::Status GetStatus() const = 0;
167 [[nodiscard]] virtual std::optional<data::ByteMap> GetTrailers() const = 0;
169 [[nodiscard]] virtual std::string GetId() const = 0;
172 [[nodiscard]] virtual void* absl_nullable GetImpl() const = 0;
173};
174
177absl::StatusOr<data::ByteMap> NormalizeWireHeaders(data::ByteMap headers);
178
179} // namespace a11::net
180
181#endif // A11_NET_WIRE_STREAM_H_
A bidirectional, message-oriented channel between two A11 endpoints.
Definition wire_stream.h:89
virtual std::optional< data::ByteMap > GetTrailers() const =0
virtual ~WireStream()=default
virtual a11::Task Accept(OnMessage on_message, OnDone on_done)=0
Begin the stream as the accepting ("accept") side.
virtual void *absl_nullable GetImpl() const =0
absl::Status SetDeadline()
Clear any deadline (equivalent to an infinite deadline).
Definition wire_stream.h:154
absl::Status HalfClose()
Half-close with no trailers.
Definition wire_stream.h:125
virtual a11::Task DrainOutgoingMessages()=0
Await delivery of all buffered outbound messages.
virtual absl::Status Send(data::WireMessage message)=0
Enqueue a message for delivery to the peer.
virtual absl::Status SetDeadline(absl::Time deadline)=0
Set the absolute deadline after which the stream is aborted.
virtual absl::Status HalfClose(data::ByteMap trailers)=0
Signal that this side will send no more messages.
virtual absl::Time deadline() const =0
virtual a11::Task Start(OnMessage on_message, OnDone on_done)=0
Begin the stream as the initiating ("start") side.
virtual absl::Status GetStatus() const =0
virtual absl::Status Abort(absl::Status status)=0
Abort the stream, discarding buffered work.
virtual std::string GetId() const =0
Completion values used by every asynchronous A11 operation.
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:42
Definition action.h:46
std::function< a11::Task(std::optional< data::WireMessage > message)> OnMessage
Called for each inbound message; std::nullopt signals the peer half-closed.
Definition wire_stream.h:65
std::function< a11::Task()> OnDone
Called once, when the stream has fully finished (cleanly or via abort).
Definition wire_stream.h:67
constexpr size_t kMaxSingleMessageSize
Hard ceiling on the size of a single reassembled inbound WireMessage.
Definition wire_stream.h:35
absl::StatusOr< data::ByteMap > NormalizeWireHeaders(data::ByteMap headers)
Validate and case-normalise a wire header map, returning the normalised copy or a non-OK status if a ...
Definition wire_stream.cc:40
constexpr std::string_view kAbortStatusHeader
Trailer key under which an aborting endpoint reports its terminal status.
Definition wire_stream.h:33
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:403
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
The top-level frame exchanged between two A11 endpoints.
Definition types.h:275
Buffering, sizing, and deadline limits for a WireStream endpoint.
Definition wire_stream.h:44
size_t max_single_message_size
Reject any single reassembled message larger than this.
Definition wire_stream.h:48
size_t max_buffered_incoming_messages
Maximum inbound messages buffered before backpressure is applied.
Definition wire_stream.h:46
absl::Duration message_timeout
Fail a message that cannot be delivered within this duration (infinite = no per-message timeout).
Definition wire_stream.h:53
absl::Status Validate() const
Validate the option values, returning a non-OK status if inconsistent.
Definition wire_stream.cc:16
size_t max_buffered_incoming_bytes
Maximum total bytes of buffered inbound messages.
Definition wire_stream.h:50
absl::Time deadline
Absolute deadline after which the stream is aborted.
Definition wire_stream.h:55
A11's core wire value types: chunks, node fragments and messages.