src/corosio/src/tcp_acceptor.cpp

93.1% Lines (27/29) 100.0% Functions (6/6) 70.0% Branches (7/10)
src/corosio/src/tcp_acceptor.cpp
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/detail/platform.hpp>
13
14 #if BOOST_COROSIO_HAS_IOCP
15 #include <boost/corosio/native/detail/iocp/win_acceptor_service.hpp>
16 #else
17 #include <boost/corosio/detail/acceptor_service.hpp>
18 #endif
19
20 #include <boost/corosio/detail/except.hpp>
21
22 namespace boost::corosio {
23
24 2026 tcp_acceptor::~tcp_acceptor()
25 2026 {
26
1/2
✓ Branch 0 taken 1022 times.
✗ Branch 1 not taken.
1022 close();
27 2026 }
28
29 2004 tcp_acceptor::tcp_acceptor(capy::execution_context& ctx)
30 #if BOOST_COROSIO_HAS_IOCP
31 : io_object(create_handle<detail::win_acceptor_service>(ctx))
32 #else
33 1002 : io_object(create_handle<detail::acceptor_service>(ctx))
34 #endif
35 2004 {
36 2004 }
37
38 std::error_code
39 1013 tcp_acceptor::listen(endpoint ep, int backlog)
40 {
41
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 1 time.
1013 if (is_open())
42 1 close();
43
44 #if BOOST_COROSIO_HAS_IOCP
45 auto& svc = static_cast<detail::win_acceptor_service&>(h_.service());
46 #else
47 1013 auto& svc = static_cast<detail::acceptor_service&>(h_.service());
48 #endif
49 2026 return svc.open_acceptor(
50 1013 *static_cast<tcp_acceptor::implementation*>(h_.get()), ep, backlog);
51 }
52
53 void
54 2018 tcp_acceptor::close()
55 {
56
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 1011 times.
2018 if (!is_open())
57 1007 return;
58 1011 h_.service().close(h_);
59 2018 }
60
61 void
62 2 tcp_acceptor::cancel()
63 {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!is_open())
65 return;
66 2 get().cancel();
67 2 }
68
69 endpoint
70 989 tcp_acceptor::local_endpoint() const noexcept
71 {
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 989 times.
989 if (!is_open())
73 return endpoint{};
74 989 return get().local_endpoint();
75 989 }
76
77 } // namespace boost::corosio
78