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 9589x tcp_socket::~tcp_socket()
24 {
25 9589x close();
26 9589x }
27
28 5094x tcp_socket::tcp_socket(capy::execution_context& ctx)
29 #if BOOST_COROSIO_HAS_IOCP
30
1/1
✓ Branch 2 → 3 taken 5094 times.
5094x : io_object(create_handle<detail::win_tcp_service>(ctx))
31 #else
32 : io_object(create_handle<detail::tcp_service>(ctx))
33 #endif
34 {
35 5094x }
36
37 std::error_code
38 2377x tcp_socket::open(tcp proto) noexcept
39 {
40
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 2375 times.
2377x if (is_open())
41 2x return {};
42 2375x return open_for_family(proto.family(), proto.type(), proto.protocol());
43 }
44
45 std::error_code
46 2375x tcp_socket::open_for_family(int family, int type, int protocol) noexcept
47 {
48 #if BOOST_COROSIO_HAS_IOCP
49 2375x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
50 2375x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
51 2375x std::error_code ec = svc.open_socket(
52 2375x *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), family, type,
53 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 2375x return ec;
61 }
62
63 std::error_code
64 8x tcp_socket::assign(native_handle_type fd) noexcept
65 {
66 #if BOOST_COROSIO_HAS_IOCP
67 8x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
68 8x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
69 8x std::error_code ec = svc.assign_socket(
70 8x *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 8x return ec;
77 }
78
79 native_handle_type
80 2x tcp_socket::release()
81 {
82
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 1 time.
2x if (!is_open())
83 1x detail::throw_system_error(
84 1x make_error_code(std::errc::bad_file_descriptor),
85 "tcp_socket::release");
86 1x return get().release_socket();
87 }
88
89 std::error_code
90 8x tcp_socket::bind(endpoint ep) noexcept
91 {
92
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 7 times.
8x if (!is_open())
93 1x return make_error_code(std::errc::bad_file_descriptor);
94 #if BOOST_COROSIO_HAS_IOCP
95 7x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
96 7x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
97 7x return svc.bind_socket(
98 14x *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 14894x tcp_socket::close() noexcept
108 {
109
2/2
✓ Branch 3 → 4 taken 10186 times.
✓ Branch 3 → 5 taken 4708 times.
14894x if (!is_open())
110 10186x return;
111 4708x h_.service().close(h_);
112 }
113
114 void
115 2174x tcp_socket::cancel() noexcept
116 {
117
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2173 times.
2174x if (!is_open())
118 1x return;
119 2173x get().cancel();
120 }
121
122 std::error_code
123 16x tcp_socket::shutdown(shutdown_type what) noexcept
124 {
125
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 13 times.
16x if (!is_open())
126 3x return make_error_code(std::errc::bad_file_descriptor);
127 13x return get().shutdown(what);
128 }
129
130 native_handle_type
131 16x tcp_socket::native_handle() const noexcept
132 {
133
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 15 times.
16x 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 15x 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 33x tcp_socket::remote_endpoint() const noexcept
154 {
155
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 28 times.
33x if (!is_open())
156 5x return endpoint{};
157 28x return get().remote_endpoint();
158 }
159
160 } // namespace boost::corosio
161