src/corosio/src/tcp_socket.cpp
100.0% Lines (55/0/55)
100.0% List of functions (13/0/13)
Functions (13)
Function
Calls
Lines
Blocks
boost::corosio::tcp_socket::~tcp_socket()
:23
14740x
100.0%
100.0%
boost::corosio::tcp_socket::tcp_socket(boost::capy::execution_context&)
:28
14230x
100.0%
100.0%
boost::corosio::tcp_socket::open(boost::corosio::tcp)
:38
7086x
100.0%
100.0%
boost::corosio::tcp_socket::open_for_family(int, int, int)
:46
7082x
100.0%
100.0%
boost::corosio::tcp_socket::assign(int)
:64
18x
100.0%
100.0%
boost::corosio::tcp_socket::release()
:80
4x
100.0%
100.0%
boost::corosio::tcp_socket::bind(boost::corosio::endpoint)
:90
16x
100.0%
100.0%
boost::corosio::tcp_socket::close()
:107
28561x
100.0%
100.0%
boost::corosio::tcp_socket::cancel()
:115
212x
100.0%
100.0%
boost::corosio::tcp_socket::shutdown(boost::corosio::shutdown_type)
:123
25x
100.0%
100.0%
boost::corosio::tcp_socket::native_handle() const
:131
34x
100.0%
100.0%
boost::corosio::tcp_socket::local_endpoint() const
:145
68x
100.0%
100.0%
boost::corosio::tcp_socket::remote_endpoint() const
:153
66x
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 | // | ||
| 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 | 14740x | tcp_socket::~tcp_socket() | |
| 24 | { | ||
| 25 | 14740x | close(); | |
| 26 | 14740x | } | |
| 27 | |||
| 28 | 14230x | 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 | 14230x | : io_object(create_handle<detail::tcp_service>(ctx)) | |
| 33 | #endif | ||
| 34 | { | ||
| 35 | 14230x | } | |
| 36 | |||
| 37 | std::error_code | ||
| 38 | 7086x | tcp_socket::open(tcp proto) noexcept | |
| 39 | { | ||
| 40 | 7086x | if (is_open()) | |
| 41 | 4x | return {}; | |
| 42 | 7082x | return open_for_family(proto.family(), proto.type(), proto.protocol()); | |
| 43 | } | ||
| 44 | |||
| 45 | std::error_code | ||
| 46 | 7082x | 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 | 7082x | auto& svc = static_cast<detail::tcp_service&>(h_.service()); | |
| 56 | 7082x | std::error_code ec = svc.open_socket( | |
| 57 | 7082x | static_cast<tcp_socket::implementation&>(*h_.get()), family, type, | |
| 58 | protocol); | ||
| 59 | #endif | ||
| 60 | 7082x | 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 | 18x | 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 | 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 | 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 | 14x | return svc.bind_socket( | |
| 102 | 28x | static_cast<tcp_socket::implementation&>(*h_.get()), ep); | |
| 103 | #endif | ||
| 104 | } | ||
| 105 | |||
| 106 | void | ||
| 107 | 28561x | tcp_socket::close() noexcept | |
| 108 | { | ||
| 109 | 28561x | if (!is_open()) | |
| 110 | 14477x | return; | |
| 111 | 14084x | h_.service().close(h_); | |
| 112 | } | ||
| 113 | |||
| 114 | void | ||
| 115 | 212x | tcp_socket::cancel() noexcept | |
| 116 | { | ||
| 117 | 212x | if (!is_open()) | |
| 118 | 2x | return; | |
| 119 | 210x | get().cancel(); | |
| 120 | } | ||
| 121 | |||
| 122 | std::error_code | ||
| 123 | 25x | tcp_socket::shutdown(shutdown_type what) noexcept | |
| 124 | { | ||
| 125 | 25x | if (!is_open()) | |
| 126 | 6x | return make_error_code(std::errc::bad_file_descriptor); | |
| 127 | 19x | return get().shutdown(what); | |
| 128 | } | ||
| 129 | |||
| 130 | native_handle_type | ||
| 131 | 34x | tcp_socket::native_handle() const noexcept | |
| 132 | { | ||
| 133 | 34x | 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 | 32x | return get().native_handle(); | |
| 142 | } | ||
| 143 | |||
| 144 | endpoint | ||
| 145 | 68x | tcp_socket::local_endpoint() const noexcept | |
| 146 | { | ||
| 147 | 68x | if (!is_open()) | |
| 148 | 10x | return endpoint{}; | |
| 149 | 58x | return get().local_endpoint(); | |
| 150 | } | ||
| 151 | |||
| 152 | endpoint | ||
| 153 | 66x | tcp_socket::remote_endpoint() const noexcept | |
| 154 | { | ||
| 155 | 66x | if (!is_open()) | |
| 156 | 10x | return endpoint{}; | |
| 157 | 56x | return get().remote_endpoint(); | |
| 158 | } | ||
| 159 | |||
| 160 | } // namespace boost::corosio | ||
| 161 |