src/corosio/src/tcp_socket.cpp
100.0% Lines (59 / 59)
100.0% Functions (13 / 13)
Functions (13)
Function
Calls
Lines
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:38
10254x
100.0%
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:43
9469x
100.0%
100.0%
boost::corosio::tcp_socket::open(boost::corosio::family)
:52
4726x
100.0%
100.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:60
4722x
100.0%
100.0%
boost::corosio::tcp_socket::assign(int)
:78
31x
100.0%
100.0%
boost::corosio::tcp_socket::release()
:96
6x
100.0%
100.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:106
21x
100.0%
100.0%
boost::corosio::tcp_socket::close()
:123
19038x
100.0%
100.0%
boost::corosio::tcp_socket::cancel()
:131
222x
100.0%
100.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::shutdown_type)
:139
27x
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:147
71x
100.0%
100.0%
boost::corosio::tcp_socket::local_endpoint() const
:161
68x
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:169
68x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 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 | 10254x | tcp_socket::~tcp_socket() | |
| 39 | { | ||
| 40 | 10238x | close(); | |
| 41 | 10254x | } | |
| 42 | |||
| 43 | 9469x | tcp_socket::tcp_socket(capy::execution_context& ctx) | |
| 44 | 9469x | : io_object(handle( | |
| 45 | ctx, | ||
| 46 | 9469x | detail::use_backend_service<detail::tcp_service_of, tcp_service_key>( | |
| 47 | 28407x | ctx))) | |
| 48 | { | ||
| 49 | 9469x | } | |
| 50 | |||
| 51 | std::error_code | ||
| 52 | 4726x | tcp_socket::open(family f) noexcept | |
| 53 | { | ||
| 54 | 4726x | if (is_open()) | |
| 55 | 4x | return {}; | |
| 56 | 4722x | return open_for_family(detail::native_family(f), SOCK_STREAM, IPPROTO_TCP); | |
| 57 | } | ||
| 58 | |||
| 59 | std::error_code | ||
| 60 | 4722x | 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 | 4722x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 70 | 4722x | std::error_code ec = svc.open_socket( | |
| 71 | 4722x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | |
| 72 | protocol); | ||
| 73 | #endif | ||
| 74 | 4722x | return ec; | |
| 75 | } | ||
| 76 | |||
| 77 | std::error_code | ||
| 78 | 31x | tcp_socket::assign(native_handle_type fd) noexcept | |
| 79 | { | ||
| 80 | 31x | 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 | 23x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 89 | 23x | std::error_code ec = svc.assign_socket( | |
| 90 | 23x | static_cast<tcp_socket::implementation&>(*h_.get()), fd); | |
| 91 | #endif | ||
| 92 | 23x | return ec; | |
| 93 | } | ||
| 94 | |||
| 95 | native_handle_type | ||
| 96 | 6x | tcp_socket::release() | |
| 97 | { | ||
| 98 | 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 | 21x | tcp_socket::bind(endpoint ep) noexcept | |
| 107 | { | ||
| 108 | 21x | 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 | 19x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 117 | 19x | return svc.bind_socket( | |
| 118 | 38x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | |
| 119 | #endif | ||
| 120 | } | ||
| 121 | |||
| 122 | void | ||
| 123 | 19038x | tcp_socket::close() noexcept | |
| 124 | { | ||
| 125 | 19038x | if (!is_open()) | |
| 126 | 9740x | return; | |
| 127 | 9298x | h_.service().close(h_); | |
| 128 | } | ||
| 129 | |||
| 130 | void | ||
| 131 | 222x | tcp_socket::cancel() noexcept | |
| 132 | { | ||
| 133 | 222x | if (!is_open()) | |
| 134 | 2x | return; | |
| 135 | 220x | get().cancel(); | |
| 136 | } | ||
| 137 | |||
| 138 | std::error_code | ||
| 139 | 27x | tcp_socket::shutdown(shutdown_type what) noexcept | |
| 140 | { | ||
| 141 | 27x | if (!is_open()) | |
| 142 | 6x | return make_error_code(std::errc::bad_file_descriptor); | |
| 143 | 21x | return get().shutdown(what); | |
| 144 | } | ||
| 145 | |||
| 146 | native_handle_type | ||
| 147 | 71x | tcp_socket::native_handle() const noexcept | |
| 148 | { | ||
| 149 | 71x | 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 | 69x | return get().native_handle(); | |
| 158 | } | ||
| 159 | |||
| 160 | endpoint | ||
| 161 | 68x | tcp_socket::local_endpoint() const noexcept | |
| 162 | { | ||
| 163 | 68x | if (!is_open()) | |
| 164 | 10x | return endpoint{}; | |
| 165 | 58x | return get().local_endpoint(); | |
| 166 | } | ||
| 167 | |||
| 168 | endpoint | ||
| 169 | 68x | tcp_socket::remote_endpoint() const noexcept | |
| 170 | { | ||
| 171 | 68x | if (!is_open()) | |
| 172 | 10x | return endpoint{}; | |
| 173 | 58x | return get().remote_endpoint(); | |
| 174 | } | ||
| 175 | |||
| 176 | } // namespace boost::corosio | ||
| 177 |