C/C++ API Reference
Loading...
Searching...
No Matches
loopback_channel.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstdint>
17#include <optional>
18
19#include "pw_async2/dispatcher.h"
20#include "pw_async2/poll.h"
21#include "pw_channel/channel.h"
22#include "pw_multibuf/allocator.h"
23#include "pw_multibuf/allocator_async.h"
24#include "pw_multibuf/multibuf.h"
25
26namespace pw::channel {
27
29
32
33// Channel implementation which will read its own writes.
34template <DataType kType>
36
39
42
44
45template <>
46class LoopbackChannel<DataType::kDatagram>
47 : public Implement<ReliableDatagramReaderWriter> {
48 public:
50 : write_alloc_future_(write_allocator) {}
51 LoopbackChannel(const LoopbackChannel&) = delete;
52 LoopbackChannel& operator=(const LoopbackChannel&) = delete;
53
55 LoopbackChannel& operator=(LoopbackChannel&&) = default;
56
57 private:
59 async2::Context& cx) override;
60
61 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx) final;
62
63 async2::PollOptional<multibuf::MultiBuf> DoPendAllocateWriteBuffer(
64 async2::Context& cx, size_t min_bytes) final {
65 write_alloc_future_.SetDesiredSize(min_bytes);
66 return write_alloc_future_.Pend(cx);
67 }
68
69 Status DoStageWrite(multibuf::MultiBuf&& data) final;
70
71 async2::Poll<Status> DoPendWrite(async2::Context&) final;
72
73 async2::Poll<Status> DoPendClose(async2::Context&) final;
74
75 multibuf::MultiBufAllocationFuture write_alloc_future_;
76 std::optional<multibuf::MultiBuf> queue_;
77
78 async2::Waker waker_;
79};
80
81template <>
82class LoopbackChannel<DataType::kByte>
83 : public Implement<ReliableByteReaderWriter> {
84 public:
86 : write_alloc_future_(write_allocator) {}
87 LoopbackChannel(const LoopbackChannel&) = delete;
88 LoopbackChannel& operator=(const LoopbackChannel&) = delete;
89
91 LoopbackChannel& operator=(LoopbackChannel&&) = default;
92
93 private:
95 async2::Context& cx) override;
96
97 async2::Poll<Status> DoPendReadyToWrite(async2::Context&) final {
98 return async2::Ready(OkStatus());
99 }
100
101 async2::PollOptional<multibuf::MultiBuf> DoPendAllocateWriteBuffer(
102 async2::Context& cx, size_t min_bytes) final {
103 write_alloc_future_.SetDesiredSize(min_bytes);
104 return write_alloc_future_.Pend(cx);
105 }
106
107 Status DoStageWrite(multibuf::MultiBuf&& data) final;
108
109 async2::Poll<Status> DoPendWrite(async2::Context&) final;
110
111 async2::Poll<Status> DoPendClose(async2::Context&) final;
112
113 multibuf::MultiBufAllocationFuture write_alloc_future_;
114 multibuf::MultiBuf queue_;
115
116 async2::Waker read_waker_;
117};
118
119} // namespace pw::channel
Definition: status.h:109
Definition: context.h:55
Definition: poll.h:60
Definition: waker.h:160
Definition: channel.h:583
Definition: loopback_channel.h:83
Definition: loopback_channel.h:35
Definition: allocator_async.h:93
Definition: allocator.h:57
Definition: multibuf_v1.h:248
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
constexpr Status OkStatus()
Definition: status.h:297