1  
//
1  
//
2  
// Copyright (c) 2026 Steve Gerbino
2  
// Copyright (c) 2026 Steve Gerbino
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/corosio
7  
// Official repository: https://github.com/cppalliance/corosio
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_SOCKET_HPP
10  
#ifndef BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_SOCKET_HPP
11  
#define BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_SOCKET_HPP
11  
#define BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_SOCKET_HPP
12  

12  

13  
#include <boost/corosio/detail/platform.hpp>
13  
#include <boost/corosio/detail/platform.hpp>
14  

14  

15  
#if BOOST_COROSIO_HAS_EPOLL
15  
#if BOOST_COROSIO_HAS_EPOLL
16  

16  

17  
#include <boost/corosio/tcp_socket.hpp>
17  
#include <boost/corosio/tcp_socket.hpp>
18  
#include <boost/capy/ex/executor_ref.hpp>
18  
#include <boost/capy/ex/executor_ref.hpp>
19  
#include <boost/corosio/detail/intrusive.hpp>
19  
#include <boost/corosio/detail/intrusive.hpp>
20  

20  

21  
#include <boost/corosio/native/detail/epoll/epoll_op.hpp>
21  
#include <boost/corosio/native/detail/epoll/epoll_op.hpp>
22  

22  

23  
#include <memory>
23  
#include <memory>
24  

24  

25  
namespace boost::corosio::detail {
25  
namespace boost::corosio::detail {
26  

26  

27  
class epoll_socket_service;
27  
class epoll_socket_service;
28  

28  

29  
/// Socket implementation for epoll backend.
29  
/// Socket implementation for epoll backend.
30  
class epoll_socket final
30  
class epoll_socket final
31  
    : public tcp_socket::implementation
31  
    : public tcp_socket::implementation
32  
    , public std::enable_shared_from_this<epoll_socket>
32  
    , public std::enable_shared_from_this<epoll_socket>
33  
    , public intrusive_list<epoll_socket>::node
33  
    , public intrusive_list<epoll_socket>::node
34  
{
34  
{
35  
    friend class epoll_socket_service;
35  
    friend class epoll_socket_service;
36  

36  

37  
public:
37  
public:
38  
    explicit epoll_socket(epoll_socket_service& svc) noexcept;
38  
    explicit epoll_socket(epoll_socket_service& svc) noexcept;
39  
    ~epoll_socket() override;
39  
    ~epoll_socket() override;
40  

40  

41  
    std::coroutine_handle<> connect(
41  
    std::coroutine_handle<> connect(
42  
        std::coroutine_handle<>,
42  
        std::coroutine_handle<>,
43  
        capy::executor_ref,
43  
        capy::executor_ref,
44  
        endpoint,
44  
        endpoint,
45  
        std::stop_token,
45  
        std::stop_token,
46  
        std::error_code*) override;
46  
        std::error_code*) override;
47  

47  

48  
    std::coroutine_handle<> read_some(
48  
    std::coroutine_handle<> read_some(
49  
        std::coroutine_handle<>,
49  
        std::coroutine_handle<>,
50  
        capy::executor_ref,
50  
        capy::executor_ref,
51  
        io_buffer_param,
51  
        io_buffer_param,
52  
        std::stop_token,
52  
        std::stop_token,
53  
        std::error_code*,
53  
        std::error_code*,
54  
        std::size_t*) override;
54  
        std::size_t*) override;
55  

55  

56  
    std::coroutine_handle<> write_some(
56  
    std::coroutine_handle<> write_some(
57  
        std::coroutine_handle<>,
57  
        std::coroutine_handle<>,
58  
        capy::executor_ref,
58  
        capy::executor_ref,
59  
        io_buffer_param,
59  
        io_buffer_param,
60  
        std::stop_token,
60  
        std::stop_token,
61  
        std::error_code*,
61  
        std::error_code*,
62  
        std::size_t*) override;
62  
        std::size_t*) override;
63  

63  

64  
    std::error_code shutdown(tcp_socket::shutdown_type what) noexcept override;
64  
    std::error_code shutdown(tcp_socket::shutdown_type what) noexcept override;
65  

65  

66  
    native_handle_type native_handle() const noexcept override
66  
    native_handle_type native_handle() const noexcept override
67  
    {
67  
    {
68  
        return fd_;
68  
        return fd_;
69  
    }
69  
    }
70  

70  

71  
    std::error_code set_option(
71  
    std::error_code set_option(
72  
        int level, int optname,
72  
        int level, int optname,
73  
        void const* data, std::size_t size) noexcept override;
73  
        void const* data, std::size_t size) noexcept override;
74  
    std::error_code get_option(
74  
    std::error_code get_option(
75  
        int level, int optname,
75  
        int level, int optname,
76  
        void* data, std::size_t* size) const noexcept override;
76  
        void* data, std::size_t* size) const noexcept override;
77  

77  

78  
    endpoint local_endpoint() const noexcept override
78  
    endpoint local_endpoint() const noexcept override
79  
    {
79  
    {
80  
        return local_endpoint_;
80  
        return local_endpoint_;
81  
    }
81  
    }
82  
    endpoint remote_endpoint() const noexcept override
82  
    endpoint remote_endpoint() const noexcept override
83  
    {
83  
    {
84  
        return remote_endpoint_;
84  
        return remote_endpoint_;
85  
    }
85  
    }
86  
    bool is_open() const noexcept
86  
    bool is_open() const noexcept
87  
    {
87  
    {
88  
        return fd_ >= 0;
88  
        return fd_ >= 0;
89  
    }
89  
    }
90  
    void cancel() noexcept override;
90  
    void cancel() noexcept override;
91  
    void cancel_single_op(epoll_op& op) noexcept;
91  
    void cancel_single_op(epoll_op& op) noexcept;
92  
    void close_socket() noexcept;
92  
    void close_socket() noexcept;
93  
    void set_socket(int fd) noexcept
93  
    void set_socket(int fd) noexcept
94  
    {
94  
    {
95  
        fd_ = fd;
95  
        fd_ = fd;
96  
    }
96  
    }
97  
    void set_endpoints(endpoint local, endpoint remote) noexcept
97  
    void set_endpoints(endpoint local, endpoint remote) noexcept
98  
    {
98  
    {
99  
        local_endpoint_  = local;
99  
        local_endpoint_  = local;
100  
        remote_endpoint_ = remote;
100  
        remote_endpoint_ = remote;
101  
    }
101  
    }
102  

102  

103  
    epoll_connect_op conn_;
103  
    epoll_connect_op conn_;
104  
    epoll_read_op rd_;
104  
    epoll_read_op rd_;
105  
    epoll_write_op wr_;
105  
    epoll_write_op wr_;
106  

106  

107  
    /// Per-descriptor state for persistent epoll registration
107  
    /// Per-descriptor state for persistent epoll registration
108  
    descriptor_state desc_state_;
108  
    descriptor_state desc_state_;
109  

109  

110  
private:
110  
private:
111  
    epoll_socket_service& svc_;
111  
    epoll_socket_service& svc_;
112  
    int fd_ = -1;
112  
    int fd_ = -1;
113  
    endpoint local_endpoint_;
113  
    endpoint local_endpoint_;
114  
    endpoint remote_endpoint_;
114  
    endpoint remote_endpoint_;
115  

115  

116  
    void register_op(
116  
    void register_op(
117  
        epoll_op& op,
117  
        epoll_op& op,
118  
        epoll_op*& desc_slot,
118  
        epoll_op*& desc_slot,
119  
        bool& ready_flag,
119  
        bool& ready_flag,
120  
        bool& cancel_flag) noexcept;
120  
        bool& cancel_flag) noexcept;
121  

121  

122  
    friend struct epoll_op;
122  
    friend struct epoll_op;
123  
    friend struct epoll_connect_op;
123  
    friend struct epoll_connect_op;
124  
};
124  
};
125  

125  

126  
} // namespace boost::corosio::detail
126  
} // namespace boost::corosio::detail
127  

127  

128  
#endif // BOOST_COROSIO_HAS_EPOLL
128  
#endif // BOOST_COROSIO_HAS_EPOLL
129  

129  

130  
#endif // BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_SOCKET_HPP
130  
#endif // BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_SOCKET_HPP