src/corosio/src/tcp_acceptor.cpp

96.3% Lines (52/54) 100.0% List of functions (9/9) 46.2% Branches (24/52)
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 2582x tcp_acceptor::~tcp_acceptor()
26 2582x {
27
1/2
✓ Branch 0 taken 1304 times.
✗ Branch 1 not taken.
1304x close();
28 2582x }
29
30 2552x tcp_acceptor::tcp_acceptor(capy::execution_context& ctx)
31 #if BOOST_COROSIO_HAS_IOCP
32 : io_object(create_handle<detail::win_tcp_acceptor_service>(ctx))
33 #else
34 1276x : io_object(create_handle<detail::tcp_acceptor_service>(ctx))
35 #endif
36 2552x {
37 2552x }
38
39 38x tcp_acceptor::tcp_acceptor(
40 capy::execution_context& ctx, endpoint ep, int backlog)
41 38x : tcp_acceptor(ctx)
42 {
43
3/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
38x open(ep.is_v6() ? tcp::v6() : tcp::v4());
44
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
38x set_option(socket_option::reuse_address(true));
45
3/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 36 times.
38x if (auto ec = bind(ep))
46
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2x detail::throw_system_error(ec, "tcp_acceptor");
47
2/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 36 times.
36x if (auto ec = listen(backlog))
48 detail::throw_system_error(ec, "tcp_acceptor");
49 38x }
50
51 void
52 1290x tcp_acceptor::open(tcp proto)
53 {
54
2/2
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 2 times.
1290x if (is_open())
55 2x return;
56
57 #if BOOST_COROSIO_HAS_IOCP
58 auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service());
59 #else
60 1288x auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service());
61 #endif
62 2576x std::error_code ec = svc.open_acceptor_socket(
63 1288x *static_cast<tcp_acceptor::implementation*>(h_.get()), proto.family(),
64 1288x proto.type(), proto.protocol());
65
1/2
✓ Branch 0 taken 1288 times.
✗ Branch 1 not taken.
1288x if (ec)
66 detail::throw_system_error(ec, "tcp_acceptor::open");
67 1290x }
68
69 std::error_code
70 1286x tcp_acceptor::bind(endpoint ep)
71 {
72
2/2
✓ Branch 0 taken 1284 times.
✓ Branch 1 taken 2 times.
1286x if (!is_open())
73 2x detail::throw_logic_error("bind: acceptor not open");
74 #if BOOST_COROSIO_HAS_IOCP
75 auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service());
76 #else
77 1284x auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service());
78 #endif
79 2568x return svc.bind_acceptor(
80 1284x *static_cast<tcp_acceptor::implementation*>(h_.get()), ep);
81 }
82
83 std::error_code
84 1262x tcp_acceptor::listen(int backlog)
85 {
86
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 2 times.
1262x if (!is_open())
87 2x detail::throw_logic_error("listen: acceptor not open");
88 #if BOOST_COROSIO_HAS_IOCP
89 auto& svc = static_cast<detail::win_tcp_acceptor_service&>(h_.service());
90 #else
91 1260x auto& svc = static_cast<detail::tcp_acceptor_service&>(h_.service());
92 #endif
93 2520x return svc.listen_acceptor(
94 1260x *static_cast<tcp_acceptor::implementation*>(h_.get()), backlog);
95 }
96
97 void
98 2508x tcp_acceptor::close()
99 {
100
2/2
✓ Branch 0 taken 1220 times.
✓ Branch 1 taken 1288 times.
2508x if (!is_open())
101 1220x return;
102 1288x h_.service().close(h_);
103 2508x }
104
105 void
106 10x tcp_acceptor::cancel()
107 {
108
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10x if (!is_open())
109 2x return;
110 8x get().cancel();
111 10x }
112
113 endpoint
114 1246x tcp_acceptor::local_endpoint() const noexcept
115 {
116
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1244 times.
1246x if (!is_open())
117 2x return endpoint{};
118 1244x return get().local_endpoint();
119 1246x }
120
121 } // namespace boost::corosio
122