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