blob: 1095f1489f45ab5b38ea878ed95ba2b6d194a5f3 [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
30// forward declaration
31namespace boost {
32namespace asio {
33class io_service;
34} // namespace asio
35} // namespace boost
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070036
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070037namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070038
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040039class Transport : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070040{
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070041public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060042 class Error : public std::runtime_error
43 {
44 public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 inline Error(const boost::system::error_code& code, const std::string& msg);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060046 inline Error(const std::string& msg);
47 };
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070049 typedef function<void (const Block& wire)> ReceiveCallback;
50 typedef function<void ()> ErrorCallback;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080052 inline
53 Transport();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080055 inline virtual
56 ~Transport();
57
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070058 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070059 * @brief Connect transport
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080060 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070061 * @throws boost::system::system_error if connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080062 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 inline virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080064 connect(boost::asio::io_service& io_service,
65 const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080067 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070068 * @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
Jeff Thompson432c8be2013-08-09 16:16:08 -070073 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +020074 * @brief Send block of data from @p wire through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080075 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070076 * @param wire A block of data to send
Jeff Thompson432c8be2013-08-09 16:16:08 -070077 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000079 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070080
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080081 /**
82 * @brief Alternative version of sending data, applying scatter/gather I/O concept
83 *
84 * Two non-consecutive memory blocks will be send out together, e.g., as part of the
85 * same message in datagram-oriented transports.
86 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000088 send(const Block& header, const Block& payload) = 0;
89
90 virtual void
91 pause() = 0;
92
93 virtual void
94 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095
96 inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080097 isConnected();
98
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000099 inline bool
100 isExpectingData();
101
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800102protected:
103 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800104 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800106protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800107 boost::asio::io_service* m_ioService;
108 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000109 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800110 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700111};
112
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800113inline
114Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800115 : m_ioService(0)
116 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000117 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800118{
119}
120
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600121inline
122Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800123 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
124{
125}
126
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800127inline
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600128Transport::Error::Error(const std::string& msg)
129 : std::runtime_error(msg)
130{
131}
132
133inline
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800134Transport::~Transport()
135{
136}
137
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800139Transport::connect(boost::asio::io_service& ioService,
140 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800141{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142 m_ioService = &ioService;
143 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800144}
145
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800147Transport::isConnected()
148{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800149 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800150}
151
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000152inline bool
153Transport::isExpectingData()
154{
155 return m_isExpectingData;
156}
157
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800158inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800159Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800160{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800161 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800162}
163
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800164} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700165
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800166#endif // NDN_TRANSPORT_TRANSPORT_HPP