src/corosio/src/tcp_acceptor.cpp
97.0% Lines (64/66)
100.0% List of functions (16/16)
92.3% Branches (24/26)
Functions (16)
Function
Calls
Lines
Branches
Blocks
boost::corosio::(anonymous namespace)::exclusive_address_use::level()
:36
20x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::exclusive_address_use::name()
:37
20x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::exclusive_address_use::data() const
:38
20x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::exclusive_address_use::size() const
:39
20x
100.0%
–
100.0%
boost::corosio::tcp_acceptor::~tcp_acceptor()
:45
2338x
0.0%
–
100.0%
boost::corosio::tcp_acceptor::tcp_acceptor(boost::capy::execution_context&)
:50
2321x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::tcp_acceptor(boost::capy::execution_context&, boost::corosio::endpoint, int)
:59
20x
81.8%
77.8%
80.0%
boost::corosio::tcp_acceptor::open(boost::corosio::tcp)
:77
2321x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::assign(unsigned long long)
:94
8x
100.0%
–
100.0%
boost::corosio::tcp_acceptor::release()
:107
5x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::native_handle() const
:117
13x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::bind(boost::corosio::endpoint)
:131
2317x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::listen(int)
:145
2299x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::close()
:159
4602x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::cancel()
:167
6x
100.0%
100.0%
100.0%
boost::corosio::tcp_acceptor::local_endpoint() const
:175
2299x
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 | // | |||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #include <boost/corosio/tcp_acceptor.hpp> | |||
| 12 | #include <boost/corosio/socket_option.hpp> | |||
| 13 | #include <boost/corosio/detail/platform.hpp> | |||
| 14 | ||||
| 15 | #if BOOST_COROSIO_HAS_IOCP | |||
| 16 | #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp> | |||
| 17 | #else | |||
| 18 | #include <boost/corosio/detail/tcp_acceptor_service.hpp> | |||
| 19 | #endif | |||
| 20 | ||||
| 21 | #include <boost/corosio/detail/except.hpp> | |||
| 22 | ||||
| 23 | namespace boost::corosio { | |||
| 24 | ||||
| 25 | #if BOOST_COROSIO_HAS_IOCP | |||
| 26 | namespace { | |||
| 27 | ||||
| 28 | // On Windows SO_REUSEADDR grants bind-over ("hijack") rights instead | |||
| 29 | // of TIME_WAIT reuse, so a second listener would share the port | |||
| 30 | // silently. SO_EXCLUSIVEADDRUSE restores the POSIX contract: the | |||
| 31 | // collision surfaces as WSAEADDRINUSE (errc::address_in_use). | |||
| 32 | struct exclusive_address_use | |||
| 33 | { | |||
| 34 | int value_ = 1; | |||
| 35 | ||||
| 36 | 20x | static int level() noexcept { return SOL_SOCKET; } | ||
| 37 | 20x | static int name() noexcept { return SO_EXCLUSIVEADDRUSE; } | ||
| 38 | 20x | void const* data() const noexcept { return &value_; } | ||
| 39 | 20x | std::size_t size() const noexcept { return sizeof(value_); } | ||
| 40 | }; | |||
| 41 | ||||
| 42 | } // namespace | |||
| 43 | #endif | |||
| 44 | ||||
| 45 | 2338x | tcp_acceptor::~tcp_acceptor() | ||
| 46 | { | |||
| 47 | 2338x | close(); | ||
| 48 | 2338x | } | ||
| 49 | ||||
| 50 | 2321x | tcp_acceptor::tcp_acceptor(capy::execution_context& ctx) | ||
| 51 | #if BOOST_COROSIO_HAS_IOCP | |||
| 52 |
1/1✓ Branch 2 → 3 taken 2321 times.
|
2321x | : io_object(create_handle<detail::win_tcp_acceptor_service>(ctx)) | |
| 53 | #else | |||
| 54 | : io_object(create_handle<detail::tcp_acceptor_service>(ctx)) | |||
| 55 | #endif | |||
| 56 | { | |||
| 57 | 2321x | } | ||
| 58 | ||||
| 59 | 20x | tcp_acceptor::tcp_acceptor( | ||
| 60 | 20x | capy::execution_context& ctx, endpoint ep, int backlog) | ||
| 61 | 20x | : tcp_acceptor(ctx) | ||
| 62 | { | |||
| 63 |
3/4✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 19 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 20 times.
|
20x | if (auto ec = open(ep.is_v6() ? tcp::v6() : tcp::v4())) | |
| 64 | ✗ | detail::throw_system_error(ec, "tcp_acceptor"); | ||
| 65 | #if BOOST_COROSIO_HAS_IOCP | |||
| 66 |
1/1✓ Branch 11 → 12 taken 20 times.
|
20x | set_option(exclusive_address_use{}); | |
| 67 | #else | |||
| 68 | set_option(socket_option::reuse_address(true)); | |||
| 69 | #endif | |||
| 70 |
2/2✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 16 taken 18 times.
|
20x | if (auto ec = bind(ep)) | |
| 71 | 2x | detail::throw_system_error(ec, "tcp_acceptor"); | ||
| 72 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 18 times.
|
18x | if (auto ec = listen(backlog)) | |
| 73 | ✗ | detail::throw_system_error(ec, "tcp_acceptor"); | ||
| 74 | 20x | } | ||
| 75 | ||||
| 76 | std::error_code | |||
| 77 | 2321x | tcp_acceptor::open(tcp proto) noexcept | ||
| 78 | { | |||
| 79 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2320 times.
|
2321x | if (is_open()) | |
| 80 | 1x | return {}; | ||
| 81 | ||||
| 82 | #if BOOST_COROSIO_HAS_IOCP | |||
| 83 | 2320x | auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service()); | ||
| 84 | #else | |||
| 85 | auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service()); | |||
| 86 | #endif | |||
| 87 | 4640x | std::error_code ec = svc.open_acceptor_socket( | ||
| 88 | 2320x | *static_cast<tcp_acceptor::implementation*>(h_.get()), proto.family(), | ||
| 89 | proto.type(), proto.protocol()); | |||
| 90 | 2320x | return ec; | ||
| 91 | } | |||
| 92 | ||||
| 93 | std::error_code | |||
| 94 | 8x | tcp_acceptor::assign(native_handle_type fd) noexcept | ||
| 95 | { | |||
| 96 | #if BOOST_COROSIO_HAS_IOCP | |||
| 97 | 8x | auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service()); | ||
| 98 | #else | |||
| 99 | auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service()); | |||
| 100 | #endif | |||
| 101 | 8x | std::error_code ec = svc.assign_socket( | ||
| 102 | 8x | *static_cast<tcp_acceptor::implementation*>(h_.get()), fd); | ||
| 103 | 8x | return ec; | ||
| 104 | } | |||
| 105 | ||||
| 106 | native_handle_type | |||
| 107 | 5x | tcp_acceptor::release() | ||
| 108 | { | |||
| 109 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 4 times.
|
5x | if (!is_open()) | |
| 110 | 1x | detail::throw_system_error( | ||
| 111 | 1x | make_error_code(std::errc::bad_file_descriptor), | ||
| 112 | "tcp_acceptor::release"); | |||
| 113 | 4x | return get().release_socket(); | ||
| 114 | } | |||
| 115 | ||||
| 116 | native_handle_type | |||
| 117 | 13x | tcp_acceptor::native_handle() const noexcept | ||
| 118 | { | |||
| 119 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 11 times.
|
13x | if (!is_open()) | |
| 120 | { | |||
| 121 | #if BOOST_COROSIO_HAS_IOCP | |||
| 122 | 2x | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | ||
| 123 | #else | |||
| 124 | return -1; | |||
| 125 | #endif | |||
| 126 | } | |||
| 127 | 11x | return get().native_handle(); | ||
| 128 | } | |||
| 129 | ||||
| 130 | std::error_code | |||
| 131 | 2317x | tcp_acceptor::bind(endpoint ep) noexcept | ||
| 132 | { | |||
| 133 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2316 times.
|
2317x | if (!is_open()) | |
| 134 | 1x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 135 | #if BOOST_COROSIO_HAS_IOCP | |||
| 136 | 2316x | auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service()); | ||
| 137 | #else | |||
| 138 | auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service()); | |||
| 139 | #endif | |||
| 140 | 2316x | return svc.bind_acceptor( | ||
| 141 | 4632x | *static_cast<tcp_acceptor::implementation*>(h_.get()), ep); | ||
| 142 | } | |||
| 143 | ||||
| 144 | std::error_code | |||
| 145 | 2299x | tcp_acceptor::listen(int backlog) noexcept | ||
| 146 | { | |||
| 147 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2298 times.
|
2299x | if (!is_open()) | |
| 148 | 1x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 149 | #if BOOST_COROSIO_HAS_IOCP | |||
| 150 | 2298x | auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service()); | ||
| 151 | #else | |||
| 152 | auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service()); | |||
| 153 | #endif | |||
| 154 | 2298x | return svc.listen_acceptor( | ||
| 155 | 4596x | *static_cast<tcp_acceptor::implementation*>(h_.get()), backlog); | ||
| 156 | } | |||
| 157 | ||||
| 158 | void | |||
| 159 | 4602x | tcp_acceptor::close() noexcept | ||
| 160 | { | |||
| 161 |
2/2✓ Branch 3 → 4 taken 2282 times.
✓ Branch 3 → 5 taken 2320 times.
|
4602x | if (!is_open()) | |
| 162 | 2282x | return; | ||
| 163 | 2320x | h_.service().close(h_); | ||
| 164 | } | |||
| 165 | ||||
| 166 | void | |||
| 167 | 6x | tcp_acceptor::cancel() noexcept | ||
| 168 | { | |||
| 169 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 5 times.
|
6x | if (!is_open()) | |
| 170 | 1x | return; | ||
| 171 | 5x | get().cancel(); | ||
| 172 | } | |||
| 173 | ||||
| 174 | endpoint | |||
| 175 | 2299x | tcp_acceptor::local_endpoint() const noexcept | ||
| 176 | { | |||
| 177 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2298 times.
|
2299x | if (!is_open()) | |
| 178 | 1x | return endpoint{}; | ||
| 179 | 2298x | return get().local_endpoint(); | ||
| 180 | } | |||
| 181 | ||||
| 182 | } // namespace boost::corosio | |||
| 183 |