src/corosio/src/detail/socket_service.hpp
100.0% Lines (4/4)
100.0% Functions (4/4)
-% Branches (0/0)
src/corosio/src/detail/socket_service.hpp
| Line | Hits | Source Code |
|---|---|---|
| 1 | // | |
| 2 | // Copyright (c) 2026 Steve Gerbino | |
| 3 | // | |
| 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) | |
| 6 | // | |
| 7 | // Official repository: https://github.com/cppalliance/corosio | |
| 8 | // | |
| 9 | ||
| 10 | #ifndef BOOST_COROSIO_DETAIL_SOCKET_SERVICE_HPP | |
| 11 | #define BOOST_COROSIO_DETAIL_SOCKET_SERVICE_HPP | |
| 12 | ||
| 13 | #include <boost/corosio/detail/config.hpp> | |
| 14 | #include <boost/corosio/tcp_socket.hpp> | |
| 15 | #include <boost/corosio/tcp_acceptor.hpp> | |
| 16 | #include <boost/corosio/endpoint.hpp> | |
| 17 | #include <boost/capy/ex/execution_context.hpp> | |
| 18 | #include <system_error> | |
| 19 | ||
| 20 | /* | |
| 21 | Abstract Socket Service | |
| 22 | ======================= | |
| 23 | ||
| 24 | These abstract base classes enable runtime backend selection for socket | |
| 25 | and acceptor operations. Both epoll_sockets and select_sockets derive | |
| 26 | from socket_service and use it as their key_type. This allows | |
| 27 | use_service<socket_service>() to return whichever implementation was | |
| 28 | installed first (by the context constructor). | |
| 29 | ||
| 30 | Design Pattern: | |
| 31 | - socket_service is the abstract base with key_type = socket_service | |
| 32 | - Concrete implementations (epoll_sockets, select_sockets) inherit from it | |
| 33 | - The concrete implementation's key_type is inherited from socket_service | |
| 34 | - Whichever context type is constructed first installs its implementation | |
| 35 | - socket.cpp and acceptor.cpp use the abstract interface | |
| 36 | ||
| 37 | This enables: | |
| 38 | - epoll_context installs epoll_sockets via make_service<epoll_sockets>() | |
| 39 | - select_context installs select_sockets via make_service<select_sockets>() | |
| 40 | - socket.cpp uses use_service<socket_service>() to get whichever is installed | |
| 41 | */ | |
| 42 | ||
| 43 | namespace boost::corosio::detail { | |
| 44 | ||
| 45 | //------------------------------------------------------------------------------ | |
| 46 | ||
| 47 | /** Abstract socket service base class. | |
| 48 | ||
| 49 | This is the service interface used by socket.cpp. Concrete implementations | |
| 50 | (epoll_socket_service, select_socket_service, etc.) inherit from this class | |
| 51 | and provide the actual socket operations. | |
| 52 | ||
| 53 | The key_type is socket_service itself, which enables runtime polymorphism: | |
| 54 | whichever concrete implementation is installed first by a context constructor | |
| 55 | will be returned by find_service<socket_service>(). | |
| 56 | */ | |
| 57 | class socket_service | |
| 58 | : public capy::execution_context::service | |
| 59 | , public io_object::io_service | |
| 60 | { | |
| 61 | public: | |
| 62 | using key_type = socket_service; | |
| 63 | ||
| 64 | /** Open a socket. | |
| 65 | ||
| 66 | Creates an IPv4 TCP socket and associates it with the platform reactor. | |
| 67 | ||
| 68 | @param impl The socket implementation to open. | |
| 69 | @return Error code on failure, empty on success. | |
| 70 | */ | |
| 71 | virtual std::error_code open_socket(tcp_socket::implementation& impl) = 0; | |
| 72 | ||
| 73 | protected: | |
| 74 | 336 | socket_service() = default; |
| 75 | 336 | ~socket_service() override = default; |
| 76 | }; | |
| 77 | ||
| 78 | //------------------------------------------------------------------------------ | |
| 79 | ||
| 80 | /** Abstract acceptor service base class. | |
| 81 | ||
| 82 | This is the service interface used by acceptor.cpp. Concrete implementations | |
| 83 | (epoll_acceptor_service, select_acceptor_service, etc.) inherit from this class | |
| 84 | and provide the actual acceptor operations. | |
| 85 | ||
| 86 | The key_type is acceptor_service itself, which enables runtime polymorphism. | |
| 87 | */ | |
| 88 | class acceptor_service | |
| 89 | : public capy::execution_context::service | |
| 90 | , public io_object::io_service | |
| 91 | { | |
| 92 | public: | |
| 93 | using key_type = acceptor_service; | |
| 94 | ||
| 95 | /** Open an acceptor. | |
| 96 | ||
| 97 | Creates an IPv4 TCP socket, binds it to the specified endpoint, | |
| 98 | and begins listening for incoming connections. | |
| 99 | ||
| 100 | @param impl The acceptor implementation to open. | |
| 101 | @param ep The local endpoint to bind to. | |
| 102 | @param backlog The maximum length of the queue of pending connections. | |
| 103 | @return Error code on failure, empty on success. | |
| 104 | */ | |
| 105 | virtual std::error_code open_acceptor( | |
| 106 | tcp_acceptor::implementation& impl, | |
| 107 | endpoint ep, | |
| 108 | int backlog) = 0; | |
| 109 | ||
| 110 | protected: | |
| 111 | 336 | acceptor_service() = default; |
| 112 | 336 | ~acceptor_service() override = default; |
| 113 | }; | |
| 114 | ||
| 115 | } // namespace boost::corosio::detail | |
| 116 | ||
| 117 | #endif // BOOST_COROSIO_DETAIL_SOCKET_SERVICE_HPP | |
| 118 |