blob: 6e8977fc0e29d59c6095f7ced77b285d6f30608a [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
Davide Pesavento4d0d0962017-12-19 22:23:14 -050028#include <boost/asio/ip/tcp.hpp>
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080029
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080030namespace ndn {
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080031
Junxiao Shi446de3c2016-07-25 22:38:16 +000032template<typename BaseTransport, typename Protocol>
33class StreamTransportImpl;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080034
Junxiao Shi446de3c2016-07-25 22:38:16 +000035template<typename BaseTransport, typename Protocol>
36class StreamTransportWithResolverImpl;
37
38/** \brief a transport using TCP socket
39 */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080040class TcpTransport : public Transport
41{
42public:
Junxiao Shi446de3c2016-07-25 22:38:16 +000043 explicit
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080044 TcpTransport(const std::string& host, const std::string& port = "6363");
Junxiao Shi446de3c2016-07-25 22:38:16 +000045
Davide Pesavento57c07df2016-12-11 18:41:45 -050046 ~TcpTransport() override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080047
Davide Pesavento57c07df2016-12-11 18:41:45 -050048 void
Junxiao Shif9b880b2017-09-08 13:05:06 +000049 connect(boost::asio::io_service& ioService, const ReceiveCallback& receiveCallback) override;
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070050
Davide Pesavento57c07df2016-12-11 18:41:45 -050051 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000052 close() override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080053
Davide Pesavento57c07df2016-12-11 18:41:45 -050054 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000055 pause() override;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000056
Davide Pesavento57c07df2016-12-11 18:41:45 -050057 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000058 resume() override;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000059
Davide Pesavento57c07df2016-12-11 18:41:45 -050060 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000061 send(const Block& wire) override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080062
Davide Pesavento57c07df2016-12-11 18:41:45 -050063 void
Junxiao Shi446de3c2016-07-25 22:38:16 +000064 send(const Block& header, const Block& payload) override;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080065
Junxiao Shi446de3c2016-07-25 22:38:16 +000066 /** \brief Create transport with parameters defined in URI
67 * \throw Transport::Error incorrect URI or unsupported protocol is specified
Alexander Afanasyev57e00362016-06-23 13:22:54 -070068 */
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070069 static shared_ptr<TcpTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070070 create(const std::string& uri);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070071
72NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070073 static std::pair<std::string, std::string>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070074 getSocketHostAndPortFromUri(const std::string& uri);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070075
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080076private:
77 std::string m_host;
78 std::string m_port;
79
Junxiao Shif9b880b2017-09-08 13:05:06 +000080 using Impl = StreamTransportWithResolverImpl<TcpTransport, boost::asio::ip::tcp>;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080081 friend class StreamTransportImpl<TcpTransport, boost::asio::ip::tcp>;
Junxiao Shif9b880b2017-09-08 13:05:06 +000082 friend Impl;
Junxiao Shi446de3c2016-07-25 22:38:16 +000083 shared_ptr<Impl> m_impl;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080084};
85
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080086} // namespace ndn
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080087
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080088#endif // NDN_TRANSPORT_TCP_TRANSPORT_HPP