include/boost/corosio/native/detail/kqueue/kqueue_acceptor.hpp
100.0% Lines (9/9)
100.0% Functions (5/5)
-% Branches (0/0)
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Michael Vandeberg | ||
| 3 | // Copyright (c) 2026 Steve Gerbino | ||
| 4 | // | ||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 7 | // | ||
| 8 | // Official repository: https://github.com/cppalliance/corosio | ||
| 9 | // | ||
| 10 | |||
| 11 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_KQUEUE_KQUEUE_ACCEPTOR_HPP | ||
| 12 | #define BOOST_COROSIO_NATIVE_DETAIL_KQUEUE_KQUEUE_ACCEPTOR_HPP | ||
| 13 | |||
| 14 | #include <boost/corosio/detail/platform.hpp> | ||
| 15 | |||
| 16 | #if BOOST_COROSIO_HAS_KQUEUE | ||
| 17 | |||
| 18 | #include <boost/corosio/tcp_acceptor.hpp> | ||
| 19 | #include <boost/capy/ex/executor_ref.hpp> | ||
| 20 | #include <boost/corosio/detail/intrusive.hpp> | ||
| 21 | |||
| 22 | #include <boost/corosio/native/detail/kqueue/kqueue_op.hpp> | ||
| 23 | |||
| 24 | #include <memory> | ||
| 25 | |||
| 26 | namespace boost::corosio::detail { | ||
| 27 | |||
| 28 | class kqueue_acceptor_service; | ||
| 29 | |||
| 30 | /// Acceptor implementation for kqueue backend. | ||
| 31 | class kqueue_acceptor final | ||
| 32 | : public tcp_acceptor::implementation | ||
| 33 | , public std::enable_shared_from_this<kqueue_acceptor> | ||
| 34 | , public intrusive_list<kqueue_acceptor>::node | ||
| 35 | { | ||
| 36 | friend class kqueue_acceptor_service; | ||
| 37 | |||
| 38 | public: | ||
| 39 | explicit kqueue_acceptor(kqueue_acceptor_service& svc) noexcept; | ||
| 40 | |||
| 41 | /** Initiate an asynchronous accept on the listening socket. | ||
| 42 | |||
| 43 | Attempts a synchronous accept first. If the socket would block | ||
| 44 | (EAGAIN), the operation is parked in desc_state_ until the | ||
| 45 | reactor delivers a read-readiness event, at which point the | ||
| 46 | accept is retried. On completion (success, error, or | ||
| 47 | cancellation) the operation is posted to the scheduler and | ||
| 48 | @a caller is resumed via @a ex. | ||
| 49 | |||
| 50 | Only one accept may be outstanding at a time; overlapping | ||
| 51 | calls produce undefined behavior. | ||
| 52 | |||
| 53 | @param caller Coroutine handle resumed on completion. | ||
| 54 | @param ex Executor through which @a caller is resumed. | ||
| 55 | @param token Stop token for cancellation. | ||
| 56 | @param ec Points to storage for the result error code. | ||
| 57 | @param out_impl Points to storage for the accepted socket impl. | ||
| 58 | |||
| 59 | @return std::noop_coroutine() unconditionally. | ||
| 60 | */ | ||
| 61 | std::coroutine_handle<> accept( | ||
| 62 | std::coroutine_handle<> caller, | ||
| 63 | capy::executor_ref ex, | ||
| 64 | std::stop_token token, | ||
| 65 | std::error_code* ec, | ||
| 66 | io_object::implementation** out_impl) override; | ||
| 67 | |||
| 68 | int native_handle() const noexcept | ||
| 69 | { | ||
| 70 | return fd_; | ||
| 71 | } | ||
| 72 | 945 | endpoint local_endpoint() const noexcept override | |
| 73 | { | ||
| 74 | 945 | return local_endpoint_; | |
| 75 | } | ||
| 76 | 9285 | bool is_open() const noexcept override | |
| 77 | { | ||
| 78 | 9285 | return fd_ >= 0; | |
| 79 | } | ||
| 80 | |||
| 81 | /** Cancel any pending accept operation. */ | ||
| 82 | void cancel() noexcept override; | ||
| 83 | |||
| 84 | /** Cancel a specific pending operation. | ||
| 85 | |||
| 86 | @param op The operation to cancel. | ||
| 87 | */ | ||
| 88 | void cancel_single_op(kqueue_op& op) noexcept; | ||
| 89 | |||
| 90 | /** Close the listening socket and cancel pending operations. */ | ||
| 91 | void close_socket() noexcept; | ||
| 92 | 962 | void set_local_endpoint(endpoint ep) noexcept | |
| 93 | { | ||
| 94 | 962 | local_endpoint_ = ep; | |
| 95 | 962 | } | |
| 96 | |||
| 97 | 10881 | kqueue_acceptor_service& service() noexcept | |
| 98 | { | ||
| 99 | 10881 | return svc_; | |
| 100 | } | ||
| 101 | |||
| 102 | private: | ||
| 103 | kqueue_acceptor_service& svc_; | ||
| 104 | kqueue_accept_op acc_; | ||
| 105 | descriptor_state desc_state_; | ||
| 106 | int fd_ = -1; | ||
| 107 | endpoint local_endpoint_; | ||
| 108 | }; | ||
| 109 | |||
| 110 | } // namespace boost::corosio::detail | ||
| 111 | |||
| 112 | #endif // BOOST_COROSIO_HAS_KQUEUE | ||
| 113 | |||
| 114 | #endif // BOOST_COROSIO_NATIVE_DETAIL_KQUEUE_KQUEUE_ACCEPTOR_HPP | ||
| 115 |