summaryrefslogtreecommitdiffstats
path: root/src/lib/asiolink/tcp_acceptor.h
blob: 4fab1ddbbe8bf7a1c6c9f04a6b9ad7ad53bb6b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
//
// 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_acceptor.h>
#include <asiolink/io_service.h>
#include <asiolink/io_socket.h>
#include <asiolink/tcp_endpoint.h>
#include <asiolink/tcp_socket.h>
#include <boost/shared_ptr.hpp>
#include <netinet/in.h>

namespace isc {
namespace asiolink {

/// @brief Provides a service for accepting new TCP connections.
///
/// Internally it uses @c boost::asio::ip::tcp::acceptor class to implement
/// the acceptor service.
///
/// @tparam C Acceptor callback type.
template<typename C>
class TCPAcceptor : public IOAcceptor<boost::asio::ip::tcp, C> {
public:

    /// @brief Constructor.
    ///
    /// @param io_service IO service.
    explicit TCPAcceptor(IOService& io_service)
        : IOAcceptor<boost::asio::ip::tcp, C>(io_service) {
    }

    /// @brief Returns protocol of the socket.
    ///
    /// @return IPPROTO_TCP.
    virtual int getProtocol() const final {
        return (IPPROTO_TCP);
    }

    /// @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.
    template<typename SocketCallback>
    void asyncAccept(const TCPSocket<SocketCallback>& socket, C& callback) {
        IOAcceptor<boost::asio::ip::tcp, C>::asyncAcceptInternal(socket, callback);
    }
};


} // namespace asiolink
} // namespace isc

#endif