Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_FACE_TCP_CHANNEL_HPP |
| 8 | #define NFD_FACE_TCP_CHANNEL_HPP |
| 9 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 10 | #include "common.hpp" |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 11 | #include "core/monotonic_deadline_timer.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 12 | |
| 13 | namespace tcp { |
| 14 | typedef boost::asio::ip::tcp::endpoint Endpoint; |
| 15 | } // namespace tcp |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 19 | /** |
| 20 | * \brief Class implementing TCP-based channel to create faces |
| 21 | * |
| 22 | * Channel can create faces as a response to incoming TCP |
| 23 | * connections (TcpChannel::listen needs to be called for that |
| 24 | * to work) or explicitly after using TcpChannel::connect method. |
| 25 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 26 | class TcpChannel // : protected SessionBasedChannel |
| 27 | { |
| 28 | public: |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 29 | /** |
| 30 | * \brief Prototype for the callback called when face is created |
| 31 | * (as a response to incoming connection or after connection |
| 32 | * is established) |
| 33 | */ |
| 34 | typedef function<void(const shared_ptr<TcpFace>& newFace)> FaceCreatedCallback; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 36 | /** |
| 37 | * \brief Prototype for the callback that is called when face is failed to |
| 38 | * get created |
| 39 | */ |
| 40 | typedef function<void(const std::string& reason)> ConnectFailedCallback; |
| 41 | |
| 42 | /** |
| 43 | * \brief Create TCP channel for the local endpoint |
| 44 | * |
| 45 | * To enable creation faces upon incoming connections, |
| 46 | * one needs to explicitly call TcpChannel::listen method. |
| 47 | */ |
| 48 | TcpChannel(boost::asio::io_service& ioService, |
| 49 | const tcp::Endpoint& localEndpoint); |
| 50 | |
| 51 | /** |
| 52 | * \brief Enable listening on the local endpoint, accept connections, |
| 53 | * and create faces when remote host makes a connection |
| 54 | * \param backlog The maximum length of the queue of pending incoming |
| 55 | * connections |
| 56 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 57 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 58 | listen(int backlog = boost::asio::ip::tcp::acceptor::max_connections); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 60 | /** |
| 61 | * \brief Create a face by establishing connection to remote endpoint |
| 62 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 63 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 64 | connect(const tcp::Endpoint& remoteEndpoint, |
| 65 | const time::Duration& timeout = time::seconds(4)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 67 | /** |
| 68 | * \brief Create a face by establishing connection to the specified |
| 69 | * remote host and remote port |
| 70 | * |
| 71 | * This method will never blocks and will return immediately. All |
| 72 | * necessary hostname and port resolution and connection will happen |
| 73 | * in asynchronous mode. |
| 74 | * |
| 75 | * If connection cannot be established within specified timeout, it |
| 76 | * will be aborted. |
| 77 | */ |
| 78 | void |
| 79 | connect(const std::string& remoteHost, const std::string& remotePort, |
| 80 | const time::Duration& timeout = time::seconds(4)); |
| 81 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 82 | private: |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 83 | void |
| 84 | handleConnection(const boost::system::error_code& error, |
| 85 | const shared_ptr<boost::asio::ip::tcp::socket>& socket); |
| 86 | |
| 87 | void |
| 88 | handleSuccessfulConnect(const boost::system::error_code& error, |
| 89 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 90 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer); |
| 91 | |
| 92 | void |
| 93 | handleFailedConnect(const boost::system::error_code& error, |
| 94 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 95 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer); |
| 96 | |
| 97 | void |
| 98 | handleEndpointResoution(const boost::system::error_code& error, |
| 99 | boost::asio::ip::tcp::resolver::iterator remoteEndpoint, |
| 100 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 101 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer); |
| 102 | |
| 103 | private: |
| 104 | boost::asio::io_service& m_ioService; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 105 | tcp::Endpoint m_localEndpoint; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame^] | 106 | |
| 107 | bool isListening; |
| 108 | shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace ndn |
| 112 | |
| 113 | #endif // NFD_FACE_TCP_CHANNEL_HPP |