src/corosio/src/tcp_acceptor.cpp

91.7% Lines (22/24) 100.0% Functions (6/6)
src/corosio/src/tcp_acceptor.cpp
Line 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/acceptor_service.hpp"
17 #endif
18
19 #include <boost/corosio/detail/except.hpp>
20
21 namespace boost::corosio {
22
23 111 tcp_acceptor::~tcp_acceptor()
24 {
25 111 close();
26 111 }
27
28 109 tcp_acceptor::tcp_acceptor(capy::execution_context& ctx)
29 #if BOOST_COROSIO_HAS_IOCP
30 : io_object(create_handle<detail::win_acceptor_service>(ctx))
31 #else
32 109 : io_object(create_handle<detail::acceptor_service>(ctx))
33 #endif
34 {
35 109 }
36
37 std::error_code
38 106 tcp_acceptor::listen(endpoint ep, int backlog)
39 {
40 106 if (is_open())
41 1 close();
42
43 #if BOOST_COROSIO_HAS_IOCP
44 auto& svc = static_cast<detail::win_acceptor_service&>(h_.service());
45 #else
46 106 auto& svc = static_cast<detail::acceptor_service&>(h_.service());
47 #endif
48 106 return svc.open_acceptor(
49 212 *static_cast<tcp_acceptor::implementation*>(h_.get()), ep, backlog);
50 }
51
52 void
53 206 tcp_acceptor::close()
54 {
55 206 if (!is_open())
56 102 return;
57 104 h_.service().close(h_);
58 }
59
60 void
61 2 tcp_acceptor::cancel()
62 {
63 2 if (!is_open())
64 return;
65 2 get().cancel();
66 }
67
68 endpoint
69 86 tcp_acceptor::local_endpoint() const noexcept
70 {
71 86 if (!is_open())
72 return endpoint{};
73 86 return get().local_endpoint();
74 }
75
76 } // namespace boost::corosio
77