blob: eb7878bb09ac4d2523ad6ac4d3c44e74a5509063 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08002/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 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
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080052 ~TcpTransport();
53
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070054 virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080055 connect(boost::asio::io_service& ioService,
Junxiao Shi446de3c2016-07-25 22:38:16 +000056 const ReceiveCallback& receiveCallback) override;
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070057
58 virtual void
Junxiao Shi446de3c2016-07-25 22:38:16 +000059 close() override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080060
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000061 virtual void
Junxiao Shi446de3c2016-07-25 22:38:16 +000062 pause() override;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000063
64 virtual void
Junxiao Shi446de3c2016-07-25 22:38:16 +000065 resume() override;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000066
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070067 virtual void
Junxiao Shi446de3c2016-07-25 22:38:16 +000068 send(const Block& wire) override;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080069
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070070 virtual void
Junxiao Shi446de3c2016-07-25 22:38:16 +000071 send(const Block& header, const Block& payload) override;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080072
Junxiao Shi446de3c2016-07-25 22:38:16 +000073 /** \brief Create transport with parameters defined in URI
74 * \throw Transport::Error incorrect URI or unsupported protocol is specified
Alexander Afanasyev57e00362016-06-23 13:22:54 -070075 */
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070076 static shared_ptr<TcpTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070077 create(const std::string& uri);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070078
79NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070080 static std::pair<std::string, std::string>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070081 getSocketHostAndPortFromUri(const std::string& uri);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070082
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080083private:
84 std::string m_host;
85 std::string m_port;
86
87 typedef StreamTransportWithResolverImpl<TcpTransport, boost::asio::ip::tcp> Impl;
88 friend class StreamTransportImpl<TcpTransport, boost::asio::ip::tcp>;
89 friend class StreamTransportWithResolverImpl<TcpTransport, boost::asio::ip::tcp>;
Junxiao Shi446de3c2016-07-25 22:38:16 +000090 shared_ptr<Impl> m_impl;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080091};
92
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080093} // namespace ndn
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080094
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080095#endif // NDN_TRANSPORT_TCP_TRANSPORT_HPP