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_SELECT_SELECT_SOCKET_HPP
10  
#ifndef BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SOCKET_HPP
11  
#define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SOCKET_HPP
11  
#define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_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_SELECT
15  
#if BOOST_COROSIO_HAS_SELECT
16  

16  

17 -
#include <boost/corosio/tcp_socket.hpp>
17 +
#include <boost/corosio/native/detail/reactor/reactor_socket.hpp>
18 -
#include <boost/capy/ex/executor_ref.hpp>
 
19 -
#include <boost/corosio/detail/intrusive.hpp>
 
20 -

 
21  
#include <boost/corosio/native/detail/select/select_op.hpp>
18  
#include <boost/corosio/native/detail/select/select_op.hpp>
22 -

19 +
#include <boost/capy/ex/executor_ref.hpp>
23 -
#include <memory>
 
24  

20  

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

22  

27  
class select_socket_service;
23  
class select_socket_service;
28  

24  

29  
/// Socket implementation for select backend.
25  
/// Socket implementation for select backend.
30  
class select_socket final
26  
class select_socket final
31 -
    : public tcp_socket::implementation
27 +
    : public reactor_socket<
32 -
    , public std::enable_shared_from_this<select_socket>
28 +
          select_socket,
33 -
    , public intrusive_list<select_socket>::node
29 +
          select_socket_service,
 
30 +
          select_op,
 
31 +
          select_connect_op,
 
32 +
          select_read_op,
 
33 +
          select_write_op,
 
34 +
          select_descriptor_state>
34  
{
35  
{
35  
    friend class select_socket_service;
36  
    friend class select_socket_service;
36  

37  

37  
public:
38  
public:
38  
    explicit select_socket(select_socket_service& svc) noexcept;
39  
    explicit select_socket(select_socket_service& svc) noexcept;
 
40 +
    ~select_socket() override;
39  

41  

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

48  

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

56  

55  
    std::coroutine_handle<> write_some(
57  
    std::coroutine_handle<> write_some(
56  
        std::coroutine_handle<>,
58  
        std::coroutine_handle<>,
57  
        capy::executor_ref,
59  
        capy::executor_ref,
58  
        buffer_param,
60  
        buffer_param,
59  
        std::stop_token,
61  
        std::stop_token,
60  
        std::error_code*,
62  
        std::error_code*,
61  
        std::size_t*) override;
63  
        std::size_t*) override;
62 -
    std::error_code shutdown(tcp_socket::shutdown_type what) noexcept override;
 
63 -

 
64 -
    native_handle_type native_handle() const noexcept override
 
65 -
    {
 
66 -
        return fd_;
 
67 -
    }
 
68 -

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

 
78 -
    endpoint local_endpoint() const noexcept override
 
79 -
    {
 
80 -
        return local_endpoint_;
 
81 -
    }
 
82 -
    endpoint remote_endpoint() const noexcept override
 
83 -
    {
 
84 -
        return remote_endpoint_;
 
85 -
    }
 
86 -
    bool is_open() const noexcept
 
87 -
    {
 
88 -
        return fd_ >= 0;
 
89 -
    }
 
90  

64  

91 -
    void cancel_single_op(select_op& op) noexcept;
 
92  
    void cancel() noexcept override;
65  
    void cancel() noexcept override;
93 -
    void set_socket(int fd) noexcept
 
94 -
    {
 
95 -
        fd_ = fd;
 
96 -
    }
 
97 -
    void set_endpoints(endpoint local, endpoint remote) noexcept
 
98 -
    {
 
99 -
        local_endpoint_  = local;
 
100 -
        remote_endpoint_ = remote;
 
101 -
    }
 
102 -

 
103 -
    select_connect_op conn_;
 
104 -
    select_read_op rd_;
 
105 -
    select_write_op wr_;
 
106 -

 
107 -
private:
 
108 -
    select_socket_service& svc_;
 
109 -
    int fd_ = -1;
 
110 -
    endpoint local_endpoint_;
 
111 -
    endpoint remote_endpoint_;
 
112  
    void close_socket() noexcept;
66  
    void close_socket() noexcept;
113  
};
67  
};
114  

68  

115  
} // namespace boost::corosio::detail
69  
} // namespace boost::corosio::detail
116  

70  

117  
#endif // BOOST_COROSIO_HAS_SELECT
71  
#endif // BOOST_COROSIO_HAS_SELECT
118  

72  

119  
#endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SOCKET_HPP
73  
#endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SOCKET_HPP