blob: c410769f8f3cd4405a94194c640f5731236cb6fa [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- 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 Afanasyeva9034b02014-01-26 18:32:02 -080010#include "common.hpp"
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080011#include "core/monotonic_deadline_timer.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080012
13namespace tcp {
14typedef boost::asio::ip::tcp::endpoint Endpoint;
15} // namespace tcp
16
17namespace ndn {
18
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080019/**
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 Afanasyeva9034b02014-01-26 18:32:02 -080026class TcpChannel // : protected SessionBasedChannel
27{
28public:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080029 /**
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 Afanasyeva9034b02014-01-26 18:32:02 -080035
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080036 /**
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 Afanasyeva9034b02014-01-26 18:32:02 -080057 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080058 listen(int backlog = boost::asio::ip::tcp::acceptor::max_connections);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080059
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080060 /**
61 * \brief Create a face by establishing connection to remote endpoint
62 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080063 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080064 connect(const tcp::Endpoint& remoteEndpoint,
65 const time::Duration& timeout = time::seconds(4));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080066
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080067 /**
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 Afanasyeva9034b02014-01-26 18:32:02 -080082private:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080083 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
103private:
104 boost::asio::io_service& m_ioService;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800105 tcp::Endpoint m_localEndpoint;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800106
107 bool isListening;
108 shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800109};
110
111} // namespace ndn
112
113#endif // NFD_FACE_TCP_CHANNEL_HPP