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, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame^] | 94 | const ConnectFailedCallback& onConnectFailed, |
| 95 | bool remoteConnection); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 96 | |
| 97 | void |
| 98 | handleSuccessfulConnect(const boost::system::error_code& error, |
| 99 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 100 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer, |
| 101 | const FaceCreatedCallback& onFaceCreated, |
| 102 | const ConnectFailedCallback& onConnectFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 103 | |
| 104 | void |
| 105 | handleFailedConnect(const boost::system::error_code& error, |
| 106 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 107 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer, |
| 108 | const ConnectFailedCallback& onConnectFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 109 | |
| 110 | void |
| 111 | handleEndpointResoution(const boost::system::error_code& error, |
| 112 | boost::asio::ip::tcp::resolver::iterator remoteEndpoint, |
| 113 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 114 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer, |
| 115 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame^] | 116 | const ConnectFailedCallback& onConnectFailed, |
| 117 | const shared_ptr<boost::asio::ip::tcp::resolver>& resolver); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 118 | |
| 119 | private: |
| 120 | boost::asio::io_service& m_ioService; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 121 | tcp::Endpoint m_localEndpoint; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 123 | typedef std::map< tcp::Endpoint, shared_ptr<TcpFace> > ChannelFaceMap; |
| 124 | ChannelFaceMap m_channelFaces; |
| 125 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 126 | bool isListening; |
| 127 | shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 130 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 131 | |
| 132 | #endif // NFD_FACE_TCP_CHANNEL_HPP |