A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
wire_stream_with_recv.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
3#ifndef A11_NET_WIRE_STREAM_WITH_RECV_H_
4#define A11_NET_WIRE_STREAM_WITH_RECV_H_
5
6#include <memory>
7#include <optional>
8#include <string>
9
10#include <absl/base/nullability.h>
11#include <absl/status/status.h>
12#include <absl/status/statusor.h>
13#include <absl/time/time.h>
14
16#include "a11/data/types.h"
17#include "a11/net/wire_stream.h"
18
19namespace a11::net {
20
30 : public WireStream,
31 public std::enable_shared_from_this<WireStreamWithRecv> {
32 private:
33 struct State;
34
35 struct ConstructorToken {};
36
37 public:
39 static absl::StatusOr<std::shared_ptr<WireStreamWithRecv>> Create(
40 std::shared_ptr<WireStream> stream);
41
44
45 absl::Status Send(data::WireMessage message) override;
46 a11::Task Start(OnMessage on_message, OnDone on_done) override;
47 a11::Task Accept(OnMessage on_message, OnDone on_done) override;
52 absl::Status HalfClose(data::ByteMap trailers) override;
54 absl::Status Abort(absl::Status status) override;
55 absl::Status SetDeadline(absl::Time deadline) override;
56
57 [[nodiscard]] absl::Time deadline() const override;
58 [[nodiscard]] absl::Status GetStatus() const override;
59 [[nodiscard]] std::optional<data::ByteMap> GetTrailers() const override;
60 [[nodiscard]] std::string GetId() const override;
61 [[nodiscard]] void* absl_nullable GetImpl() const override;
62
68 absl::Duration timeout = absl::InfiniteDuration());
69
71 [[nodiscard]] std::shared_ptr<WireStream> wrapped_stream() const {
72 return stream_;
73 }
74
75 WireStreamWithRecv(ConstructorToken, std::shared_ptr<WireStream> stream,
76 std::string id, std::shared_ptr<State> state)
77 : stream_(std::move(stream)),
78 id_(std::move(id)),
79 state_(std::move(state)) {}
80
81 private:
82 a11::Task StartImpl(bool accept, OnMessage on_message, OnDone on_done);
83 a11::Task HandleMessage(OnMessage observer,
84 std::optional<data::WireMessage> message);
85 a11::Task HandleDone(OnDone observer);
86 void RecordCurrentStatus() const;
87 void SignalError(absl::Status status) const;
88
89 std::shared_ptr<WireStream> stream_;
90 std::string id_;
91 std::shared_ptr<State> state_;
92};
93
94} // namespace a11::net
95
96#endif // A11_NET_WIRE_STREAM_WITH_RECV_H_
Pull-oriented adapter for a callback-driven WireStream.
Definition wire_stream_with_recv.h:31
absl::Status SetDeadline()
Clear any deadline (equivalent to an infinite deadline).
Definition wire_stream.h:154
a11::Task DrainOutgoingMessages() override
Await delivery of all buffered outbound messages.
Definition wire_stream_with_recv.cc:128
absl::Status HalfClose()
Half-close with no trailers.
Definition wire_stream.h:125
std::shared_ptr< WireStream > wrapped_stream() const
Return the underlying callback-oriented stream.
Definition wire_stream_with_recv.h:71
absl::Status GetStatus() const override
Definition wire_stream_with_recv.cc:173
absl::Status Abort(absl::Status status) override
Abort the stream, discarding buffered work.
Definition wire_stream_with_recv.cc:139
a11::Task Accept()
Accept the wrapped stream and route inbound messages to Receive().
Definition wire_stream_with_recv.cc:90
std::optional< data::ByteMap > GetTrailers() const override
Definition wire_stream_with_recv.cc:183
void *absl_nullable GetImpl() const override
Definition wire_stream_with_recv.cc:195
a11::Task Start()
Start the wrapped stream and route inbound messages to Receive().
Definition wire_stream_with_recv.cc:86
absl::Time deadline() const override
Definition wire_stream_with_recv.cc:165
WireStreamWithRecv(ConstructorToken, std::shared_ptr< WireStream > stream, std::string id, std::shared_ptr< State > state)
Definition wire_stream_with_recv.h:75
std::string GetId() const override
Definition wire_stream_with_recv.cc:191
absl::Status Send(data::WireMessage message) override
Enqueue a message for delivery to the peer.
Definition wire_stream_with_recv.cc:68
static absl::StatusOr< std::shared_ptr< WireStreamWithRecv > > Create(std::shared_ptr< WireStream > stream)
Wrap stream, preserving its id, lifecycle, and transport handle.
Definition wire_stream_with_recv.cc:51
a11::Future< std::optional< data::WireMessage > > Receive(absl::Duration timeout=absl::InfiniteDuration())
Await one inbound message, or nullopt after the peer half-closes.
Definition wire_stream_with_recv.cc:203
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
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
Definition wire_stream_with_recv.cc:40
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...