blob: d9ed7f24551ecf73110f2a2e9620cf9703d0740d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif9b880b2017-09-08 13:05:06 +00002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080020 */
21
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080022#ifndef NDN_TRANSPORT_TCP_TRANSPORT_HPP
23#define NDN_TRANSPORT_TCP_TRANSPORT_HPP
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080024
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080025#include "transport.hpp"
Junxiao Shid27b0692014-12-28 10:33:14 -070026#include "../util/config-file.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070027
Junxiao Shi446de3c2016-07-25 22:38:16 +000028namespace boost {
29namespace asio {
30namespace ip {
31class tcp;
32} // namespace ip
33} // namespace asio
34} // namespace boost
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080035
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080036namespace ndn {
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080037
Junxiao Shi446de3c2016-07-25 22:38:16 +000038template<typename BaseTransport, typename Protocol>
39class StreamTransportImpl;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080040
Junxiao Shi446de3c2016-07-25 22:38:16 +000041template<typename BaseTransport, typename Protocol>
42class StreamTransportWithResolverImpl;
43
44/** \brief a transport using TCP socket
45 */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080046class TcpTransport : public Transport
47{
48public:
Junxiao Shi446de3c2016-07-25 22:38:16 +000049 explicit
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080050 TcpTransport(const std::string& host, const std::string& port = "6363");
Junxiao Shi446de3c2016-07-25 22:38:16 +000051
Davide Pesavento57c07df2016-12-11 18:41:45 -050052 ~TcpTransport() override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080053
Davide Pesavento57c07df2016-12-11 18:41:45 -050054 void
Junxiao Shif9b880b2017-09-08 13:05:06 +000055 connect(boost::asio::io_service& ioService, const ReceiveCallback& receiveCallback) override;
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070056
Davide Pesavento57c07df2016-12-11 18:41:45 -050057 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000058 close() override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080059
Davide Pesavento57c07df2016-12-11 18:41:45 -050060 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000061 pause() override;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000062
Davide Pesavento57c07df2016-12-11 18:41:45 -050063 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000064 resume() override;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000065
Davide Pesavento57c07df2016-12-11 18:41:45 -050066 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000067 send(const Block& wire) override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080068
Davide Pesavento57c07df2016-12-11 18:41:45 -050069 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000070 send(const Block& header, const Block& payload) override;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080071
Junxiao Shi446de3c2016-07-25 22:38:16 +000072 /** \brief Create transport with parameters defined in URI
73 * \throw Transport::Error incorrect URI or unsupported protocol is specified
Alexander Afanasyev57e00362016-06-23 13:22:54 -070074 */
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070075 static shared_ptr<TcpTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070076 create(const std::string& uri);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070077
78NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070079 static std::pair<std::string, std::string>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070080 getSocketHostAndPortFromUri(const std::string& uri);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070081
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080082private:
83 std::string m_host;
84 std::string m_port;
85
Junxiao Shif9b880b2017-09-08 13:05:06 +000086 using Impl = StreamTransportWithResolverImpl<TcpTransport, boost::asio::ip::tcp>;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080087 friend class StreamTransportImpl<TcpTransport, boost::asio::ip::tcp>;
Junxiao Shif9b880b2017-09-08 13:05:06 +000088 friend Impl;
Junxiao Shi446de3c2016-07-25 22:38:16 +000089 shared_ptr<Impl> m_impl;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080090};
91
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080092} // namespace ndn
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080093
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080094#endif // NDN_TRANSPORT_TCP_TRANSPORT_HPP