2017-01-02 16:25:36 +01:00
|
|
|
// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
|
2016-12-23 16:31:09 +01:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
#ifndef TCP_ACCEPTOR_H
|
|
|
|
#define TCP_ACCEPTOR_H
|
|
|
|
|
|
|
|
#ifndef BOOST_ASIO_HPP
|
|
|
|
#error "asio.hpp must be included before including this, see asiolink.h as to why"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <asiolink/io_service.h>
|
2017-01-02 16:25:36 +01:00
|
|
|
#include <asiolink/io_socket.h>
|
2016-12-23 16:31:09 +01:00
|
|
|
#include <asiolink/tcp_endpoint.h>
|
|
|
|
#include <asiolink/tcp_socket.h>
|
2017-01-02 16:25:36 +01:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <netinet/in.h>
|
2016-12-23 16:31:09 +01:00
|
|
|
|
|
|
|
namespace isc {
|
|
|
|
namespace asiolink {
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Provides a service for accepting new TCP connections.
|
|
|
|
///
|
|
|
|
/// Internally it uses @c boost::asio::ip::tcp::acceptor class to implement
|
|
|
|
/// the acceptor service.
|
|
|
|
///
|
2017-01-06 14:14:21 +01:00
|
|
|
/// @tparam C Acceptor callback type.
|
2016-12-23 16:31:09 +01:00
|
|
|
template<typename C>
|
2017-01-02 17:25:52 +01:00
|
|
|
class TCPAcceptor : public IOSocket {
|
2016-12-23 16:31:09 +01:00
|
|
|
public:
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Constructor.
|
|
|
|
///
|
|
|
|
/// @param io_service IO service.
|
|
|
|
explicit TCPAcceptor(IOService& io_service)
|
2017-01-02 16:25:36 +01:00
|
|
|
: IOSocket(),
|
|
|
|
acceptor_(new boost::asio::ip::tcp::acceptor(io_service.get_io_service())) {
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Destructor.
|
2017-01-02 16:25:36 +01:00
|
|
|
virtual ~TCPAcceptor() { }
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Returns file descriptor of the underlying socket.
|
|
|
|
virtual int getNative() const final {
|
2017-01-02 16:25:36 +01:00
|
|
|
return (acceptor_->native());
|
|
|
|
}
|
2016-12-23 16:31:09 +01:00
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Returns protocol of the socket.
|
|
|
|
///
|
|
|
|
/// @return IPPROTO_TCP.
|
|
|
|
virtual int getProtocol() const final {
|
2017-01-02 16:25:36 +01:00
|
|
|
return (IPPROTO_TCP);
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Opens acceptor socket given the endpoint.
|
|
|
|
///
|
|
|
|
/// @param endpoint Reference to the endpoint object which specifies the
|
|
|
|
/// address and port on which the acceptor service will run.
|
2017-01-02 16:25:36 +01:00
|
|
|
void open(const TCPEndpoint& endpoint) {
|
|
|
|
acceptor_->open(endpoint.getASIOEndpoint().protocol());
|
|
|
|
}
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Sets socket option.
|
|
|
|
///
|
|
|
|
/// Typically, this method is used to set SO_REUSEADDR option on the socket:
|
|
|
|
/// @code
|
|
|
|
/// IOService io_service;
|
|
|
|
/// TCPAcceptor<Callback> acceptor(io_service);
|
|
|
|
/// acceptor.setOption(TCPAcceptor::ReuseAddress(true))
|
|
|
|
/// @endcode
|
|
|
|
///
|
|
|
|
/// @param socket_option Reference to the object encapsulating an option to
|
|
|
|
/// be set for the socket.
|
|
|
|
/// @tparam SettableSocketOption Type of the object encapsulating socket option
|
|
|
|
/// being set.
|
2017-01-02 16:25:36 +01:00
|
|
|
template<typename SettableSocketOption>
|
|
|
|
void setOption(const SettableSocketOption& socket_option) {
|
|
|
|
acceptor_->set_option(socket_option);
|
|
|
|
}
|
2016-12-23 16:31:09 +01:00
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Binds socket to an endpoint.
|
|
|
|
///
|
|
|
|
/// @param endpoint Reference to an endpoint to which the socket is to
|
|
|
|
/// be bound.
|
2016-12-23 16:31:09 +01:00
|
|
|
void bind(const TCPEndpoint& endpoint) {
|
2017-01-02 16:25:36 +01:00
|
|
|
acceptor_->bind(endpoint.getASIOEndpoint());
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Starts listening for the new connections.
|
2016-12-23 16:31:09 +01:00
|
|
|
void listen() {
|
2017-01-02 16:25:36 +01:00
|
|
|
acceptor_->listen();
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Asynchronously accept new connection.
|
|
|
|
///
|
|
|
|
/// This method accepts new connection into the specified socket. When the
|
|
|
|
/// new connection arrives or an error occurs the specified callback function
|
|
|
|
/// is invoked.
|
|
|
|
///
|
|
|
|
/// @param socket Socket into which connection should be accepted.
|
|
|
|
/// @param callback Callback function to be invoked when the new connection
|
|
|
|
/// arrives.
|
|
|
|
/// @tparam SocketCallback Type of the callback for the @ref TCPSocket.
|
2016-12-23 16:31:09 +01:00
|
|
|
template<typename SocketCallback>
|
|
|
|
void asyncAccept(const TCPSocket<SocketCallback>& socket, C& callback) {
|
2017-01-02 16:25:36 +01:00
|
|
|
acceptor_->async_accept(socket.getASIOSocket(), callback);
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Checks if the acceptor is open.
|
|
|
|
///
|
|
|
|
/// @return true if acceptor is open.
|
2016-12-23 16:31:09 +01:00
|
|
|
bool isOpen() const {
|
2017-01-02 16:25:36 +01:00
|
|
|
return (acceptor_->is_open());
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:57:55 +01:00
|
|
|
/// @brief Closes the acceptor.
|
|
|
|
void close() const {
|
|
|
|
acceptor_->close();
|
|
|
|
}
|
|
|
|
|
2016-12-23 16:31:09 +01:00
|
|
|
private:
|
|
|
|
|
2017-01-02 17:25:52 +01:00
|
|
|
/// @brief Underlying ASIO acceptor implementation.
|
2017-01-02 16:25:36 +01:00
|
|
|
boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
|
2016-12-23 16:31:09 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace asiolink
|
|
|
|
} // namespace isc
|
|
|
|
|
|
|
|
#endif
|