blob: b177fad76bc18ff86cc8bb30538ae60a79c9bfb3 [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
56 * \param ioService io_service to create socket on
57 * \param receiveCallback callback function when a TLV block is received; must not be empty
58 * \throw boost::system::system_error connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080059 */
Junxiao Shi446de3c2016-07-25 22:38:16 +000060 virtual void
61 connect(boost::asio::io_service& ioService, const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062
Junxiao Shi446de3c2016-07-25 22:38:16 +000063 /** \brief Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070064 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000066 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080067
Junxiao Shi446de3c2016-07-25 22:38:16 +000068 /** \brief send a TLV block through the transport
Jeff Thompson432c8be2013-08-09 16:16:08 -070069 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000071 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070072
Junxiao Shi446de3c2016-07-25 22:38:16 +000073 /** \brief send two memory blocks through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080074 *
Junxiao Shi446de3c2016-07-25 22:38:16 +000075 * Scatter/gather API is utilized to send two non-consecutive memory blocks together
76 * (as part of the same message in datagram-oriented transports).
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080077 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000079 send(const Block& header, const Block& payload) = 0;
80
Junxiao Shi446de3c2016-07-25 22:38:16 +000081 /** \brief pause the transport
82 * \post receiveCallback will not be invoked
83 * \note This operation has no effect if transport has been paused,
84 * or when connection is being established.
85 */
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000086 virtual void
87 pause() = 0;
88
Junxiao Shi446de3c2016-07-25 22:38:16 +000089 /** \brief resume the transport
90 * \post receiveCallback will be invoked
91 * \note This operation has no effect if transport is not paused,
92 * or when connection is being established.
93 */
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000094 virtual void
95 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070096
Junxiao Shi446de3c2016-07-25 22:38:16 +000097 /** \retval true connection has been established
98 * \retval false connection is not yet established or has been closed
99 */
100 bool
101 isConnected() const;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800102
Junxiao Shi446de3c2016-07-25 22:38:16 +0000103 /** \retval true incoming packets are expected, receiveCallback will be invoked
104 * \retval false incoming packets are not expected, receiveCallback will not be invoked
105 */
106 bool
107 isReceiving() const;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000108
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800109protected:
Junxiao Shi446de3c2016-07-25 22:38:16 +0000110 /** \brief invoke the receive callback
111 */
112 void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800113 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800115protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800116 boost::asio::io_service* m_ioService;
117 bool m_isConnected;
Junxiao Shi446de3c2016-07-25 22:38:16 +0000118 bool m_isReceiving;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800119 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700120};
121
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122inline bool
Junxiao Shi446de3c2016-07-25 22:38:16 +0000123Transport::isConnected() const
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800124{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800125 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800126}
127
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000128inline bool
Junxiao Shi446de3c2016-07-25 22:38:16 +0000129Transport::isReceiving() const
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000130{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000131 return m_isReceiving;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000132}
133
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800134inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800135Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800136{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800137 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800138}
139
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800140} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700141
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142#endif // NDN_TRANSPORT_TRANSPORT_HPP