src/corosio/src/tcp_socket.cpp
100.0% Lines (59 / 59)
100.0% Functions (13 / 13)
Functions (13)
Function
Calls
Lines
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:37
50946x
100.0%
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:42
32763x
100.0%
100.0%
boost::corosio::tcp_socket::open(boost::corosio::family)
:51
14945x
100.0%
100.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:59
14939x
100.0%
100.0%
boost::corosio::tcp_socket::assign(int)
:75
5058x
100.0%
100.0%
boost::corosio::tcp_socket::release()
:92
12x
100.0%
100.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:102
33x
100.0%
100.0%
boost::corosio::tcp_socket::close()
:118
74619x
100.0%
100.0%
boost::corosio::tcp_socket::cancel()
:126
356x
100.0%
100.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::shutdown_type)
:134
34x
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:142
207x
100.0%
100.0%
boost::corosio::tcp_socket::local_endpoint() const
:156
109x
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:164
132x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco ([email protected]) | ||
| 3 | // Copyright (c) 2026 Steve Gerbino | ||
| 4 | // Copyright (c) 2026 Michael Vandeberg | ||
| 5 | // | ||
| 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 8 | // | ||
| 9 | // Official repository: https://github.com/cppalliance/corosio | ||
| 10 | // | ||
| 11 | |||
| 12 | #include <boost/corosio/tcp_socket.hpp> | ||
| 13 | #include <boost/corosio/error.hpp> | ||
| 14 | #include <boost/corosio/native/detail/endpoint_convert.hpp> | ||
| 15 | #include <boost/corosio/detail/platform.hpp> | ||
| 16 | |||
| 17 | #include "src/detail/use_backend_service.hpp" | ||
| 18 | |||
| 19 | #if BOOST_COROSIO_HAS_IOCP | ||
| 20 | #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp> | ||
| 21 | #else | ||
| 22 | #include <boost/corosio/detail/tcp_service.hpp> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | namespace boost::corosio { | ||
| 26 | |||
| 27 | namespace { | ||
| 28 | |||
| 29 | #if BOOST_COROSIO_HAS_IOCP | ||
| 30 | using tcp_service_key = detail::win_tcp_service; | ||
| 31 | #else | ||
| 32 | using tcp_service_key = detail::tcp_service; | ||
| 33 | #endif | ||
| 34 | |||
| 35 | } // namespace | ||
| 36 | |||
| 37 | 50946x | tcp_socket::~tcp_socket() | |
| 38 | { | ||
| 39 | 50919x | close(); | |
| 40 | 50946x | } | |
| 41 | |||
| 42 | 32763x | tcp_socket::tcp_socket(capy::execution_context& ctx) | |
| 43 | 32763x | : io_object(handle( | |
| 44 | ctx, | ||
| 45 | 32763x | detail::use_backend_service<detail::tcp_service_of, tcp_service_key>( | |
| 46 | 98289x | ctx))) | |
| 47 | { | ||
| 48 | 32763x | } | |
| 49 | |||
| 50 | std::error_code | ||
| 51 | 14945x | tcp_socket::open(family f) noexcept | |
| 52 | { | ||
| 53 | 14945x | if (is_open()) | |
| 54 | 6x | return {}; | |
| 55 | 14939x | return open_for_family(detail::native_family(f), SOCK_STREAM, IPPROTO_TCP); | |
| 56 | } | ||
| 57 | |||
| 58 | std::error_code | ||
| 59 | 14939x | tcp_socket::open_for_family(int family, int type, int protocol) noexcept | |
| 60 | { | ||
| 61 | #if BOOST_COROSIO_HAS_IOCP | ||
| 62 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | ||
| 63 | auto& sock = static_cast<detail::win_tcp_socket&>(*h_.get()); | ||
| 64 | std::error_code ec = svc.open_socket(sock, family, type, protocol); | ||
| 65 | #else | ||
| 66 | 14939x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 67 | 14939x | std::error_code ec = svc.open_socket( | |
| 68 | 14939x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | |
| 69 | protocol); | ||
| 70 | #endif | ||
| 71 | 14939x | return ec; | |
| 72 | } | ||
| 73 | |||
| 74 | std::error_code | ||
| 75 | 5058x | tcp_socket::assign(native_handle_type fd) noexcept | |
| 76 | { | ||
| 77 | 5058x | if (is_open()) | |
| 78 | 12x | return make_error_code(error::already_open); | |
| 79 | #if BOOST_COROSIO_HAS_IOCP | ||
| 80 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | ||
| 81 | auto& sock = static_cast<detail::win_tcp_socket&>(*h_.get()); | ||
| 82 | std::error_code ec = svc.assign_socket(sock, fd); | ||
| 83 | #else | ||
| 84 | 5046x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 85 | 5046x | std::error_code ec = svc.assign_socket( | |
| 86 | 5046x | static_cast<tcp_socket::implementation&>(*h_.get()), fd); | |
| 87 | #endif | ||
| 88 | 5046x | return ec; | |
| 89 | } | ||
| 90 | |||
| 91 | capy::results<native_handle_type> | ||
| 92 | 12x | tcp_socket::release() noexcept | |
| 93 | { | ||
| 94 | 12x | if (!is_open()) | |
| 95 | return { | ||
| 96 | 6x | make_error_code(std::errc::bad_file_descriptor), | |
| 97 | 3x | detail::invalid_native_handle}; | |
| 98 | 9x | return {std::error_code{}, get().release_socket()}; | |
| 99 | } | ||
| 100 | |||
| 101 | std::error_code | ||
| 102 | 33x | tcp_socket::bind(endpoint ep) noexcept | |
| 103 | { | ||
| 104 | 33x | if (!is_open()) | |
| 105 | 3x | return make_error_code(std::errc::bad_file_descriptor); | |
| 106 | #if BOOST_COROSIO_HAS_IOCP | ||
| 107 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | ||
| 108 | auto& sock = static_cast<detail::win_tcp_socket&>(*h_.get()); | ||
| 109 | return svc.bind_socket(sock, ep); | ||
| 110 | #else | ||
| 111 | 30x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 112 | 30x | return svc.bind_socket( | |
| 113 | 60x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | |
| 114 | #endif | ||
| 115 | } | ||
| 116 | |||
| 117 | void | ||
| 118 | 74619x | tcp_socket::close() noexcept | |
| 119 | { | ||
| 120 | 74619x | if (!is_open()) | |
| 121 | 42619x | return; | |
| 122 | 32000x | h_.service().close(h_); | |
| 123 | } | ||
| 124 | |||
| 125 | void | ||
| 126 | 356x | tcp_socket::cancel() noexcept | |
| 127 | { | ||
| 128 | 356x | if (!is_open()) | |
| 129 | 4x | return; | |
| 130 | 352x | get().cancel(); | |
| 131 | } | ||
| 132 | |||
| 133 | std::error_code | ||
| 134 | 34x | tcp_socket::shutdown(shutdown_type what) noexcept | |
| 135 | { | ||
| 136 | 34x | if (!is_open()) | |
| 137 | 9x | return make_error_code(std::errc::bad_file_descriptor); | |
| 138 | 25x | return get().shutdown(what); | |
| 139 | } | ||
| 140 | |||
| 141 | native_handle_type | ||
| 142 | 207x | tcp_socket::native_handle() const noexcept | |
| 143 | { | ||
| 144 | 207x | if (!is_open()) | |
| 145 | { | ||
| 146 | #if BOOST_COROSIO_HAS_IOCP | ||
| 147 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | ||
| 148 | #else | ||
| 149 | 3x | return -1; | |
| 150 | #endif | ||
| 151 | } | ||
| 152 | 204x | return get().native_handle(); | |
| 153 | } | ||
| 154 | |||
| 155 | endpoint | ||
| 156 | 109x | tcp_socket::local_endpoint() const noexcept | |
| 157 | { | ||
| 158 | 109x | if (!is_open()) | |
| 159 | 15x | return endpoint{}; | |
| 160 | 94x | return get().local_endpoint(); | |
| 161 | } | ||
| 162 | |||
| 163 | endpoint | ||
| 164 | 132x | tcp_socket::remote_endpoint() const noexcept | |
| 165 | { | ||
| 166 | 132x | if (!is_open()) | |
| 167 | 15x | return endpoint{}; | |
| 168 | 117x | return get().remote_endpoint(); | |
| 169 | } | ||
| 170 | |||
| 171 | } // namespace boost::corosio | ||
| 172 |