2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-24 02:47:11 +00:00
kea/src/lib/asiolink/tcp_acceptor.h

80 lines
1.9 KiB
C
Raw Normal View History

// 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>
#include <asiolink/io_socket.h>
2016-12-23 16:31:09 +01:00
#include <asiolink/tcp_endpoint.h>
#include <asiolink/tcp_socket.h>
#include <boost/shared_ptr.hpp>
#include <netinet/in.h>
2016-12-23 16:31:09 +01:00
namespace isc {
namespace asiolink {
template<typename C>
class TCPAcceptor : public IOSocket{
2016-12-23 16:31:09 +01:00
public:
TCPAcceptor(IOService& io_service)
: IOSocket(),
acceptor_(new boost::asio::ip::tcp::acceptor(io_service.get_io_service())) {
2016-12-23 16:31:09 +01:00
}
virtual ~TCPAcceptor() { }
virtual int getNative() const {
return (acceptor_->native());
}
2016-12-23 16:31:09 +01:00
virtual int getProtocol() const {
return (IPPROTO_TCP);
2016-12-23 16:31:09 +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) {
acceptor_->bind(endpoint.getASIOEndpoint());
2016-12-23 16:31:09 +01:00
}
void listen() {
acceptor_->listen();
2016-12-23 16:31:09 +01:00
}
template<typename SocketCallback>
void asyncAccept(const TCPSocket<SocketCallback>& socket, C& callback) {
acceptor_->async_accept(socket.getASIOSocket(), callback);
2016-12-23 16:31:09 +01:00
}
bool isOpen() const {
return (acceptor_->is_open());
2016-12-23 16:31:09 +01:00
}
private:
boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
2016-12-23 16:31:09 +01:00
};
} // namespace asiolink
} // namespace isc
#endif