C/C++ API Reference
Loading...
Searching...
No Matches
epoll_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/epoll_dispatcher.h"
21#include "pw_async2/poll.h"
22#include "pw_channel/channel.h"
23#include "pw_multibuf/allocator.h"
24#include "pw_multibuf/allocator_async.h"
25#include "pw_multibuf/multibuf.h"
26
27namespace pw::channel {
28
30
33
43class EpollChannel : public Implement<ByteReaderWriter> {
44 public:
45 EpollChannel(int channel_fd,
46 async2::EpollDispatcher& dispatcher,
48 : channel_fd_(channel_fd),
49 ready_to_write_(false),
50 dispatcher_(&dispatcher),
51 write_alloc_future_(allocator) {
52 Register();
53 }
54
55 ~EpollChannel() override { Cleanup(); }
56
57 EpollChannel(const EpollChannel&) = delete;
58 EpollChannel& operator=(const EpollChannel&) = delete;
59
60 EpollChannel(EpollChannel&&) = default;
61 EpollChannel& operator=(EpollChannel&&) = default;
62
63 private:
64 static constexpr size_t kMinimumReadSize = 64;
65 static constexpr size_t kDesiredReadSize = 1024;
66
67 void Register();
68
70 async2::Context& cx) override;
71
72 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx) final;
73
74 async2::PollOptional<multibuf::MultiBuf> DoPendAllocateWriteBuffer(
75 async2::Context& cx, size_t min_bytes) final {
76 write_alloc_future_.SetDesiredSize(min_bytes);
77 return write_alloc_future_.Pend(cx);
78 }
79
80 Status DoStageWrite(multibuf::MultiBuf&& data) final;
81
82 async2::Poll<Status> DoPendWrite(async2::Context&) final {
83 return OkStatus();
84 }
85
86 async2::Poll<Status> DoPendClose(async2::Context&) final {
87 Cleanup();
88 return async2::Ready(OkStatus());
89 }
90
91 void set_closed() {
92 set_read_closed();
93 set_write_closed();
94 }
95
96 void Cleanup();
97
98 int channel_fd_;
99 bool ready_to_write_;
100
101 async2::EpollDispatcher* dispatcher_;
102 multibuf::MultiBufAllocationFuture write_alloc_future_;
103 async2::Waker waker_;
104};
105
107
108} // namespace pw::channel
Definition: status.h:120
Definition: context.h:54
Definition: epoll_dispatcher.h:23
Definition: poll.h:60
Definition: waker.h:160
Definition: epoll_channel.h:43
Definition: channel.h:554
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:450