src/corosio/src/tcp_socket.cpp
100.0% Lines (66/66)
100.0% List of functions (13/13)
78.1% Branches (25/32)
Functions (13)
Function
Calls
Lines
Branches
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:23
38249x
100.0%
–
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:28
15557x
100.0%
–
100.0%
boost::corosio::tcp_socket::open(boost::corosio::tcp)
:38
7610x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:46
7606x
100.0%
50.0%
66.0%
boost::corosio::tcp_socket::assign(int)
:64
18x
100.0%
50.0%
66.0%
boost::corosio::tcp_socket::release()
:80
4x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:90
16x
100.0%
66.7%
83.0%
boost::corosio::tcp_socket::close()
:107
34714x
100.0%
75.0%
80.0%
boost::corosio::tcp_socket::cancel()
:115
194x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::shutdown_type)
:123
26x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:131
36x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::local_endpoint() const
:145
68x
100.0%
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:153
66x
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 | 38249x | tcp_socket::~tcp_socket() | ||
| 24 | 38249x | { | ||
| 25 | 19159x | close(); | ||
| 26 | 38249x | } | ||
| 27 | ||||
| 28 | 31114x | 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 | 15557x | : io_object(create_handle<detail::tcp_service>(ctx)) | ||
| 33 | #endif | |||
| 34 | 31114x | { | ||
| 35 | 15557x | } | ||
| 36 | ||||
| 37 | std::error_code | |||
| 38 | 7610x | tcp_socket::open(tcp proto) noexcept | ||
| 39 | { | |||
| 40 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7606 times.
|
7610x | if (is_open()) | |
| 41 | 4x | return {}; | ||
| 42 | 7606x | return open_for_family(proto.family(), proto.type(), proto.protocol()); | ||
| 43 | 7610x | } | ||
| 44 | ||||
| 45 | std::error_code | |||
| 46 | 7606x | tcp_socket::open_for_family(int family, int type, int protocol) noexcept | ||
| 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 | 7606x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 56 |
2/4✓ Branch 0 taken 7606 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7606 times.
✗ Branch 3 not taken.
|
15212x | std::error_code ec = svc.open_socket( | |
| 57 | 7606x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | ||
| 58 | 7606x | protocol); | ||
| 59 | #endif | |||
| 60 | 7606x | return ec; | ||
| 61 | } | |||
| 62 | ||||
| 63 | std::error_code | |||
| 64 | 18x | tcp_socket::assign(native_handle_type fd) noexcept | ||
| 65 | { | |||
| 66 | #if BOOST_COROSIO_HAS_IOCP | |||
| 67 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 68 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 69 | std::error_code ec = svc.assign_socket( | |||
| 70 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), fd); | |||
| 71 | #else | |||
| 72 | 18x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 73 |
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( | |
| 74 | 18x | static_cast<tcp_socket::implementation&>(*h_.get()), fd); | ||
| 75 | #endif | |||
| 76 | 18x | return ec; | ||
| 77 | } | |||
| 78 | ||||
| 79 | native_handle_type | |||
| 80 | 4x | tcp_socket::release() | ||
| 81 | { | |||
| 82 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4x | if (!is_open()) | |
| 83 | 2x | detail::throw_system_error( | ||
| 84 | 2x | make_error_code(std::errc::bad_file_descriptor), | ||
| 85 | "tcp_socket::release"); | |||
| 86 | 2x | return get().release_socket(); | ||
| 87 | } | |||
| 88 | ||||
| 89 | std::error_code | |||
| 90 | 16x | tcp_socket::bind(endpoint ep) noexcept | ||
| 91 | { | |||
| 92 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
|
16x | if (!is_open()) | |
| 93 | 2x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 94 | #if BOOST_COROSIO_HAS_IOCP | |||
| 95 | auto& svc = static_cast<detail::win_tcp_service&>(h_.service()); | |||
| 96 | auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get()); | |||
| 97 | return svc.bind_socket( | |||
| 98 | *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), ep); | |||
| 99 | #else | |||
| 100 | 14x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | ||
| 101 |
2/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
28x | return svc.bind_socket( | |
| 102 | 14x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | ||
| 103 | #endif | |||
| 104 | 16x | } | ||
| 105 | ||||
| 106 | void | |||
| 107 | 34714x | tcp_socket::close() noexcept | ||
| 108 | { | |||
| 109 |
2/2✓ Branch 0 taken 15132 times.
✓ Branch 1 taken 19582 times.
|
34714x | if (!is_open()) | |
| 110 | 19582x | return; | ||
| 111 |
1/2✓ Branch 0 taken 15132 times.
✗ Branch 1 not taken.
|
15132x | h_.service().close(h_); | |
| 112 | 34714x | } | ||
| 113 | ||||
| 114 | void | |||
| 115 | 194x | tcp_socket::cancel() noexcept | ||
| 116 | { | |||
| 117 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 192 times.
|
194x | if (!is_open()) | |
| 118 | 2x | return; | ||
| 119 | 192x | get().cancel(); | ||
| 120 | 194x | } | ||
| 121 | ||||
| 122 | std::error_code | |||
| 123 | 26x | tcp_socket::shutdown(shutdown_type what) noexcept | ||
| 124 | { | |||
| 125 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
|
26x | if (!is_open()) | |
| 126 | 6x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 127 | 20x | return get().shutdown(what); | ||
| 128 | 26x | } | ||
| 129 | ||||
| 130 | native_handle_type | |||
| 131 | 36x | tcp_socket::native_handle() const noexcept | ||
| 132 | { | |||
| 133 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34 times.
|
36x | if (!is_open()) | |
| 134 | { | |||
| 135 | #if BOOST_COROSIO_HAS_IOCP | |||
| 136 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | |||
| 137 | #else | |||
| 138 | 2x | return -1; | ||
| 139 | #endif | |||
| 140 | } | |||
| 141 | 34x | return get().native_handle(); | ||
| 142 | 36x | } | ||
| 143 | ||||
| 144 | endpoint | |||
| 145 | 68x | tcp_socket::local_endpoint() const noexcept | ||
| 146 | { | |||
| 147 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 58 times.
|
68x | if (!is_open()) | |
| 148 | 10x | return endpoint{}; | ||
| 149 | 58x | return get().local_endpoint(); | ||
| 150 | 68x | } | ||
| 151 | ||||
| 152 | endpoint | |||
| 153 | 66x | tcp_socket::remote_endpoint() const noexcept | ||
| 154 | { | |||
| 155 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 56 times.
|
66x | if (!is_open()) | |
| 156 | 10x | return endpoint{}; | ||
| 157 | 56x | return get().remote_endpoint(); | ||
| 158 | 66x | } | ||
| 159 | ||||
| 160 | } // namespace boost::corosio | |||
| 161 |