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 {
31template <
typename Packet>
33 :
public Implement<PacketReaderWriter<Packet>> {
36 : read_queue_(allocator), staged_(allocator), written_(allocator) {}
43 read_queue_.push_back(std::move(packet));
44 std::move(read_waker_).Wake();
51 if (read_queue_.empty()) {
53 cx, read_waker_,
"TestPacketReaderWriter::DoPendRead");
58 read_queue_.pop_front();
62 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx,
63 size_t count)
override {
67 cx, write_waker(),
"TestPacketReaderWriter::DoPendReadyToWrite");
71 static_cast<typename decltype(staged_)::size_type
>(count));
75 void DoStageWrite(Packet&& packet)
override {
77 staged_.push_back(std::move(packet));
80 async2::Poll<> DoPendWrite(async2::Context&)
override {
85 async2::Poll<Status> DoPendClose(async2::Context&)
override {
92 auto it = std::make_move_iterator(staged_.begin());
93 const auto end = std::make_move_iterator(staged_.end());
99 std::move(write_waker()).Wake();
102 async2::Waker read_waker_;
103 DynamicDeque<Packet> read_queue_;
104 DynamicDeque<Packet> staged_;
105 DynamicVector<Packet> written_;
Definition: allocator.h:36
Definition: packet_channel.h:222
Definition: channel.h:583
pw::channel::PacketReadWriter implementation for testing use.
Definition: test_packet_channel.h:33
constexpr size_type size() const noexcept
Returns the number of elements in the deque.
Definition: generic_deque.h:64
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:69
Definition: span_impl.h:235
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
#define PW_ASYNC_STORE_WAKER(context, waker_or_queue_out, wait_reason_string)
Definition: waker.h:60
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
void EnqueueReadPacket(Packet &&packet)
Enqueues packets to be returned from future PendRead calls.
Definition: test_packet_channel.h:42
span< const Packet > written_packets() const
Returns all packets that have been written to this packet channel.
Definition: test_packet_channel.h:39
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:167
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:261
void push_back(const value_type &value)
Definition: dynamic_vector.h:260
constexpr Status OkStatus()
Definition: status.h:297