LCOV - code coverage report
Current view: top level - corosio/native - native_tcp_socket.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 93.0 % 57 53 4
Test Date: 2026-04-09 23:21:11 Functions: 100.0 % 38 38

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

Generated by: LCOV version 2.3