Pigweed
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
26
namespace
pw::channel {
27
30
31
// Channel implementation which will read its own writes.
32
template
<DataType kType>
33
class
LoopbackChannel
;
34
36
using
LoopbackDatagramChannel
=
LoopbackChannel<DataType::kDatagram>
;
37
39
using
LoopbackByteChannel
=
LoopbackChannel<DataType::kByte>
;
40
42
43
template
<>
44
class
LoopbackChannel
<DataType::kDatagram>
45
:
public
Implement
<ReliableDatagramReaderWriter> {
46
public
:
47
LoopbackChannel
(
multibuf::MultiBufAllocator
& write_allocator)
48
: write_alloc_future_(write_allocator) {}
49
LoopbackChannel
(
const
LoopbackChannel
&) =
delete
;
50
LoopbackChannel
& operator=(
const
LoopbackChannel
&) =
delete
;
51
52
LoopbackChannel
(
LoopbackChannel
&&) =
default
;
53
LoopbackChannel
& operator=(
LoopbackChannel
&&) =
default
;
54
55
private
:
56
async2::Poll<Result<multibuf::MultiBuf>
> DoPendRead(
57
async2::Context
& cx)
override
;
58
59
async2::Poll<Status>
DoPendReadyToWrite(
async2::Context
& cx)
final
;
60
61
async2::Poll<std::optional<multibuf::MultiBuf>
> DoPendAllocateWriteBuffer(
62
async2::Context
& cx,
size_t
min_bytes)
final
{
63
write_alloc_future_.SetDesiredSize(min_bytes);
64
return
write_alloc_future_.Pend(cx);
65
}
66
67
Status
DoStageWrite(
multibuf::MultiBuf
&& data)
final
;
68
69
async2::Poll<Status>
DoPendWrite(
async2::Context
&)
final
;
70
71
async2::Poll<Status>
DoPendClose(
async2::Context
&)
final
;
72
73
multibuf::MultiBufAllocationFuture
write_alloc_future_;
74
std::optional<multibuf::MultiBuf> queue_;
75
76
async2::Waker
waker_;
77
};
78
79
template
<>
80
class
LoopbackChannel
<DataType::kByte>
81
:
public
Implement
<ReliableByteReaderWriter> {
82
public
:
83
LoopbackChannel
(
multibuf::MultiBufAllocator
& write_allocator)
84
: write_alloc_future_(write_allocator) {}
85
LoopbackChannel
(
const
LoopbackChannel
&) =
delete
;
86
LoopbackChannel
& operator=(
const
LoopbackChannel
&) =
delete
;
87
88
LoopbackChannel
(
LoopbackChannel
&&) =
default
;
89
LoopbackChannel
& operator=(
LoopbackChannel
&&) =
default
;
90
91
private
:
92
async2::Poll<Result<multibuf::MultiBuf>
> DoPendRead(
93
async2::Context
& cx)
override
;
94
95
async2::Poll<Status>
DoPendReadyToWrite(
async2::Context
&)
final
{
96
return
async2::Ready(
OkStatus
());
97
}
98
99
async2::Poll<std::optional<multibuf::MultiBuf>
> DoPendAllocateWriteBuffer(
100
async2::Context
& cx,
size_t
min_bytes)
final
{
101
write_alloc_future_.SetDesiredSize(min_bytes);
102
return
write_alloc_future_.Pend(cx);
103
}
104
105
Status
DoStageWrite(
multibuf::MultiBuf
&& data)
final
;
106
107
async2::Poll<Status>
DoPendWrite(
async2::Context
&)
final
;
108
109
async2::Poll<Status>
DoPendClose(
async2::Context
&)
final
;
110
111
multibuf::MultiBufAllocationFuture
write_alloc_future_;
112
multibuf::MultiBuf
queue_;
113
114
async2::Waker
read_waker_;
115
};
116
117
}
// namespace pw::channel
pw::Status
Definition:
status.h:85
pw::async2::Context
Definition:
dispatcher_base.h:52
pw::async2::Poll
Definition:
poll.h:54
pw::async2::Waker
Definition:
dispatcher_base.h:318
pw::channel::Implement
Definition:
channel.h:583
pw::channel::LoopbackChannel< DataType::kByte >
Definition:
loopback_channel.h:81
pw::channel::LoopbackChannel< DataType::kDatagram >
Definition:
loopback_channel.h:45
pw::channel::LoopbackChannel
Definition:
loopback_channel.h:33
pw::multibuf::MultiBufAllocationFuture
Definition:
allocator_async.h:90
pw::multibuf::MultiBufAllocator
Definition:
allocator.h:54
pw::multibuf::MultiBuf
Definition:
multibuf.h:245
pw::OkStatus
constexpr Status OkStatus()
Definition:
status.h:234
pw_channel
public
pw_channel
loopback_channel.h
Generated by
1.9.6