src/corosio/src/tcp_socket.cpp

100.0% Lines (58/58) 100.0% List of functions (13/13) 100.0% Branches (19/19)
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 //
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 7964x tcp_socket::~tcp_socket()
24 {
25 7964x close();
26 7964x }
27
28 4313x tcp_socket::tcp_socket(capy::execution_context& ctx)
29 #if BOOST_COROSIO_HAS_IOCP
30
1/1
✓ Branch 2 → 3 taken 4313 times.
4313x : io_object(create_handle<detail::win_tcp_service>(ctx))
31 #else
32 : io_object(create_handle<detail::tcp_service>(ctx))
33 #endif
34 {
35 4313x }
36
37 std::error_code
38 1987x tcp_socket::open(tcp proto) noexcept
39 {
40
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 1985 times.
1987x if (is_open())
41 2x return {};
42 1985x return open_for_family(proto.family(), proto.type(), proto.protocol());
43 }
44
45 std::error_code
46 1985x tcp_socket::open_for_family(int family, int type, int protocol) noexcept
47 {
48 #if BOOST_COROSIO_HAS_IOCP
49 1985x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
50 1985x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
51 1985x std::error_code ec = svc.open_socket(
52 1985x *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), family,
53 type, protocol);
54 #else
55 auto& svc = static_cast<detail::tcp_service&>(h_.service());
56 std::error_code ec = svc.open_socket(
57 static_cast<tcp_socket::implementation&>(*h_.get()), family, type,
58 protocol);
59 #endif
60 1985x return ec;
61 }
62
63 std::error_code
64 29x tcp_socket::assign(native_handle_type fd) noexcept
65 {
66 #if BOOST_COROSIO_HAS_IOCP
67 29x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
68 29x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
69 29x std::error_code ec = svc.assign_socket(
70 29x *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), fd);
71 #else
72 auto& svc = static_cast<detail::tcp_service&>(h_.service());
73 std::error_code ec = svc.assign_socket(
74 static_cast<tcp_socket::implementation&>(*h_.get()), fd);
75 #endif
76 29x return ec;
77 }
78
79 native_handle_type
80 6x tcp_socket::release()
81 {
82
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 5 times.
6x if (!is_open())
83 1x detail::throw_system_error(
84 1x make_error_code(std::errc::bad_file_descriptor),
85 "tcp_socket::release");
86 5x return get().release_socket();
87 }
88
89 std::error_code
90 9x tcp_socket::bind(endpoint ep) noexcept
91 {
92
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 8 times.
9x if (!is_open())
93 1x return make_error_code(std::errc::bad_file_descriptor);
94 #if BOOST_COROSIO_HAS_IOCP
95 8x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
96 8x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
97 8x return svc.bind_socket(
98 16x *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), ep);
99 #else
100 auto& svc = static_cast<detail::tcp_service&>(h_.service());
101 return svc.bind_socket(
102 static_cast<tcp_socket::implementation&>(*h_.get()), ep);
103 #endif
104 }
105
106 void
107 12414x tcp_socket::close() noexcept
108 {
109
2/2
✓ Branch 3 → 4 taken 8531 times.
✓ Branch 3 → 5 taken 3883 times.
12414x if (!is_open())
110 8531x return;
111 3883x h_.service().close(h_);
112 }
113
114 void
115 2152x tcp_socket::cancel() noexcept
116 {
117
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2151 times.
2152x if (!is_open())
118 1x return;
119 2151x get().cancel();
120 }
121
122 std::error_code
123 17x tcp_socket::shutdown(shutdown_type what) noexcept
124 {
125
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 14 times.
17x if (!is_open())
126 3x return make_error_code(std::errc::bad_file_descriptor);
127 14x return get().shutdown(what);
128 }
129
130 native_handle_type
131 25x tcp_socket::native_handle() const noexcept
132 {
133
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 24 times.
25x if (!is_open())
134 {
135 #if BOOST_COROSIO_HAS_IOCP
136 1x return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
137 #else
138 return -1;
139 #endif
140 }
141 24x return get().native_handle();
142 }
143
144 endpoint
145 34x tcp_socket::local_endpoint() const noexcept
146 {
147
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 29 times.
34x if (!is_open())
148 5x return endpoint{};
149 29x return get().local_endpoint();
150 }
151
152 endpoint
153 34x tcp_socket::remote_endpoint() const noexcept
154 {
155
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 29 times.
34x if (!is_open())
156 5x return endpoint{};
157 29x return get().remote_endpoint();
158 }
159
160 } // namespace boost::corosio
161