src/corosio/src/tcp_socket.cpp

85.7% Lines (42/49) 90.9% List of functions (10/11) 75.0% Branches (15/20)
tcp_socket.cpp
f(x) Functions (11)
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 4626x tcp_socket::~tcp_socket()
24 {
25 4626x close();
26 4626x }
27
28 2435x tcp_socket::tcp_socket(capy::execution_context& ctx)
29 #if BOOST_COROSIO_HAS_IOCP
30
1/1
✓ Branch 2 → 3 taken 2435 times.
2435x : io_object(create_handle<detail::win_tcp_service>(ctx))
31 #else
32 : io_object(create_handle<detail::tcp_service>(ctx))
33 #endif
34 {
35 2435x }
36
37 void
38 1192x tcp_socket::open(tcp proto)
39 {
40
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1192 times.
1192x if (is_open())
41 return;
42 1192x open_for_family(proto.family(), proto.type(), proto.protocol());
43 }
44
45 void
46 1192x tcp_socket::open_for_family(int family, int type, int protocol)
47 {
48 #if BOOST_COROSIO_HAS_IOCP
49 1192x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
50 1192x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
51
1/1
✓ Branch 5 → 6 taken 1192 times.
1192x std::error_code ec = svc.open_socket(
52 1192x *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
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1192 times.
1192x if (ec)
61 detail::throw_system_error(ec, "tcp_socket::open");
62 1192x }
63
64 std::error_code
65 7x tcp_socket::bind(endpoint ep)
66 {
67
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 6 times.
7x if (!is_open())
68 1x detail::throw_logic_error("bind: socket not open");
69 #if BOOST_COROSIO_HAS_IOCP
70 6x auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
71 6x auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
72 6x return svc.bind_socket(
73 12x *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), ep);
74 #else
75 auto& svc = static_cast<detail::tcp_service&>(h_.service());
76 return svc.bind_socket(
77 static_cast<tcp_socket::implementation&>(*h_.get()), ep);
78 #endif
79 }
80
81 void
82 7055x tcp_socket::close()
83 {
84
2/2
✓ Branch 3 → 4 taken 4695 times.
✓ Branch 3 → 5 taken 2360 times.
7055x if (!is_open())
85 4695x return;
86 2360x h_.service().close(h_);
87 }
88
89 void
90 2107x tcp_socket::cancel()
91 {
92
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2107 times.
2107x if (!is_open())
93 return;
94 2107x get().cancel();
95 }
96
97 void
98 6x tcp_socket::shutdown(shutdown_type what)
99 {
100
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 7 taken 3 times.
6x if (is_open())
101 {
102 // Best-effort: errors like ENOTCONN are expected and unhelpful
103 3x [[maybe_unused]] auto ec = get().shutdown(what);
104 }
105 6x }
106
107 native_handle_type
108 tcp_socket::native_handle() const noexcept
109 {
110 if (!is_open())
111 {
112 #if BOOST_COROSIO_HAS_IOCP
113 return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
114 #else
115 return -1;
116 #endif
117 }
118 return get().native_handle();
119 }
120
121 endpoint
122 29x tcp_socket::local_endpoint() const noexcept
123 {
124
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 24 times.
29x if (!is_open())
125 5x return endpoint{};
126 24x return get().local_endpoint();
127 }
128
129 endpoint
130 27x tcp_socket::remote_endpoint() const noexcept
131 {
132
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 22 times.
27x if (!is_open())
133 5x return endpoint{};
134 22x return get().remote_endpoint();
135 }
136
137 } // namespace boost::corosio
138