src/corosio/src/tcp_socket.cpp
100.0% Lines (73 / 73)
79.4% Branches (27 / 34)
100.0% Functions (13 / 13)
Functions (13)
Function
Calls
Lines
Branches
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:37
85850x
100.0%
–
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:42
28339x
100.0%
–
100.0%
boost::corosio::tcp_socket::open(boost::corosio::family)
:51
14695x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:59
14691x
100.0%
50.0%
66.0%
boost::corosio::tcp_socket::assign(int)
:75
5034x
100.0%
66.7%
83.0%
boost::corosio::tcp_socket::release()
:92
8x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:102
18x
100.0%
66.7%
83.0%
boost::corosio::tcp_socket::close()
:118
62620x
100.0%
75.0%
80.0%
boost::corosio::tcp_socket::cancel()
:126
205x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::shutdown_type)
:134
27x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:142
80x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::local_endpoint() const
:156
68x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:164
84x
100.0%
100.0%
100.0%
| Line | Branch | 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 | 85850x | tcp_socket::~tcp_socket() | ||
| 38 | 85850x | { | ||
| 39 | 42981x | close(); | ||
| 40 | 85850x | } | ||
| 41 | ||||
| 42 | 56678x | tcp_socket::tcp_socket(capy::execution_context& ctx) | ||
| 43 | 28339x | : io_object(handle( | ||
| 44 | 28339x | ctx, | ||
| 45 | 28339x | detail::use_backend_service<detail::tcp_service_of, tcp_service_key>( | ||
| 46 | 28339x | ctx))) | ||
| 47 | 56678x | { | ||
| 48 | 28339x | } | ||
| 49 | ||||
| 50 | std::error_code | |||
| 51 | 14695x | tcp_socket::open(family f) noexcept | ||
| 52 | { | |||
| 53 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14691 times.
|
14695x | if (is_open()) | |
| 54 | 4x | return {}; | ||
| 55 | 14691x | return open_for_family(detail::native_family(f), SOCK_STREAM, IPPROTO_TCP); | ||
| 56 | 14695x | } | ||
| 57 | ||||
| 58 | std::error_code | |||
| 59 | 14691x | 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 | 14691x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 67 |
2/4✓ Branch 0 taken 14691 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14691 times.
✗ Branch 3 not taken.
|
29382x | std::error_code ec = svc.open_socket( | |
| 68 | 14691x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | ||
| 69 | 14691x | protocol); | ||
| 70 | #endif | |||
| 71 | 14691x | return ec; | ||
| 72 | } | |||
| 73 | ||||
| 74 | std::error_code | |||
| 75 | 5034x | tcp_socket::assign(native_handle_type fd) noexcept | ||
| 76 | { | |||
| 77 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5026 times.
|
5034x | if (is_open()) | |
| 78 | 8x | 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 | 5026x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 85 |
2/4✓ Branch 0 taken 5026 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5026 times.
✗ Branch 3 not taken.
|
10052x | std::error_code ec = svc.assign_socket( | |
| 86 | 5026x | static_cast<tcp_socket::implementation&>(*h_.get()), fd); | ||
| 87 | #endif | |||
| 88 | 5026x | return ec; | ||
| 89 | 5034x | } | ||
| 90 | ||||
| 91 | capy::results<native_handle_type> | |||
| 92 | 8x | tcp_socket::release() noexcept | ||
| 93 | { | |||
| 94 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8x | if (!is_open()) | |
| 95 | 2x | return { | ||
| 96 | 2x | make_error_code(std::errc::bad_file_descriptor), | ||
| 97 | detail::invalid_native_handle}; | |||
| 98 | 6x | return {std::error_code{}, get().release_socket()}; | ||
| 99 | 8x | } | ||
| 100 | ||||
| 101 | std::error_code | |||
| 102 | 18x | tcp_socket::bind(endpoint ep) noexcept | ||
| 103 | { | |||
| 104 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18x | if (!is_open()) | |
| 105 | 2x | 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 | 16x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 112 |
2/4✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
32x | return svc.bind_socket( | |
| 113 | 16x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | ||
| 114 | #endif | |||
| 115 | 18x | } | ||
| 116 | ||||
| 117 | void | |||
| 118 | 62620x | tcp_socket::close() noexcept | ||
| 119 | { | |||
| 120 |
2/2✓ Branch 0 taken 30394 times.
✓ Branch 1 taken 32226 times.
|
62620x | if (!is_open()) | |
| 121 | 32226x | return; | ||
| 122 |
1/2✓ Branch 0 taken 30394 times.
✗ Branch 1 not taken.
|
30394x | h_.service().close(h_); | |
| 123 | 62620x | } | ||
| 124 | ||||
| 125 | void | |||
| 126 | 205x | tcp_socket::cancel() noexcept | ||
| 127 | { | |||
| 128 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 202 times.
|
205x | if (!is_open()) | |
| 129 | 3x | return; | ||
| 130 | 202x | get().cancel(); | ||
| 131 | 205x | } | ||
| 132 | ||||
| 133 | std::error_code | |||
| 134 | 27x | tcp_socket::shutdown(shutdown_type what) noexcept | ||
| 135 | { | |||
| 136 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 21 times.
|
27x | if (!is_open()) | |
| 137 | 6x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 138 | 21x | return get().shutdown(what); | ||
| 139 | 27x | } | ||
| 140 | ||||
| 141 | native_handle_type | |||
| 142 | 80x | tcp_socket::native_handle() const noexcept | ||
| 143 | { | |||
| 144 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 78 times.
|
80x | if (!is_open()) | |
| 145 | { | |||
| 146 | #if BOOST_COROSIO_HAS_IOCP | |||
| 147 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | |||
| 148 | #else | |||
| 149 | 2x | return -1; | ||
| 150 | #endif | |||
| 151 | } | |||
| 152 | 78x | return get().native_handle(); | ||
| 153 | 80x | } | ||
| 154 | ||||
| 155 | endpoint | |||
| 156 | 68x | tcp_socket::local_endpoint() const noexcept | ||
| 157 | { | |||
| 158 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 58 times.
|
68x | if (!is_open()) | |
| 159 | 10x | return endpoint{}; | ||
| 160 | 58x | return get().local_endpoint(); | ||
| 161 | 68x | } | ||
| 162 | ||||
| 163 | endpoint | |||
| 164 | 84x | tcp_socket::remote_endpoint() const noexcept | ||
| 165 | { | |||
| 166 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 74 times.
|
84x | if (!is_open()) | |
| 167 | 10x | return endpoint{}; | ||
| 168 | 74x | return get().remote_endpoint(); | ||
| 169 | 84x | } | ||
| 170 | ||||
| 171 | } // namespace boost::corosio | |||
| 172 |