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 {
|
|
|
|
|
|
|
|
template<typename C>
|
2017-01-02 16:25:36 +01:00
|
|
|
class TCPAcceptor : public IOSocket{
|
2016-12-23 16:31:09 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
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 16:25:36 +01:00
|
|
|
virtual ~TCPAcceptor() { }
|
|
|
|
|
|
|
|
virtual int getNative() const {
|
|
|
|
return (acceptor_->native());
|
|
|
|
}
|
2016-12-23 16:31:09 +01:00
|
|
|
|
2017-01-02 16:25:36 +01:00
|
|
|
virtual int getProtocol() const {
|
|
|
|
return (IPPROTO_TCP);
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 16:25:36 +01:00
|
|
|
void open(const TCPEndpoint& endpoint) {
|
|
|
|
acceptor_->open(endpoint.getASIOEndpoint().protocol());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename SettableSocketOption>
|
|
|
|
void setOption(const SettableSocketOption& socket_option) {
|
|
|
|
acceptor_->set_option(socket_option);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void listen() {
|
2017-01-02 16:25:36 +01:00
|
|
|
acceptor_->listen();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool isOpen() const {
|
2017-01-02 16:25:36 +01:00
|
|
|
return (acceptor_->is_open());
|
2016-12-23 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
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
|