blob: f656802f0012d268d72146207dacb6e58752878e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Davide Pesavento537dc3a2016-02-18 19:35:26 +01003 * 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.
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"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070027
Davide Pesavento537dc3a2016-02-18 19:35:26 +010028#include <boost/system/error_code.hpp>
29
Davide Pesavento537dc3a2016-02-18 19:35:26 +010030namespace boost {
31namespace asio {
32class io_service;
33} // namespace asio
34} // namespace boost
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070035
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070036namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070037
Junxiao Shi446de3c2016-07-25 22:38:16 +000038/** \brief provides TLV-block delivery service
39 */
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040040class Transport : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070041{
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070042public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060043 class Error : public std::runtime_error
44 {
45 public:
Junxiao Shi446de3c2016-07-25 22:38:16 +000046 Error(const boost::system::error_code& code, const std::string& msg);
47
48 explicit
49 Error(const std::string& msg);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060050 };
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051
Junxiao Shi446de3c2016-07-25 22:38:16 +000052 typedef function<void(const Block& wire)> ReceiveCallback;
53 typedef function<void()> ErrorCallback;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080055 Transport();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056
Junxiao Shi446de3c2016-07-25 22:38:16 +000057 virtual
58 ~Transport() = default;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080059
Junxiao Shi446de3c2016-07-25 22:38:16 +000060 /** \brief asynchronously open the connection
61 * \param ioService io_service to create socket on
62 * \param receiveCallback callback function when a TLV block is received; must not be empty
63 * \throw boost::system::system_error connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080064 */
Junxiao Shi446de3c2016-07-25 22:38:16 +000065 virtual void
66 connect(boost::asio::io_service& ioService, const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067
Junxiao Shi446de3c2016-07-25 22:38:16 +000068 /** \brief Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070069 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000071 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080072
Junxiao Shi446de3c2016-07-25 22:38:16 +000073 /** \brief send a TLV block through the transport
Jeff Thompson432c8be2013-08-09 16:16:08 -070074 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000076 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070077
Junxiao Shi446de3c2016-07-25 22:38:16 +000078 /** \brief send two memory blocks through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080079 *
Junxiao Shi446de3c2016-07-25 22:38:16 +000080 * Scatter/gather API is utilized to send two non-consecutive memory blocks together
81 * (as part of the same message in datagram-oriented transports).
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080082 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000084 send(const Block& header, const Block& payload) = 0;
85
Junxiao Shi446de3c2016-07-25 22:38:16 +000086 /** \brief pause the transport
87 * \post receiveCallback will not be invoked
88 * \note This operation has no effect if transport has been paused,
89 * or when connection is being established.
90 */
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000091 virtual void
92 pause() = 0;
93
Junxiao Shi446de3c2016-07-25 22:38:16 +000094 /** \brief resume the transport
95 * \post receiveCallback will be invoked
96 * \note This operation has no effect if transport is not paused,
97 * or when connection is being established.
98 */
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000099 virtual void
100 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700101
Junxiao Shi446de3c2016-07-25 22:38:16 +0000102 /** \retval true connection has been established
103 * \retval false connection is not yet established or has been closed
104 */
105 bool
106 isConnected() const;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800107
Junxiao Shi446de3c2016-07-25 22:38:16 +0000108 /** \retval true incoming packets are expected, receiveCallback will be invoked
109 * \retval false incoming packets are not expected, receiveCallback will not be invoked
110 */
111 bool
112 isReceiving() const;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000113
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800114protected:
Junxiao Shi446de3c2016-07-25 22:38:16 +0000115 /** \brief invoke the receive callback
116 */
117 void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800118 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800120protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800121 boost::asio::io_service* m_ioService;
122 bool m_isConnected;
Junxiao Shi446de3c2016-07-25 22:38:16 +0000123 bool m_isReceiving;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800124 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700125};
126
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700127inline bool
Junxiao Shi446de3c2016-07-25 22:38:16 +0000128Transport::isConnected() const
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800129{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800130 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800131}
132
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000133inline bool
Junxiao Shi446de3c2016-07-25 22:38:16 +0000134Transport::isReceiving() const
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000135{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000136 return m_isReceiving;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000137}
138
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800139inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800140Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800141{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800143}
144
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800145} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700146
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800147#endif // NDN_TRANSPORT_TRANSPORT_HPP