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