src/corosio/src/tcp_acceptor.cpp
87.5% Lines (42/48)
100.0% Functions (9/9)
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 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_acceptor_service.hpp> | ||
| 17 | #else | ||
| 18 | #include <boost/corosio/detail/acceptor_service.hpp> | ||
| 19 | #endif | ||
| 20 | |||
| 21 | #include <boost/corosio/detail/except.hpp> | ||
| 22 | |||
| 23 | namespace boost::corosio { | ||
| 24 | |||
| 25 | 145 | tcp_acceptor::~tcp_acceptor() | |
| 26 | { | ||
| 27 | 145 | close(); | |
| 28 | 145 | } | |
| 29 | |||
| 30 | 139 | tcp_acceptor::tcp_acceptor(capy::execution_context& ctx) | |
| 31 | #if BOOST_COROSIO_HAS_IOCP | ||
| 32 | : io_object(create_handle<detail::win_acceptor_service>(ctx)) | ||
| 33 | #else | ||
| 34 | 139 | : io_object(create_handle<detail::acceptor_service>(ctx)) | |
| 35 | #endif | ||
| 36 | { | ||
| 37 | 139 | } | |
| 38 | |||
| 39 | 13 | tcp_acceptor::tcp_acceptor( | |
| 40 | 13 | capy::execution_context& ctx, endpoint ep, int backlog) | |
| 41 | 13 | : tcp_acceptor(ctx) | |
| 42 | { | ||
| 43 | 13 | open(ep.is_v6() ? tcp::v6() : tcp::v4()); | |
| 44 | 13 | set_option(socket_option::reuse_address(true)); | |
| 45 | 13 | if (auto ec = bind(ep)) | |
| 46 | 1 | detail::throw_system_error(ec, "tcp_acceptor"); | |
| 47 | 12 | if (auto ec = listen(backlog)) | |
| 48 | ✗ | detail::throw_system_error(ec, "tcp_acceptor"); | |
| 49 | 13 | } | |
| 50 | |||
| 51 | void | ||
| 52 | 142 | tcp_acceptor::open(tcp proto) | |
| 53 | { | ||
| 54 | 142 | if (is_open()) | |
| 55 | 2 | return; | |
| 56 | |||
| 57 | #if BOOST_COROSIO_HAS_IOCP | ||
| 58 | auto& svc = static_cast<detail::win_acceptor_service&>(h_.service()); | ||
| 59 | #else | ||
| 60 | 140 | auto& svc = static_cast<detail::acceptor_service&>(h_.service()); | |
| 61 | #endif | ||
| 62 | 280 | std::error_code ec = svc.open_acceptor_socket( | |
| 63 | 140 | *static_cast<tcp_acceptor::implementation*>(h_.get()), | |
| 64 | proto.family(), proto.type(), proto.protocol()); | ||
| 65 | 140 | if (ec) | |
| 66 | ✗ | detail::throw_system_error(ec, "tcp_acceptor::open"); | |
| 67 | } | ||
| 68 | |||
| 69 | std::error_code | ||
| 70 | 138 | tcp_acceptor::bind(endpoint ep) | |
| 71 | { | ||
| 72 | 138 | if (!is_open()) | |
| 73 | ✗ | detail::throw_logic_error("bind: acceptor not open"); | |
| 74 | #if BOOST_COROSIO_HAS_IOCP | ||
| 75 | auto& svc = static_cast<detail::win_acceptor_service&>(h_.service()); | ||
| 76 | #else | ||
| 77 | 138 | auto& svc = static_cast<detail::acceptor_service&>(h_.service()); | |
| 78 | #endif | ||
| 79 | 138 | return svc.bind_acceptor( | |
| 80 | 276 | *static_cast<tcp_acceptor::implementation*>(h_.get()), ep); | |
| 81 | } | ||
| 82 | |||
| 83 | std::error_code | ||
| 84 | 134 | tcp_acceptor::listen(int backlog) | |
| 85 | { | ||
| 86 | 134 | if (!is_open()) | |
| 87 | ✗ | detail::throw_logic_error("listen: acceptor not open"); | |
| 88 | #if BOOST_COROSIO_HAS_IOCP | ||
| 89 | auto& svc = static_cast<detail::win_acceptor_service&>(h_.service()); | ||
| 90 | #else | ||
| 91 | 134 | auto& svc = static_cast<detail::acceptor_service&>(h_.service()); | |
| 92 | #endif | ||
| 93 | 134 | return svc.listen_acceptor( | |
| 94 | 268 | *static_cast<tcp_acceptor::implementation*>(h_.get()), backlog); | |
| 95 | } | ||
| 96 | |||
| 97 | void | ||
| 98 | 275 | tcp_acceptor::close() | |
| 99 | { | ||
| 100 | 275 | if (!is_open()) | |
| 101 | 135 | return; | |
| 102 | 140 | h_.service().close(h_); | |
| 103 | } | ||
| 104 | |||
| 105 | void | ||
| 106 | 4 | tcp_acceptor::cancel() | |
| 107 | { | ||
| 108 | 4 | if (!is_open()) | |
| 109 | ✗ | return; | |
| 110 | 4 | get().cancel(); | |
| 111 | } | ||
| 112 | |||
| 113 | endpoint | ||
| 114 | 124 | tcp_acceptor::local_endpoint() const noexcept | |
| 115 | { | ||
| 116 | 124 | if (!is_open()) | |
| 117 | ✗ | return endpoint{}; | |
| 118 | 124 | return get().local_endpoint(); | |
| 119 | } | ||
| 120 | |||
| 121 | } // namespace boost::corosio | ||
| 122 |