blob: f5b76ddd34ada57940cd12b33c0407e7a1f6dcdc [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4d0d0962017-12-19 22:23:14 -05002/*
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.
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070020 */
21
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080022#ifndef NDN_TRANSPORT_TRANSPORT_HPP
23#define NDN_TRANSPORT_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070024
Alexander Afanasyev19508852014-01-29 01:01:51 -080025#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080026#include "../encoding/block.hpp"
Davide Pesavento4d0d0962017-12-19 22:23:14 -050027#include "../net/asio-fwd.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070028
Davide Pesavento537dc3a2016-02-18 19:35:26 +010029#include <boost/system/error_code.hpp>
30
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070031namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070032
Junxiao Shi446de3c2016-07-25 22:38:16 +000033/** \brief provides TLV-block delivery service
34 */
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040035class Transport : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070036{
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070037public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060038 class Error : public std::runtime_error
39 {
40 public:
Junxiao Shi446de3c2016-07-25 22:38:16 +000041 Error(const boost::system::error_code& code, const std::string& msg);
42
43 explicit
44 Error(const std::string& msg);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060045 };
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046
Junxiao Shi446de3c2016-07-25 22:38:16 +000047 typedef function<void(const Block& wire)> ReceiveCallback;
48 typedef function<void()> ErrorCallback;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080050 Transport();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051
Junxiao Shi446de3c2016-07-25 22:38:16 +000052 virtual
53 ~Transport() = default;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080054
Junxiao Shi446de3c2016-07-25 22:38:16 +000055 /** \brief asynchronously open the connection
Junxiao Shi446de3c2016-07-25 22:38:16 +000056 * \param receiveCallback callback function when a TLV block is received; must not be empty
57 * \throw boost::system::system_error connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080058 */
Junxiao Shi446de3c2016-07-25 22:38:16 +000059 virtual void
Alexander Afanasyevc37bf162018-05-08 19:54:16 -040060 connect(const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061
Junxiao Shi446de3c2016-07-25 22:38:16 +000062 /** \brief Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070063 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000065 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080066
Junxiao Shi446de3c2016-07-25 22:38:16 +000067 /** \brief send a TLV block through the transport
Jeff Thompson432c8be2013-08-09 16:16:08 -070068 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000070 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070071
Junxiao Shi446de3c2016-07-25 22:38:16 +000072 /** \brief send two memory blocks through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080073 *
Junxiao Shi446de3c2016-07-25 22:38:16 +000074 * Scatter/gather API is utilized to send two non-consecutive memory blocks together
75 * (as part of the same message in datagram-oriented transports).
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080076 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000078 send(const Block& header, const Block& payload) = 0;
79
Junxiao Shi446de3c2016-07-25 22:38:16 +000080 /** \brief pause the transport
81 * \post receiveCallback will not be invoked
82 * \note This operation has no effect if transport has been paused,
83 * or when connection is being established.
84 */
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000085 virtual void
86 pause() = 0;
87
Junxiao Shi446de3c2016-07-25 22:38:16 +000088 /** \brief resume the transport
89 * \post receiveCallback will be invoked
90 * \note This operation has no effect if transport is not paused,
91 * or when connection is being established.
92 */
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000093 virtual void
94 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095
Junxiao Shi446de3c2016-07-25 22:38:16 +000096 /** \retval true connection has been established
97 * \retval false connection is not yet established or has been closed
98 */
99 bool
100 isConnected() const;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800101
Junxiao Shi446de3c2016-07-25 22:38:16 +0000102 /** \retval true incoming packets are expected, receiveCallback will be invoked
103 * \retval false incoming packets are not expected, receiveCallback will not be invoked
104 */
105 bool
106 isReceiving() const;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000107
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800108protected:
Junxiao Shi446de3c2016-07-25 22:38:16 +0000109 /** \brief invoke the receive callback
110 */
111 void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800112 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800114protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800115 bool m_isConnected;
Junxiao Shi446de3c2016-07-25 22:38:16 +0000116 bool m_isReceiving;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800117 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700118};
119
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120inline bool
Junxiao Shi446de3c2016-07-25 22:38:16 +0000121Transport::isConnected() const
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800122{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800123 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800124}
125
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000126inline bool
Junxiao Shi446de3c2016-07-25 22:38:16 +0000127Transport::isReceiving() const
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000128{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000129 return m_isReceiving;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000130}
131
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800132inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800133Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800134{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800135 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800136}
137
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800138} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700139
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800140#endif // NDN_TRANSPORT_TRANSPORT_HPP