src/corosio/src/tcp_acceptor.cpp

95.8% Lines (46/48) 100.0% List of functions (9/9) 92.3% Branches (24/26)
tcp_acceptor.cpp
f(x) Functions (9)
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_acceptor.hpp>
12 #include <boost/corosio/socket_option.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_acceptor_service.hpp>
19 #endif
20
21 #include <boost/corosio/detail/except.hpp>
22
23 namespace boost::corosio {
24
25 1342x tcp_acceptor::~tcp_acceptor()
26 {
27 1342x close();
28 1342x }
29
30 1330x tcp_acceptor::tcp_acceptor(capy::execution_context& ctx)
31 #if BOOST_COROSIO_HAS_IOCP
32
1/1
✓ Branch 2 → 3 taken 1330 times.
1330x : io_object(create_handle<detail::win_tcp_acceptor_service>(ctx))
33 #else
34 : io_object(create_handle<detail::tcp_acceptor_service>(ctx))
35 #endif
36 {
37 1330x }
38
39 19x tcp_acceptor::tcp_acceptor(
40 19x capy::execution_context& ctx, endpoint ep, int backlog)
41 19x : tcp_acceptor(ctx)
42 {
43
3/3
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 18 times.
✓ Branch 7 → 8 taken 19 times.
19x open(ep.is_v6() ? tcp::v6() : tcp::v4());
44
1/1
✓ Branch 9 → 10 taken 19 times.
19x set_option(socket_option::reuse_address(true));
45
3/3
✓ Branch 10 → 11 taken 19 times.
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 18 times.
19x if (auto ec = bind(ep))
46 1x detail::throw_system_error(ec, "tcp_acceptor");
47
2/3
✓ Branch 14 → 15 taken 18 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 18 times.
18x if (auto ec = listen(backlog))
48 detail::throw_system_error(ec, "tcp_acceptor");
49 19x }
50
51 void
52 1336x tcp_acceptor::open(tcp proto)
53 {
54
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1335 times.
1336x if (is_open())
55 1x return;
56
57 #if BOOST_COROSIO_HAS_IOCP
58 1335x auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service());
59 #else
60 auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service());
61 #endif
62
1/1
✓ Branch 10 → 11 taken 1335 times.
2670x std::error_code ec = svc.open_acceptor_socket(
63 1335x *static_cast<tcp_acceptor::implementation*>(h_.get()), proto.family(),
64 proto.type(), proto.protocol());
65
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1335 times.
1335x if (ec)
66 detail::throw_system_error(ec, "tcp_acceptor::open");
67 }
68
69 std::error_code
70 1335x tcp_acceptor::bind(endpoint ep)
71 {
72
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1334 times.
1335x if (!is_open())
73 1x detail::throw_logic_error("bind: acceptor not open");
74 #if BOOST_COROSIO_HAS_IOCP
75 1334x auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service());
76 #else
77 auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service());
78 #endif
79 1334x return svc.bind_acceptor(
80 2668x *static_cast<tcp_acceptor::implementation*>(h_.get()), ep);
81 }
82
83 std::error_code
84 1324x tcp_acceptor::listen(int backlog)
85 {
86
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1323 times.
1324x if (!is_open())
87 1x detail::throw_logic_error("listen: acceptor not open");
88 #if BOOST_COROSIO_HAS_IOCP
89 1323x auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service());
90 #else
91 auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service());
92 #endif
93 1323x return svc.listen_acceptor(
94 2646x *static_cast<tcp_acceptor::implementation*>(h_.get()), backlog);
95 }
96
97 void
98 2641x tcp_acceptor::close()
99 {
100
2/2
✓ Branch 3 → 4 taken 1306 times.
✓ Branch 3 → 5 taken 1335 times.
2641x if (!is_open())
101 1306x return;
102 1335x h_.service().close(h_);
103 }
104
105 void
106 5x tcp_acceptor::cancel()
107 {
108
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 4 times.
5x if (!is_open())
109 1x return;
110 4x get().cancel();
111 }
112
113 endpoint
114 1319x tcp_acceptor::local_endpoint() const noexcept
115 {
116
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1318 times.
1319x if (!is_open())
117 1x return endpoint{};
118 1318x return get().local_endpoint();
119 }
120
121 } // namespace boost::corosio
122