18#include "pw_allocator/allocator.h"
19#include "pw_assert/assert.h"
20#include "pw_async2/waker.h"
21#include "pw_channel/packet_channel.h"
22#include "pw_containers/dynamic_deque.h"
23#include "pw_containers/dynamic_vector.h"
24#include "pw_span/span.h"
26namespace pw::channel {
29template <
typename Packet>
31 :
public Implement<PacketReaderWriter<Packet>> {
34 : read_queue_(allocator), staged_(allocator), written_(allocator) {}
41 read_queue_.push_back(std::move(packet));
42 std::move(read_waker_).Wake();
49 if (read_queue_.empty()) {
51 cx, read_waker_,
"TestPacketReaderWriter::DoPendRead");
52 return async2::Pending();
55 async2::Ready(std::move(read_queue_.front()));
56 read_queue_.pop_front();
60 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx,
61 size_t count)
override {
65 cx, write_waker(),
"TestPacketReaderWriter::DoPendReadyToWrite");
66 return async2::Pending();
69 static_cast<typename decltype(staged_)::size_type
>(count));
73 void DoStageWrite(Packet&& packet)
override {
75 staged_.push_back(std::move(packet));
78 async2::Poll<> DoPendWrite(async2::Context&)
override {
80 return async2::Ready();
83 async2::Poll<Status> DoPendClose(async2::Context&)
override {
90 auto it = std::make_move_iterator(staged_.begin());
91 const auto end = std::make_move_iterator(staged_.end());
97 std::move(write_waker()).Wake();
100 async2::Waker read_waker_;
101 DynamicDeque<Packet> read_queue_;
102 DynamicDeque<Packet> staged_;
103 DynamicVector<Packet> written_;
Definition: allocator.h:34
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:162
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:256
void push_back(const value_type &value)
Definition: dynamic_vector.h:255
Definition: packet_channel.h:217
Definition: channel.h:583
pw::channel::PacketReadWriter implementation for testing use.
Definition: test_packet_channel.h:31
void EnqueueReadPacket(Packet &&packet)
Enqueues packets to be returned from future PendRead calls.
Definition: test_packet_channel.h:40
span< const Packet > written_packets() const
Returns all packets that have been written to this packet channel.
Definition: test_packet_channel.h:37
constexpr size_type size() const noexcept
Returns the number of elements in the deque.
Definition: generic_deque.h:58
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:63
constexpr Status OkStatus()
Definition: status.h:234