src/corosio/src/tcp_socket.cpp
100.0% Lines (72/72)
100.0% List of functions (13/13)
79.4% Branches (27/34)
Functions (13)
Function
Calls
Lines
Branches
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:38
42055x
100.0%
–
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:43
16798x
100.0%
–
100.0%
boost::corosio::tcp_socket::open(boost::corosio::family)
:52
8229x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:60
8225x
100.0%
50.0%
66.0%
boost::corosio::tcp_socket::assign(int)
:78
26x
100.0%
66.7%
83.0%
boost::corosio::tcp_socket::release()
:96
6x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:106
18x
100.0%
66.7%
83.0%
boost::corosio::tcp_socket::close()
:123
37647x
100.0%
75.0%
80.0%
boost::corosio::tcp_socket::cancel()
:131
203x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::shutdown_type)
:139
28x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:147
68x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::local_endpoint() const
:161
68x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:169
68x
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/except.hpp> | |||
| 16 | #include <boost/corosio/detail/platform.hpp> | |||
| 17 | ||||
| 18 | #include "src/detail/use_backend_service.hpp" | |||
| 19 | ||||
| 20 | #if BOOST_COROSIO_HAS_IOCP | |||
| 21 | #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp> | |||
| 22 | #else | |||
| 23 | #include <boost/corosio/detail/tcp_service.hpp> | |||
| 24 | #endif | |||
| 25 | ||||
| 26 | namespace boost::corosio { | |||
| 27 | ||||
| 28 | namespace { | |||
| 29 | ||||
| 30 | #if BOOST_COROSIO_HAS_IOCP | |||
| 31 | using tcp_service_key = detail::win_tcp_service; | |||
| 32 | #else | |||
| 33 | using tcp_service_key = detail::tcp_service; | |||
| 34 | #endif | |||
| 35 | ||||
| 36 | } // namespace | |||
| 37 | ||||
| 38 | 42055x | tcp_socket::~tcp_socket() | ||
| 39 | 42055x | { | ||
| 40 | 21067x | close(); | ||
| 41 | 42055x | } | ||
| 42 | ||||
| 43 | 33596x | tcp_socket::tcp_socket(capy::execution_context& ctx) | ||
| 44 | 16798x | : io_object(handle( | ||
| 45 | 16798x | ctx, | ||
| 46 | 16798x | detail::use_backend_service<detail::tcp_service_of, tcp_service_key>( | ||
| 47 | 16798x | ctx))) | ||
| 48 | 33596x | { | ||
| 49 | 16798x | } | ||
| 50 | ||||
| 51 | std::error_code | |||
| 52 | 8229x | tcp_socket::open(family f) noexcept | ||
| 53 | { | |||
| 54 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8225 times.
|
8229x | if (is_open()) | |
| 55 | 4x | return {}; | ||
| 56 | 8225x | return open_for_family(detail::native_family(f), SOCK_STREAM, IPPROTO_TCP); | ||
| 57 | 8229x | } | ||
| 58 | ||||
| 59 | std::error_code | |||
| 60 | 8225x | tcp_socket::open_for_family(int family, int type, int protocol) noexcept | ||
| 61 | { | |||
| 62 | #if BOOST_COROSIO_HAS_IOCP | |||
| 63 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 64 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 65 | std::error_code ec = svc.open_socket( | |||
| 66 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), family, | |||
| 67 | type, protocol); | |||
| 68 | #else | |||
| 69 | 8225x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 70 |
2/4✓ Branch 0 taken 8225 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8225 times.
✗ Branch 3 not taken.
|
16450x | std::error_code ec = svc.open_socket( | |
| 71 | 8225x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | ||
| 72 | 8225x | protocol); | ||
| 73 | #endif | |||
| 74 | 8225x | return ec; | ||
| 75 | } | |||
| 76 | ||||
| 77 | std::error_code | |||
| 78 | 26x | tcp_socket::assign(native_handle_type fd) noexcept | ||
| 79 | { | |||
| 80 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 18 times.
|
26x | if (is_open()) | |
| 81 | 8x | return make_error_code(error::already_open); | ||
| 82 | #if BOOST_COROSIO_HAS_IOCP | |||
| 83 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 84 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 85 | std::error_code ec = svc.assign_socket( | |||
| 86 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), fd); | |||
| 87 | #else | |||
| 88 | 18x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 89 |
2/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
|
36x | std::error_code ec = svc.assign_socket( | |
| 90 | 18x | static_cast<tcp_socket::implementation&>(*h_.get()), fd); | ||
| 91 | #endif | |||
| 92 | 18x | return ec; | ||
| 93 | 26x | } | ||
| 94 | ||||
| 95 | native_handle_type | |||
| 96 | 6x | tcp_socket::release() | ||
| 97 | { | |||
| 98 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6x | if (!is_open()) | |
| 99 | 2x | detail::throw_system_error( | ||
| 100 | 2x | make_error_code(std::errc::bad_file_descriptor), | ||
| 101 | "tcp_socket::release"); | |||
| 102 | 4x | return get().release_socket(); | ||
| 103 | } | |||
| 104 | ||||
| 105 | std::error_code | |||
| 106 | 18x | tcp_socket::bind(endpoint ep) noexcept | ||
| 107 | { | |||
| 108 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18x | if (!is_open()) | |
| 109 | 2x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 110 | #if BOOST_COROSIO_HAS_IOCP | |||
| 111 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 112 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 113 | return svc.bind_socket( | |||
| 114 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), ep); | |||
| 115 | #else | |||
| 116 | 16x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 117 |
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( | |
| 118 | 16x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | ||
| 119 | #endif | |||
| 120 | 18x | } | ||
| 121 | ||||
| 122 | void | |||
| 123 | 37647x | tcp_socket::close() noexcept | ||
| 124 | { | |||
| 125 |
2/2✓ Branch 0 taken 16304 times.
✓ Branch 1 taken 21343 times.
|
37647x | if (!is_open()) | |
| 126 | 21343x | return; | ||
| 127 |
1/2✓ Branch 0 taken 16304 times.
✗ Branch 1 not taken.
|
16304x | h_.service().close(h_); | |
| 128 | 37647x | } | ||
| 129 | ||||
| 130 | void | |||
| 131 | 203x | tcp_socket::cancel() noexcept | ||
| 132 | { | |||
| 133 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 201 times.
|
203x | if (!is_open()) | |
| 134 | 2x | return; | ||
| 135 | 201x | get().cancel(); | ||
| 136 | 203x | } | ||
| 137 | ||||
| 138 | std::error_code | |||
| 139 | 28x | tcp_socket::shutdown(shutdown_type what) noexcept | ||
| 140 | { | |||
| 141 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 22 times.
|
28x | if (!is_open()) | |
| 142 | 6x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 143 | 22x | return get().shutdown(what); | ||
| 144 | 28x | } | ||
| 145 | ||||
| 146 | native_handle_type | |||
| 147 | 68x | tcp_socket::native_handle() const noexcept | ||
| 148 | { | |||
| 149 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 66 times.
|
68x | if (!is_open()) | |
| 150 | { | |||
| 151 | #if BOOST_COROSIO_HAS_IOCP | |||
| 152 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | |||
| 153 | #else | |||
| 154 | 2x | return -1; | ||
| 155 | #endif | |||
| 156 | } | |||
| 157 | 66x | return get().native_handle(); | ||
| 158 | 68x | } | ||
| 159 | ||||
| 160 | endpoint | |||
| 161 | 68x | tcp_socket::local_endpoint() const noexcept | ||
| 162 | { | |||
| 163 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 58 times.
|
68x | if (!is_open()) | |
| 164 | 10x | return endpoint{}; | ||
| 165 | 58x | return get().local_endpoint(); | ||
| 166 | 68x | } | ||
| 167 | ||||
| 168 | endpoint | |||
| 169 | 68x | tcp_socket::remote_endpoint() const noexcept | ||
| 170 | { | |||
| 171 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 58 times.
|
68x | if (!is_open()) | |
| 172 | 10x | return endpoint{}; | ||
| 173 | 58x | return get().remote_endpoint(); | ||
| 174 | 68x | } | ||
| 175 | ||||
| 176 | } // namespace boost::corosio | |||
| 177 |