LCOV - code coverage report
Current view: top level - corosio/native - native_local_stream_socket.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 94.4 % 72 68 4
Test Date: 2026-05-29 17:59:39 Functions: 100.0 % 46 46

           TLA  Line data    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_NATIVE_NATIVE_LOCAL_STREAM_SOCKET_HPP
      11                 : #define BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_SOCKET_HPP
      12                 : 
      13                 : #include <boost/corosio/local_stream_socket.hpp>
      14                 : #include <boost/corosio/backend.hpp>
      15                 : 
      16                 : #ifndef BOOST_COROSIO_MRDOCS
      17                 : #if BOOST_COROSIO_HAS_EPOLL
      18                 : #include <boost/corosio/native/detail/epoll/epoll_types.hpp>
      19                 : #endif
      20                 : 
      21                 : #if BOOST_COROSIO_HAS_SELECT
      22                 : #include <boost/corosio/native/detail/select/select_types.hpp>
      23                 : #endif
      24                 : 
      25                 : #if BOOST_COROSIO_HAS_KQUEUE
      26                 : #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp>
      27                 : #endif
      28                 : 
      29                 : #if BOOST_COROSIO_HAS_IOCP
      30                 : #include <boost/corosio/native/detail/iocp/win_local_stream_service.hpp>
      31                 : #endif
      32                 : #endif // !BOOST_COROSIO_MRDOCS
      33                 : 
      34                 : namespace boost::corosio {
      35                 : 
      36                 : /** An asynchronous Unix stream socket with devirtualized I/O operations.
      37                 : 
      38                 :     This class template inherits from @ref local_stream_socket and
      39                 :     shadows the async operations (`read_some`, `write_some`,
      40                 :     `connect`) with versions that call the backend implementation
      41                 :     directly, allowing the compiler to inline through the entire
      42                 :     call chain.
      43                 : 
      44                 :     Non-async operations (`open`, `close`, `cancel`, socket options)
      45                 :     remain unchanged and dispatch through the compiled library.
      46                 : 
      47                 :     A `native_local_stream_socket` IS-A `local_stream_socket` and
      48                 :     can be passed to any function expecting `local_stream_socket&`
      49                 :     or `io_stream&`, in which case virtual dispatch is used
      50                 :     transparently.
      51                 : 
      52                 :     @tparam Backend A backend tag value (e.g., `epoll`) whose type
      53                 :         provides the concrete implementation types.
      54                 : 
      55                 :     @par Thread Safety
      56                 :     Same as @ref local_stream_socket.
      57                 : 
      58                 :     @par Example
      59                 :     @code
      60                 :     #include <boost/corosio/native/native_local_stream_socket.hpp>
      61                 : 
      62                 :     native_io_context<epoll> ctx;
      63                 :     native_local_stream_socket<epoll> s(ctx);
      64                 :     s.open();
      65                 :     auto [ec] = co_await s.connect(local_endpoint("/tmp/my.sock"));
      66                 :     @endcode
      67                 : 
      68                 :     @see local_stream_socket, epoll_t, iocp_t
      69                 : */
      70                 : template<auto Backend>
      71                 : class native_local_stream_socket : public local_stream_socket
      72                 : {
      73                 :     using backend_type = decltype(Backend);
      74                 :     using impl_type    = typename backend_type::local_stream_socket_type;
      75                 :     using service_type = typename backend_type::local_stream_service_type;
      76                 : 
      77 HIT          20 :     impl_type& get_impl() noexcept
      78                 :     {
      79              20 :         return *static_cast<impl_type*>(h_.get());
      80                 :     }
      81                 : 
      82                 :     template<class MutableBufferSequence>
      83                 :     struct native_read_awaitable
      84                 :     {
      85                 :         native_local_stream_socket& self_;
      86                 :         MutableBufferSequence buffers_;
      87                 :         std::stop_token token_;
      88                 :         mutable std::error_code ec_;
      89                 :         mutable std::size_t bytes_transferred_ = 0;
      90                 : 
      91               4 :         native_read_awaitable(
      92                 :             native_local_stream_socket& self,
      93                 :             MutableBufferSequence buffers) noexcept
      94               4 :             : self_(self)
      95               4 :             , buffers_(std::move(buffers))
      96                 :         {
      97               4 :         }
      98                 : 
      99               4 :         bool await_ready() const noexcept
     100                 :         {
     101               4 :             return token_.stop_requested();
     102                 :         }
     103                 : 
     104               4 :         capy::io_result<std::size_t> await_resume() const noexcept
     105                 :         {
     106               4 :             if (token_.stop_requested())
     107 MIS           0 :                 return {make_error_code(std::errc::operation_canceled), 0};
     108 HIT           4 :             return {ec_, bytes_transferred_};
     109                 :         }
     110                 : 
     111               4 :         auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
     112                 :             -> std::coroutine_handle<>
     113                 :         {
     114               4 :             token_ = env->stop_token;
     115              12 :             return self_.get_impl().read_some(
     116              12 :                 h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
     117                 :         }
     118                 :     };
     119                 : 
     120                 :     template<class ConstBufferSequence>
     121                 :     struct native_write_awaitable
     122                 :     {
     123                 :         native_local_stream_socket& self_;
     124                 :         ConstBufferSequence buffers_;
     125                 :         std::stop_token token_;
     126                 :         mutable std::error_code ec_;
     127                 :         mutable std::size_t bytes_transferred_ = 0;
     128                 : 
     129               4 :         native_write_awaitable(
     130                 :             native_local_stream_socket& self,
     131                 :             ConstBufferSequence buffers) noexcept
     132               4 :             : self_(self)
     133               4 :             , buffers_(std::move(buffers))
     134                 :         {
     135               4 :         }
     136                 : 
     137               4 :         bool await_ready() const noexcept
     138                 :         {
     139               4 :             return token_.stop_requested();
     140                 :         }
     141                 : 
     142               4 :         capy::io_result<std::size_t> await_resume() const noexcept
     143                 :         {
     144               4 :             if (token_.stop_requested())
     145 MIS           0 :                 return {make_error_code(std::errc::operation_canceled), 0};
     146 HIT           4 :             return {ec_, bytes_transferred_};
     147                 :         }
     148                 : 
     149               4 :         auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
     150                 :             -> std::coroutine_handle<>
     151                 :         {
     152               4 :             token_ = env->stop_token;
     153              12 :             return self_.get_impl().write_some(
     154              12 :                 h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
     155                 :         }
     156                 :     };
     157                 : 
     158                 :     struct native_wait_awaitable
     159                 :     {
     160                 :         native_local_stream_socket& self_;
     161                 :         wait_type w_;
     162                 :         std::stop_token token_;
     163                 :         mutable std::error_code ec_;
     164                 : 
     165               2 :         native_wait_awaitable(
     166                 :             native_local_stream_socket& self, wait_type w) noexcept
     167               2 :             : self_(self)
     168               2 :             , w_(w)
     169                 :         {
     170               2 :         }
     171                 : 
     172               2 :         bool await_ready() const noexcept
     173                 :         {
     174               2 :             return token_.stop_requested();
     175                 :         }
     176                 : 
     177               2 :         capy::io_result<> await_resume() const noexcept
     178                 :         {
     179               2 :             if (token_.stop_requested())
     180 MIS           0 :                 return {make_error_code(std::errc::operation_canceled)};
     181 HIT           2 :             return {ec_};
     182                 :         }
     183                 : 
     184               2 :         auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
     185                 :             -> std::coroutine_handle<>
     186                 :         {
     187               2 :             token_ = env->stop_token;
     188               6 :             return self_.get_impl().wait(
     189               6 :                 h, env->executor, w_, token_, &ec_);
     190                 :         }
     191                 :     };
     192                 : 
     193                 :     struct native_connect_awaitable
     194                 :     {
     195                 :         native_local_stream_socket& self_;
     196                 :         corosio::local_endpoint endpoint_;
     197                 :         std::stop_token token_;
     198                 :         mutable std::error_code ec_;
     199                 : 
     200              10 :         native_connect_awaitable(
     201                 :             native_local_stream_socket& self,
     202                 :             corosio::local_endpoint ep) noexcept
     203              10 :             : self_(self)
     204              10 :             , endpoint_(ep)
     205                 :         {
     206              10 :         }
     207                 : 
     208              10 :         bool await_ready() const noexcept
     209                 :         {
     210              10 :             return token_.stop_requested();
     211                 :         }
     212                 : 
     213              10 :         capy::io_result<> await_resume() const noexcept
     214                 :         {
     215              10 :             if (token_.stop_requested())
     216 MIS           0 :                 return {make_error_code(std::errc::operation_canceled)};
     217 HIT          10 :             return {ec_};
     218                 :         }
     219                 : 
     220              10 :         auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
     221                 :             -> std::coroutine_handle<>
     222                 :         {
     223              10 :             token_ = env->stop_token;
     224              30 :             return self_.get_impl().connect(
     225              30 :                 h, env->executor, endpoint_, token_, &ec_);
     226                 :         }
     227                 :     };
     228                 : 
     229                 : public:
     230                 :     /** Construct a native socket from an execution context.
     231                 : 
     232                 :         @param ctx The execution context that will own this socket.
     233                 :     */
     234              28 :     explicit native_local_stream_socket(capy::execution_context& ctx)
     235              28 :         : io_object(create_handle<service_type>(ctx))
     236                 :     {
     237              28 :     }
     238                 : 
     239                 :     /** Construct a native socket from an executor.
     240                 : 
     241                 :         @param ex The executor whose context will own the socket.
     242                 :     */
     243                 :     template<class Ex>
     244                 :         requires(!std::same_as<
     245                 :                  std::remove_cvref_t<Ex>,
     246                 :                  native_local_stream_socket>) &&
     247                 :         capy::Executor<Ex>
     248                 :     explicit native_local_stream_socket(Ex const& ex)
     249                 :         : native_local_stream_socket(ex.context())
     250                 :     {
     251                 :     }
     252                 : 
     253                 :     /// Move construct.
     254               4 :     native_local_stream_socket(native_local_stream_socket&&) noexcept = default;
     255                 : 
     256                 :     /// Move assign.
     257                 :     native_local_stream_socket&
     258                 :     operator=(native_local_stream_socket&&) noexcept = default;
     259                 : 
     260                 :     native_local_stream_socket(native_local_stream_socket const&) = delete;
     261                 :     native_local_stream_socket&
     262                 :     operator=(native_local_stream_socket const&) = delete;
     263                 : 
     264                 :     /** Asynchronously read data from the socket.
     265                 : 
     266                 :         Calls the backend implementation directly, bypassing virtual
     267                 :         dispatch. Otherwise identical to @ref io_stream::read_some.
     268                 : 
     269                 :         @param buffers The buffer sequence to read into.
     270                 : 
     271                 :         @return An awaitable yielding `(error_code, std::size_t)`.
     272                 :     */
     273                 :     template<capy::MutableBufferSequence MB>
     274               4 :     auto read_some(MB const& buffers)
     275                 :     {
     276               4 :         return native_read_awaitable<MB>(*this, buffers);
     277                 :     }
     278                 : 
     279                 :     /** Asynchronously write data to the socket.
     280                 : 
     281                 :         Calls the backend implementation directly, bypassing virtual
     282                 :         dispatch. Otherwise identical to @ref io_stream::write_some.
     283                 : 
     284                 :         @param buffers The buffer sequence to write from.
     285                 : 
     286                 :         @return An awaitable yielding `(error_code, std::size_t)`.
     287                 :     */
     288                 :     template<capy::ConstBufferSequence CB>
     289               4 :     auto write_some(CB const& buffers)
     290                 :     {
     291               4 :         return native_write_awaitable<CB>(*this, buffers);
     292                 :     }
     293                 : 
     294                 :     /** Asynchronously connect to a remote endpoint.
     295                 : 
     296                 :         Calls the backend implementation directly, bypassing virtual
     297                 :         dispatch. Otherwise identical to @ref local_stream_socket::connect.
     298                 : 
     299                 :         If the socket is not already open, it is opened automatically.
     300                 : 
     301                 :         @param ep The local endpoint (path) to connect to.
     302                 : 
     303                 :         @return An awaitable yielding `io_result<>`.
     304                 : 
     305                 :         @throws std::system_error if the socket needs to be opened
     306                 :             and the open fails.
     307                 :     */
     308              10 :     auto connect(corosio::local_endpoint ep)
     309                 :     {
     310              10 :         if (!is_open())
     311              10 :             open();
     312              10 :         return native_connect_awaitable(*this, ep);
     313                 :     }
     314                 : 
     315                 :     /** Asynchronously wait for the socket to be ready.
     316                 : 
     317                 :         Calls the backend implementation directly, bypassing virtual
     318                 :         dispatch. Otherwise identical to @ref local_stream_socket::wait.
     319                 : 
     320                 :         @param w The wait direction (read, write, or error).
     321                 : 
     322                 :         @return An awaitable yielding `io_result<>`.
     323                 :     */
     324               2 :     [[nodiscard]] auto wait(wait_type w)
     325                 :     {
     326               2 :         return native_wait_awaitable(*this, w);
     327                 :     }
     328                 : };
     329                 : 
     330                 : } // namespace boost::corosio
     331                 : 
     332                 : #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_SOCKET_HPP
        

Generated by: LCOV version 2.3