A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
byte_chunking.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
11#ifndef A11_NET_BYTE_CHUNKING_H_
12#define A11_NET_BYTE_CHUNKING_H_
13
14#include <cstddef>
15#include <cstdint>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <vector>
20
21#include <absl/base/nullability.h>
22#include <absl/status/status.h>
23#include <absl/status/statusor.h>
24
25namespace a11::net {
26
28enum class BytePacketType : std::uint8_t {
29 kCompleteBytes = 0x00,
30 kByteChunk = 0x01,
32};
33
35struct BytePacket {
37 std::string payload;
38 std::uint64_t transient_id = 0;
39 std::uint32_t sequence = 0;
40 std::uint32_t packet_count =
41 0;
42
43 template <typename Sink>
44 friend void AbslStringify(Sink& sink, const BytePacket& packet) {
45 sink.Append("BytePacket{type=");
46 sink.Append(packet.type == BytePacketType::kCompleteBytes ? "complete"
47 : packet.type == BytePacketType::kByteChunk ? "chunk"
48 : "first_chunk");
49 sink.Append(", id=");
50 sink.Append(packet.transient_id);
51 sink.Append(", sequence=");
52 sink.Append(packet.sequence);
53 sink.Append(", packet_count=");
54 sink.Append(packet.packet_count);
55 sink.Append(", payload_size=");
56 sink.Append(packet.payload.size());
57 sink.Append("}");
58 }
59};
60
63 size_t packet_size = 64 * 1024;
64 size_t max_message_size = 32 * 1024 * 1024;
67 64 * 1024 * 1024;
68
70 absl::Status Validate() const;
71};
72
74absl::StatusOr<std::vector<std::string>> SplitBytesIntoPackets(
75 std::string_view bytes, std::uint64_t transient_id, size_t packet_size);
77absl::StatusOr<BytePacket> ParseBytePacket(std::string_view packet);
78
88 public:
90 explicit ByteReassembler(ByteChunkingOptions options);
92
95
97 absl::StatusOr<std::optional<std::string>> Feed(std::string packet);
99 void Clear();
100
102 [[nodiscard]] size_t pending_message_count() const;
104 [[nodiscard]] size_t pending_byte_count() const;
105
106 private:
107 struct Impl;
108 static constexpr size_t kImplSize = 256;
109 static constexpr size_t kImplAlignment = alignof(std::max_align_t);
110
111 Impl* absl_nonnull GetImpl();
112 const Impl* absl_nonnull GetImpl() const;
113
114 alignas(kImplAlignment) std::byte impl_[kImplSize];
115};
116
117} // namespace a11::net
118
119#endif // A11_NET_BYTE_CHUNKING_H_
Bounded, thread-safe reassembly for interleaved binary messages.
Definition byte_chunking.h:87
size_t pending_message_count() const
Number of message ids currently awaiting more packets.
Definition byte_chunking.cc:334
ByteReassembler(const ByteReassembler &)=delete
ByteReassembler & operator=(const ByteReassembler &)=delete
absl::StatusOr< std::optional< std::string > > Feed(std::string packet)
Admit one packet and return a complete message when this finishes one.
Definition byte_chunking.cc:224
size_t pending_byte_count() const
Aggregate payload bytes retained by incomplete messages.
Definition byte_chunking.cc:340
~ByteReassembler()
Definition byte_chunking.cc:212
void Clear()
Discard every incomplete message, for example when a channel aborts.
Definition byte_chunking.cc:327
Definition action.h:46
absl::StatusOr< BytePacket > ParseBytePacket(std::string_view packet)
Parse and validate one packet without retaining the input view.
Definition byte_chunking.cc:142
BytePacketType
Packet shapes in the Action Engine byte-chunking wire format.
Definition byte_chunking.h:28
absl::StatusOr< std::vector< std::string > > SplitBytesIntoPackets(std::string_view bytes, std::uint64_t transient_id, size_t packet_size)
Split bytes into Action Engine packets with fixed little-endian suffixes.
Definition byte_chunking.cc:103
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
Bounds packet size and incomplete-message memory during reassembly.
Definition byte_chunking.h:62
size_t packet_size
Maximum encoded packet size.
Definition byte_chunking.h:63
size_t max_pending_messages
Simultaneous incomplete messages.
Definition byte_chunking.h:65
size_t max_message_size
Reassembled message limit.
Definition byte_chunking.h:64
size_t max_pending_bytes
Aggregate pending payload limit.
Definition byte_chunking.h:66
absl::Status Validate() const
Validate that all limits can represent at least one useful packet.
Definition byte_chunking.cc:86
Parsed packet metadata plus the owned piece of application payload.
Definition byte_chunking.h:35
std::uint32_t sequence
Zero-based position in the message.
Definition byte_chunking.h:39
BytePacketType type
Packet shape.
Definition byte_chunking.h:36
std::uint32_t packet_count
Total count, when supplied by the first packet.
Definition byte_chunking.h:40
std::string payload
Bytes contributed by this packet.
Definition byte_chunking.h:37
std::uint64_t transient_id
Id used to interleave messages safely.
Definition byte_chunking.h:38
friend void AbslStringify(Sink &sink, const BytePacket &packet)
Definition byte_chunking.h:44
Definition byte_chunking.cc:190