/src/kea/src/lib/asiolink/io_acceptor.h
Line | Count | Source |
1 | | // Copyright (C) 2017-2025 Internet Systems Consortium, Inc. ("ISC") |
2 | | // |
3 | | // This Source Code Form is subject to the terms of the Mozilla Public |
4 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | | |
7 | | #ifndef IO_ACCEPTOR_H |
8 | | #define IO_ACCEPTOR_H |
9 | | |
10 | | #ifndef BOOST_ASIO_HPP |
11 | | #error "asio.hpp must be included before including this, see asiolink.h as to why" |
12 | | #endif |
13 | | |
14 | | #include <asiolink/io_service.h> |
15 | | #include <asiolink/io_socket.h> |
16 | | |
17 | | namespace isc { |
18 | | namespace asiolink { |
19 | | |
20 | | /// @brief Base class for acceptor services in Kea. |
21 | | /// |
22 | | /// This is a wrapper class for ASIO acceptor service. Classes implementing |
23 | | /// services for specific protocol types should derive from this class. |
24 | | /// |
25 | | /// Acceptor is an IO object which accepts incoming connections into a socket |
26 | | /// object. This socket is then used for data transmission from the client |
27 | | /// to server and back. The acceptor is continued to be used to accept new |
28 | | /// connections while the accepted connection is active. |
29 | | /// |
30 | | /// @tparam ProtocolType ASIO protocol type, e.g. stream_protocol |
31 | | /// @tparam CallbackType Callback function type which should have the following |
32 | | /// signature: @c void(const boost::system::error_code&). |
33 | | template<typename ProtocolType, typename CallbackType> |
34 | | class IOAcceptor : public IOSocket { |
35 | | public: |
36 | | |
37 | | /// @brief Constructor. |
38 | | /// |
39 | | /// @param io_service Reference to the IO service. |
40 | | explicit IOAcceptor(const IOServicePtr& io_service) |
41 | 7.06k | : IOSocket(), io_service_(io_service), |
42 | 7.06k | acceptor_(new typename ProtocolType::acceptor(io_service_->getInternalIOService())) { |
43 | 7.06k | } isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::IOAcceptor(boost::shared_ptr<isc::asiolink::IOService> const&) Line | Count | Source | 41 | 6.84k | : IOSocket(), io_service_(io_service), | 42 | 6.84k | acceptor_(new typename ProtocolType::acceptor(io_service_->getInternalIOService())) { | 43 | 6.84k | } |
isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::IOAcceptor(boost::shared_ptr<isc::asiolink::IOService> const&) Line | Count | Source | 41 | 223 | : IOSocket(), io_service_(io_service), | 42 | 223 | acceptor_(new typename ProtocolType::acceptor(io_service_->getInternalIOService())) { | 43 | 223 | } |
|
44 | | |
45 | | /// @brief Destructor. |
46 | 7.06k | virtual ~IOAcceptor() { }isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::~IOAcceptor() Line | Count | Source | 46 | 6.84k | virtual ~IOAcceptor() { } |
isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::~IOAcceptor() Line | Count | Source | 46 | 223 | virtual ~IOAcceptor() { } |
|
47 | | |
48 | | /// @brief Returns file descriptor of the underlying socket. |
49 | 14.1k | virtual int getNative() const { |
50 | 14.1k | return (acceptor_->native_handle()); |
51 | 14.1k | } isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::getNative() const Line | Count | Source | 49 | 13.6k | virtual int getNative() const { | 50 | 13.6k | return (acceptor_->native_handle()); | 51 | 13.6k | } |
isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::getNative() const Line | Count | Source | 49 | 446 | virtual int getNative() const { | 50 | 446 | return (acceptor_->native_handle()); | 51 | 446 | } |
|
52 | | |
53 | | /// @brief Opens acceptor socket given the endpoint. |
54 | | /// |
55 | | /// @param endpoint Reference to the endpoint object defining local |
56 | | /// acceptor endpoint. |
57 | | /// |
58 | | /// @tparam EndpointType Endpoint type. |
59 | | template<typename EndpointType> |
60 | 7.06k | void open(const EndpointType& endpoint) { |
61 | 7.06k | acceptor_->open(endpoint.getASIOEndpoint().protocol()); |
62 | 7.06k | } void isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::open<isc::asiolink::UnixDomainSocketEndpoint>(isc::asiolink::UnixDomainSocketEndpoint const&) Line | Count | Source | 60 | 6.84k | void open(const EndpointType& endpoint) { | 61 | 6.84k | acceptor_->open(endpoint.getASIOEndpoint().protocol()); | 62 | 6.84k | } |
void isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::open<isc::asiolink::TCPEndpoint>(isc::asiolink::TCPEndpoint const&) Line | Count | Source | 60 | 223 | void open(const EndpointType& endpoint) { | 61 | 223 | acceptor_->open(endpoint.getASIOEndpoint().protocol()); | 62 | 223 | } |
|
63 | | |
64 | | /// @brief Binds socket to an endpoint. |
65 | | /// |
66 | | /// @param endpoint Reference to the endpoint object defining local |
67 | | /// acceptor endpoint. |
68 | | /// |
69 | | /// @tparam EndpointType Endpoint type. |
70 | | template<typename EndpointType> |
71 | 7.06k | void bind(const EndpointType& endpoint) { |
72 | 7.06k | acceptor_->bind(endpoint.getASIOEndpoint()); |
73 | 7.06k | } void isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::bind<isc::asiolink::UnixDomainSocketEndpoint>(isc::asiolink::UnixDomainSocketEndpoint const&) Line | Count | Source | 71 | 6.84k | void bind(const EndpointType& endpoint) { | 72 | 6.84k | acceptor_->bind(endpoint.getASIOEndpoint()); | 73 | 6.84k | } |
void isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::bind<isc::asiolink::TCPEndpoint>(isc::asiolink::TCPEndpoint const&) Line | Count | Source | 71 | 223 | void bind(const EndpointType& endpoint) { | 72 | 223 | acceptor_->bind(endpoint.getASIOEndpoint()); | 73 | 223 | } |
|
74 | | |
75 | | /// @brief Sets socket option. |
76 | | /// |
77 | | /// @param socket_option Reference to the object encapsulating an option to |
78 | | /// be set for the socket. |
79 | | /// @tparam SettableSocketOption Type of the object encapsulating socket option |
80 | | /// being set. |
81 | | template<typename SettableSocketOption> |
82 | 223 | void setOption(const SettableSocketOption& socket_option) { |
83 | 223 | acceptor_->set_option(socket_option); |
84 | 223 | } |
85 | | |
86 | | /// @brief Starts listening new connections. |
87 | 7.06k | void listen() { |
88 | 7.06k | acceptor_->listen(); |
89 | 7.06k | } isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::listen() Line | Count | Source | 87 | 6.84k | void listen() { | 88 | 6.84k | acceptor_->listen(); | 89 | 6.84k | } |
isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::listen() Line | Count | Source | 87 | 223 | void listen() { | 88 | 223 | acceptor_->listen(); | 89 | 223 | } |
|
90 | | |
91 | | /// @brief Checks if the acceptor is open. |
92 | | /// |
93 | | /// @return true if acceptor is open. |
94 | 7.28k | bool isOpen() const { |
95 | 7.28k | return (acceptor_->is_open()); |
96 | 7.28k | } isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::isOpen() const Line | Count | Source | 94 | 6.84k | bool isOpen() const { | 95 | 6.84k | return (acceptor_->is_open()); | 96 | 6.84k | } |
isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::isOpen() const Line | Count | Source | 94 | 446 | bool isOpen() const { | 95 | 446 | return (acceptor_->is_open()); | 96 | 446 | } |
|
97 | | |
98 | | /// @brief Closes the acceptor. |
99 | 7.28k | void close() const { |
100 | 7.28k | acceptor_->close(); |
101 | 7.28k | } isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::close() const Line | Count | Source | 99 | 6.84k | void close() const { | 100 | 6.84k | acceptor_->close(); | 101 | 6.84k | } |
isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::close() const Line | Count | Source | 99 | 446 | void close() const { | 100 | 446 | acceptor_->close(); | 101 | 446 | } |
|
102 | | |
103 | | protected: |
104 | | |
105 | | /// @brief Asynchronously accept new connection. |
106 | | /// |
107 | | /// This method accepts new connection into the specified socket. When the |
108 | | /// new connection arrives or an error occurs the specified callback |
109 | | /// function is invoked. |
110 | | /// |
111 | | /// @param socket Socket into which connection should be accepted. |
112 | | /// @param callback Callback function to be invoked when the new connection |
113 | | /// arrives. |
114 | | /// @tparam SocketType Socket type, e.g. @ref UnixDomainSocket. It must |
115 | | /// implement @c getASIOSocket method. |
116 | | template<typename SocketType> |
117 | | void asyncAcceptInternal(const SocketType& socket, |
118 | 14.1k | const CallbackType& callback) { |
119 | 14.1k | acceptor_->async_accept(socket.getASIOSocket(), callback); |
120 | 14.1k | } void isc::asiolink::IOAcceptor<boost::asio::local::stream_protocol, std::__1::function<void (boost::system::error_code const&)> >::asyncAcceptInternal<isc::asiolink::UnixDomainSocket>(isc::asiolink::UnixDomainSocket const&, std::__1::function<void (boost::system::error_code const&)> const&) Line | Count | Source | 118 | 13.6k | const CallbackType& callback) { | 119 | 13.6k | acceptor_->async_accept(socket.getASIOSocket(), callback); | 120 | 13.6k | } |
void isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::asyncAcceptInternal<isc::asiolink::TCPSocket<isc::http::HttpConnection::SocketCallback> >(isc::asiolink::TCPSocket<isc::http::HttpConnection::SocketCallback> const&, std::__1::function<void (boost::system::error_code const&)> const&) Line | Count | Source | 118 | 446 | const CallbackType& callback) { | 119 | 446 | acceptor_->async_accept(socket.getASIOSocket(), callback); | 120 | 446 | } |
Unexecuted instantiation: void isc::asiolink::IOAcceptor<boost::asio::ip::tcp, std::__1::function<void (boost::system::error_code const&)> >::asyncAcceptInternal<isc::asiolink::TCPSocket<isc::tcp::TcpConnection::SocketCallback> >(isc::asiolink::TCPSocket<isc::tcp::TcpConnection::SocketCallback> const&, std::__1::function<void (boost::system::error_code const&)> const&) |
121 | | |
122 | | /// @brief The IO service used to handle events. |
123 | | IOServicePtr io_service_; |
124 | | |
125 | | /// @brief Underlying ASIO acceptor implementation. |
126 | | boost::shared_ptr<typename ProtocolType::acceptor> acceptor_; |
127 | | |
128 | | }; |
129 | | |
130 | | } // end of namespace asiolink |
131 | | } // end of isc |
132 | | |
133 | | #endif // IO_ACCEPTOR_H |