src/corosio/src/tcp_socket.cpp
86.0% Lines (49/57)
90.9% List of functions (10/11)
70.0% Branches (14/20)
Functions (11)
Function
Calls
Lines
Branches
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:23
32510x
100.0%
50.0%
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:28
14210x
100.0%
–
100.0%
boost::corosio::tcp_socket::open(boost::corosio::tcp)
:38
7092x
80.0%
50.0%
75.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:46
7092x
87.5%
50.0%
66.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:65
14x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::close()
:82
30490x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::cancel()
:90
159x
80.0%
50.0%
75.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::tcp_socket::shutdown_type)
:98
12x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:108
0
0.0%
0.0%
0.0%
boost::corosio::tcp_socket::local_endpoint() const
:122
58x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:130
54x
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_socket.hpp> | |||
| 12 | #include <boost/corosio/detail/except.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_service.hpp> | |||
| 19 | #endif | |||
| 20 | ||||
| 21 | namespace boost::corosio { | |||
| 22 | ||||
| 23 | 32510x | tcp_socket::~tcp_socket() | ||
| 24 | 32510x | { | ||
| 25 |
1/2✓ Branch 0 taken 16278 times.
✗ Branch 1 not taken.
|
16278x | close(); | |
| 26 | 32510x | } | ||
| 27 | ||||
| 28 | 28420x | tcp_socket::tcp_socket(capy::execution_context& ctx) | ||
| 29 | #if BOOST_COROSIO_HAS_IOCP | |||
| 30 | : io_object(create_handle<detail::win_tcp_service>(ctx)) | |||
| 31 | #else | |||
| 32 | 14210x | : io_object(create_handle<detail::tcp_service>(ctx)) | ||
| 33 | #endif | |||
| 34 | 28420x | { | ||
| 35 | 14210x | } | ||
| 36 | ||||
| 37 | void | |||
| 38 | 7092x | tcp_socket::open(tcp proto) | ||
| 39 | { | |||
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7092 times.
|
7092x | if (is_open()) | |
| 41 | ✗ | return; | ||
| 42 | 7092x | open_for_family(proto.family(), proto.type(), proto.protocol()); | ||
| 43 | 7092x | } | ||
| 44 | ||||
| 45 | void | |||
| 46 | 7092x | tcp_socket::open_for_family(int family, int type, int protocol) | ||
| 47 | { | |||
| 48 | #if BOOST_COROSIO_HAS_IOCP | |||
| 49 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 50 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 51 | std::error_code ec = svc.open_socket( | |||
| 52 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), family, type, | |||
| 53 | protocol); | |||
| 54 | #else | |||
| 55 | 7092x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 56 | 14184x | std::error_code ec = svc.open_socket( | ||
| 57 | 7092x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | ||
| 58 | 7092x | protocol); | ||
| 59 | #endif | |||
| 60 |
1/2✓ Branch 0 taken 7092 times.
✗ Branch 1 not taken.
|
7092x | if (ec) | |
| 61 | ✗ | detail::throw_system_error(ec, "tcp_socket::open"); | ||
| 62 | 7092x | } | ||
| 63 | ||||
| 64 | std::error_code | |||
| 65 | 14x | tcp_socket::bind(endpoint ep) | ||
| 66 | { | |||
| 67 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
|
14x | if (!is_open()) | |
| 68 | 2x | detail::throw_logic_error("bind: socket not open"); | ||
| 69 | #if BOOST_COROSIO_HAS_IOCP | |||
| 70 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 71 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 72 | return svc.bind_socket( | |||
| 73 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), ep); | |||
| 74 | #else | |||
| 75 | 12x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 76 | 24x | return svc.bind_socket( | ||
| 77 | 12x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | ||
| 78 | #endif | |||
| 79 | } | |||
| 80 | ||||
| 81 | void | |||
| 82 | 30490x | tcp_socket::close() | ||
| 83 | { | |||
| 84 |
2/2✓ Branch 0 taken 16354 times.
✓ Branch 1 taken 14136 times.
|
30490x | if (!is_open()) | |
| 85 | 16354x | return; | ||
| 86 | 14136x | h_.service().close(h_); | ||
| 87 | 30490x | } | ||
| 88 | ||||
| 89 | void | |||
| 90 | 159x | tcp_socket::cancel() | ||
| 91 | { | |||
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
159x | if (!is_open()) | |
| 93 | ✗ | return; | ||
| 94 | 159x | get().cancel(); | ||
| 95 | 159x | } | ||
| 96 | ||||
| 97 | void | |||
| 98 | 12x | tcp_socket::shutdown(shutdown_type what) | ||
| 99 | { | |||
| 100 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12x | if (is_open()) | |
| 101 | { | |||
| 102 | // Best-effort: errors like ENOTCONN are expected and unhelpful | |||
| 103 | 6x | [[maybe_unused]] auto ec = get().shutdown(what); | ||
| 104 | 6x | } | ||
| 105 | 12x | } | ||
| 106 | ||||
| 107 | native_handle_type | |||
| 108 | ✗ | tcp_socket::native_handle() const noexcept | ||
| 109 | { | |||
| 110 | ✗ | if (!is_open()) | ||
| 111 | { | |||
| 112 | #if BOOST_COROSIO_HAS_IOCP | |||
| 113 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | |||
| 114 | #else | |||
| 115 | ✗ | return -1; | ||
| 116 | #endif | |||
| 117 | } | |||
| 118 | ✗ | return get().native_handle(); | ||
| 119 | ✗ | } | ||
| 120 | ||||
| 121 | endpoint | |||
| 122 | 58x | tcp_socket::local_endpoint() const noexcept | ||
| 123 | { | |||
| 124 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 48 times.
|
58x | if (!is_open()) | |
| 125 | 10x | return endpoint{}; | ||
| 126 | 48x | return get().local_endpoint(); | ||
| 127 | 58x | } | ||
| 128 | ||||
| 129 | endpoint | |||
| 130 | 54x | tcp_socket::remote_endpoint() const noexcept | ||
| 131 | { | |||
| 132 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 44 times.
|
54x | if (!is_open()) | |
| 133 | 10x | return endpoint{}; | ||
| 134 | 44x | return get().remote_endpoint(); | ||
| 135 | 54x | } | ||
| 136 | ||||
| 137 | } // namespace boost::corosio | |||
| 138 |