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