src/corosio/src/tcp_socket.cpp

100.0% Lines (59 / 59) 100.0% Branches (22 / 22) 100.0% Functions (13 / 13)
tcp_socket.cpp
f(x) Functions (13)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
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 17281x tcp_socket::~tcp_socket()
38 {
39 17272x close();
40 17281x }
41
42 6248x tcp_socket::tcp_socket(capy::execution_context& ctx)
43 6248x : io_object(handle(
44 ctx,
45 12496x detail::use_backend_service<detail::tcp_service_of, tcp_service_key>(
46
2/2
✓ Branch 2 → 3 taken 6248 times.
✓ Branch 3 → 4 taken 6248 times.
12496x ctx)))
47 {
48 6248x }
49
50 std::error_code
51 3052x tcp_socket::open(family f) noexcept
52 {
53
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 3050 times.
3052x if (is_open())
54 2x return {};
55 3050x return open_for_family(detail::native_family(f), SOCK_STREAM, IPPROTO_TCP);
56 }
57
58 std::error_code
59 3050x tcp_socket::open_for_family(int family, int type, int protocol) noexcept
60 {
61 #if BOOST_COROSIO_HAS_IOCP
62 3050x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
63 3050x auto& sock = static_cast<detail::win_tcp_socket&>(*h_.get());
64 3050x std::error_code ec = svc.open_socket(sock, family, type, protocol);
65 #else
66 auto& svc = static_cast<detail::tcp_service&>(h_.service());
67 std::error_code ec = svc.open_socket(
68 static_cast<tcp_socket::implementation&>(*h_.get()), family, type,
69 protocol);
70 #endif
71 3050x return ec;
72 }
73
74 std::error_code
75 32x tcp_socket::assign(native_handle_type fd) noexcept
76 {
77
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 27 times.
32x if (is_open())
78 5x return make_error_code(error::already_open);
79 #if BOOST_COROSIO_HAS_IOCP
80 27x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
81 27x auto& sock = static_cast<detail::win_tcp_socket&>(*h_.get());
82 27x std::error_code ec = svc.assign_socket(sock, fd);
83 #else
84 auto& svc = static_cast<detail::tcp_service&>(h_.service());
85 std::error_code ec = svc.assign_socket(
86 static_cast<tcp_socket::implementation&>(*h_.get()), fd);
87 #endif
88 27x return ec;
89 }
90
91 capy::results<native_handle_type>
92 9x tcp_socket::release() noexcept
93 {
94
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 7 taken 8 times.
9x if (!is_open())
95 return {
96 2x make_error_code(std::errc::bad_file_descriptor),
97 1x detail::invalid_native_handle};
98 8x return {std::error_code{}, get().release_socket()};
99 }
100
101 std::error_code
102 9x tcp_socket::bind(endpoint ep) noexcept
103 {
104
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 8 times.
9x if (!is_open())
105 1x return make_error_code(std::errc::bad_file_descriptor);
106 #if BOOST_COROSIO_HAS_IOCP
107 8x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
108 8x auto& sock = static_cast<detail::win_tcp_socket&>(*h_.get());
109 8x return svc.bind_socket(sock, ep);
110 #else
111 auto& svc = static_cast<detail::tcp_service&>(h_.service());
112 return svc.bind_socket(
113 static_cast<tcp_socket::implementation&>(*h_.get()), ep);
114 #endif
115 }
116
117 void
118 21957x tcp_socket::close() noexcept
119 {
120
2/2
✓ Branch 3 → 4 taken 16837 times.
✓ Branch 3 → 5 taken 5120 times.
21957x if (!is_open())
121 16837x return;
122 5120x h_.service().close(h_);
123 }
124
125 void
126 2028x tcp_socket::cancel() noexcept
127 {
128
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 2026 times.
2028x if (!is_open())
129 2x return;
130 2026x get().cancel();
131 }
132
133 std::error_code
134 16x tcp_socket::shutdown(shutdown_type what) noexcept
135 {
136
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 13 times.
16x if (!is_open())
137 3x return make_error_code(std::errc::bad_file_descriptor);
138 13x return get().shutdown(what);
139 }
140
141 native_handle_type
142 30x tcp_socket::native_handle() const noexcept
143 {
144
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 29 times.
30x if (!is_open())
145 {
146 #if BOOST_COROSIO_HAS_IOCP
147 1x return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
148 #else
149 return -1;
150 #endif
151 }
152 29x return get().native_handle();
153 }
154
155 endpoint
156 34x tcp_socket::local_endpoint() const noexcept
157 {
158
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 29 times.
34x if (!is_open())
159 5x return endpoint{};
160 29x return get().local_endpoint();
161 }
162
163 endpoint
164 42x tcp_socket::remote_endpoint() const noexcept
165 {
166
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 37 times.
42x if (!is_open())
167 5x return endpoint{};
168 37x return get().remote_endpoint();
169 }
170
171 } // namespace boost::corosio
172