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/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
42class EpollChannel : public Implement<ByteReaderWriter> {
43 public:
44 EpollChannel(int channel_fd,
45 async2::Dispatcher& dispatcher,
47 : channel_fd_(channel_fd),
48 ready_to_write_(false),
49 dispatcher_(&dispatcher),
50 write_alloc_future_(allocator) {
51 Register();
52 }
53
54 ~EpollChannel() override { Cleanup(); }
55
56 EpollChannel(const EpollChannel&) = delete;
57 EpollChannel& operator=(const EpollChannel&) = delete;
58
59 EpollChannel(EpollChannel&&) = default;
60 EpollChannel& operator=(EpollChannel&&) = default;
61
62 private:
63 static constexpr size_t kMinimumReadSize = 64;
64 static constexpr size_t kDesiredReadSize = 1024;
65
66 void Register();
67
69 async2::Context& cx) override;
70
71 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx) final;
72
73 async2::PollOptional<multibuf::MultiBuf> DoPendAllocateWriteBuffer(
74 async2::Context& cx, size_t min_bytes) final {
75 write_alloc_future_.SetDesiredSize(min_bytes);
76 return write_alloc_future_.Pend(cx);
77 }
78
79 Status DoStageWrite(multibuf::MultiBuf&& data) final;
80
81 async2::Poll<Status> DoPendWrite(async2::Context&) final {
82 return OkStatus();
83 }
84
85 async2::Poll<Status> DoPendClose(async2::Context&) final {
86 Cleanup();
87 return async2::Ready(OkStatus());
88 }
89
90 void set_closed() {
91 set_read_closed();
92 set_write_closed();
93 }
94
95 void Cleanup();
96
97 int channel_fd_;
98 bool ready_to_write_;
99
100 async2::Dispatcher* dispatcher_;
101 multibuf::MultiBufAllocationFuture write_alloc_future_;
102 async2::Waker waker_;
103};
104
106
107} // namespace pw::channel
Definition: status.h:109
Definition: context.h:55
A single-threaded cooperatively scheduled runtime for async tasks.
Definition: dispatcher.h:48
Definition: poll.h:60
Definition: waker.h:160
Definition: epoll_channel.h:42
Definition: channel.h:583
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