A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
channel_wire_stream.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
3#ifndef A11_NET_CHANNEL_WIRE_STREAM_H_
4#define A11_NET_CHANNEL_WIRE_STREAM_H_
5
6#include <cstddef>
7#include <functional>
8#include <memory>
9#include <optional>
10#include <string>
11
12#include <absl/base/nullability.h>
13#include <absl/status/status.h>
14#include <absl/status/statusor.h>
15#include <absl/time/time.h>
16
18#include "a11/data/types.h"
20#include "a11/net/wire_stream.h"
21
22namespace a11::net {
23
24namespace internal {
25class BinaryChannel;
26} // namespace internal
27
30
33 // Serialized byte packets never exceed split_size. Complete messages carry
34 // a small suffix; larger messages use length-suffixed first chunks followed
35 // by ordinary chunks, matching Action Engine's byte-chunking protocol.
36 size_t split_size = 64 * 1024;
38 size_t max_pending_bytes = 64 * 1024 * 1024;
39
41 absl::Status Validate() const;
42};
43
54 : public WireStream,
55 public std::enable_shared_from_this<ChannelWireStream> {
56 public:
58 using OpenOperation = std::function<absl::Status()>;
59
62
63 ~ChannelWireStream() override;
64
65 absl::Status Send(data::WireMessage message) override;
66 a11::Task Start(OnMessage on_message, OnDone on_done) override;
67 a11::Task Accept(OnMessage on_message, OnDone on_done) override;
68 absl::Status HalfClose(data::ByteMap trailers) override;
70 absl::Status Abort(absl::Status status) override;
71 absl::Status SetDeadline(absl::Time deadline) override;
72
73 [[nodiscard]] absl::Time deadline() const override;
74 [[nodiscard]] absl::Status GetStatus() const override;
75 [[nodiscard]] std::optional<data::ByteMap> GetTrailers() const override;
76 [[nodiscard]] std::string GetId() const override;
77 [[nodiscard]] void* absl_nullable GetImpl() const override;
78
79 protected:
80 struct State;
81
82 explicit ChannelWireStream(std::shared_ptr<State> state)
83 : state_(std::move(state)) {}
84
85 static absl::StatusOr<std::shared_ptr<State>> MakeState(
86 std::shared_ptr<internal::BinaryChannel> channel, std::string id,
87 ChannelEndpointRole role, OpenOperation open_operation,
88 WireStreamOptions options, ChannelFramingOptions framing = {});
89
90 private:
91 a11::Task StartEndpoint(bool accept, OnMessage on_message, OnDone on_done);
92 static void Sender(std::shared_ptr<State> state);
93 static void Receiver(std::shared_ptr<State> state);
94 static void WatchTiming(std::shared_ptr<State> state);
95 static void MarkActivity(const std::shared_ptr<State>& state);
96 static void ForceAbort(const std::shared_ptr<State>& state,
97 absl::Status status, bool can_communicate = true);
98 static void MaybeFinish(const std::shared_ptr<State>& state);
99 static void Finish(const std::shared_ptr<State>& state,
100 std::optional<absl::Status> terminal_error = std::nullopt);
101 static void Notify(const std::shared_ptr<State>& state);
102
103 std::shared_ptr<State> state_;
104};
105
106} // namespace a11::net
107
108#endif // A11_NET_CHANNEL_WIRE_STREAM_H_
Bounded packetisation and reassembly for binary channel transports.
Shared WireStream lifecycle and framing for binary channels.
Definition channel_wire_stream.h:55
std::optional< data::ByteMap > GetTrailers() const override
Definition channel_wire_stream.cc:513
ChannelWireStream(std::shared_ptr< State > state)
Definition channel_wire_stream.h:82
absl::Status GetStatus() const override
Definition channel_wire_stream.cc:498
absl::Status Abort(absl::Status status) override
Abort the stream, discarding buffered work.
Definition channel_wire_stream.cc:459
a11::Task Start(OnMessage on_message, OnDone on_done) override
Begin the stream as the initiating ("start") side.
Definition channel_wire_stream.cc:238
std::function< absl::Status()> OpenOperation
Transport-specific operation that initiates or accepts the channel.
Definition channel_wire_stream.h:58
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
~ChannelWireStream() override
Definition channel_wire_stream.cc:162
a11::Task DrainOutgoingMessages() override
Await delivery of all buffered outbound messages.
Definition channel_wire_stream.cc:447
std::string GetId() const override
Definition channel_wire_stream.cc:518
absl::Status Send(data::WireMessage message) override
Enqueue a message for delivery to the peer.
Definition channel_wire_stream.cc:177
void *absl_nullable GetImpl() const override
Definition channel_wire_stream.cc:523
a11::Task Accept(OnMessage on_message, OnDone on_done) override
Begin the stream as the accepting ("accept") side.
Definition channel_wire_stream.cc:242
absl::Time deadline() const override
Definition channel_wire_stream.cc:493
static absl::StatusOr< std::shared_ptr< State > > MakeState(std::shared_ptr< internal::BinaryChannel > channel, std::string id, ChannelEndpointRole role, OpenOperation open_operation, WireStreamOptions options, ChannelFramingOptions framing={})
Definition channel_wire_stream.cc:135
A bidirectional, message-oriented channel between two A11 endpoints.
Definition wire_stream.h:89
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
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
ChannelEndpointRole
Handshake role a binary channel endpoint is permitted to assume.
Definition channel_wire_stream.h:29
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
Packetisation and incomplete-message bounds for a binary WireStream.
Definition channel_wire_stream.h:32
size_t max_pending_bytes
Pending inbound byte limit.
Definition channel_wire_stream.h:38
size_t max_pending_messages
Incomplete inbound message limit.
Definition channel_wire_stream.h:37
size_t split_size
Maximum encoded channel packet size.
Definition channel_wire_stream.h:36
absl::Status Validate() const
Validate the framing limits before a transport starts.
Definition channel_wire_stream.cc:122
Definition channel_wire_stream.cc:48
Buffering, sizing, and deadline limits for a WireStream endpoint.
Definition wire_stream.h:44
A11's core wire value types: chunks, node fragments and messages.
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...