src/corosio/src/tcp_acceptor.cpp

92.3% Lines (24/26) 100.0% Functions (6/6) 77.8% Branches (7/9)
src/corosio/src/tcp_acceptor.cpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #include <boost/corosio/tcp_acceptor.hpp>
11 #include <boost/corosio/detail/platform.hpp>
12
13 #if BOOST_COROSIO_HAS_IOCP
14 #include "src/detail/iocp/sockets.hpp"
15 #else
16 #include "src/detail/socket_service.hpp"
17 #endif
18
19 #include <boost/corosio/detail/except.hpp>
20
21 namespace boost::corosio {
22
23 111 tcp_acceptor::
24 111 ~tcp_acceptor()
25 {
26 111 close();
27 111 }
28
29 109 tcp_acceptor::
30 tcp_acceptor(
31 109 capy::execution_context& ctx)
32 #if BOOST_COROSIO_HAS_IOCP
33 : io_object(create_handle<detail::win_acceptor_service>(ctx))
34 #else
35
1/1
✓ Branch 1 taken 109 times.
109 : io_object(create_handle<detail::acceptor_service>(ctx))
36 #endif
37 {
38 109 }
39
40 std::error_code
41 106 tcp_acceptor::
42 listen(endpoint ep, int backlog)
43 {
44
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 105 times.
106 if (is_open())
45 1 close();
46
47 #if BOOST_COROSIO_HAS_IOCP
48 auto& svc = static_cast<detail::win_acceptor_service&>(h_.service());
49 #else
50 106 auto& svc = static_cast<detail::acceptor_service&>(h_.service());
51 #endif
52 106 return svc.open_acceptor(
53 212 *static_cast<tcp_acceptor::implementation*>(h_.get()), ep, backlog);
54 }
55
56 void
57 206 tcp_acceptor::
58 close()
59 {
60
2/2
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 104 times.
206 if (!is_open())
61 102 return;
62 104 h_.service().close(h_);
63 }
64
65 void
66 2 tcp_acceptor::
67 cancel()
68 {
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!is_open())
70 return;
71 2 get().cancel();
72 }
73
74 endpoint
75 86 tcp_acceptor::
76 local_endpoint() const noexcept
77 {
78
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if (!is_open())
79 return endpoint{};
80 86 return get().local_endpoint();
81 }
82
83 } // namespace boost::corosio
84