Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 2 | /** |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 22 | #ifndef NDN_TRANSPORT_TRANSPORT_HPP |
| 23 | #define NDN_TRANSPORT_TRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 25 | #include "../common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 26 | #include "../encoding/block.hpp" |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 28 | #include <boost/system/error_code.hpp> |
| 29 | |
| 30 | // forward declaration |
| 31 | namespace boost { |
| 32 | namespace asio { |
| 33 | class io_service; |
| 34 | } // namespace asio |
| 35 | } // namespace boost |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 37 | namespace ndn { |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 39 | class Transport : noncopyable |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 40 | { |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 41 | public: |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 42 | class Error : public std::runtime_error |
| 43 | { |
| 44 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | inline Error(const boost::system::error_code& code, const std::string& msg); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 46 | inline Error(const std::string& msg); |
| 47 | }; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 49 | typedef function<void (const Block& wire)> ReceiveCallback; |
| 50 | typedef function<void ()> ErrorCallback; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 52 | inline |
| 53 | Transport(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 55 | inline virtual |
| 56 | ~Transport(); |
| 57 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 58 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 59 | * @brief Connect transport |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 60 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 61 | * @throws boost::system::system_error if connection cannot be established |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 62 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 63 | inline virtual void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 64 | connect(boost::asio::io_service& io_service, |
| 65 | const ReceiveCallback& receiveCallback); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 67 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 68 | * @brief Close the connection. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 69 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 70 | virtual void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 71 | close() = 0; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 72 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 73 | /** |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 74 | * @brief Send block of data from @p wire through the transport |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 75 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 76 | * @param wire A block of data to send |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 77 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 78 | virtual void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 79 | send(const Block& wire) = 0; |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 81 | /** |
| 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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 87 | virtual void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 88 | send(const Block& header, const Block& payload) = 0; |
| 89 | |
| 90 | virtual void |
| 91 | pause() = 0; |
| 92 | |
| 93 | virtual void |
| 94 | resume() = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | |
| 96 | inline bool |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 97 | isConnected(); |
| 98 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 99 | inline bool |
| 100 | isExpectingData(); |
| 101 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 102 | protected: |
| 103 | inline void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 104 | receive(const Block& wire); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 106 | protected: |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 107 | boost::asio::io_service* m_ioService; |
| 108 | bool m_isConnected; |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 109 | bool m_isExpectingData; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 110 | ReceiveCallback m_receiveCallback; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 113 | inline |
| 114 | Transport::Transport() |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 115 | : m_ioService(0) |
| 116 | , m_isConnected(false) |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 117 | , m_isExpectingData(false) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 118 | { |
| 119 | } |
| 120 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 121 | inline |
| 122 | Transport::Error::Error(const boost::system::error_code& code, const std::string& msg) |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 123 | : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : "")) |
| 124 | { |
| 125 | } |
| 126 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 127 | inline |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 128 | Transport::Error::Error(const std::string& msg) |
| 129 | : std::runtime_error(msg) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | inline |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 134 | Transport::~Transport() |
| 135 | { |
| 136 | } |
| 137 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 138 | inline void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 139 | Transport::connect(boost::asio::io_service& ioService, |
| 140 | const ReceiveCallback& receiveCallback) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 142 | m_ioService = &ioService; |
| 143 | m_receiveCallback = receiveCallback; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | inline bool |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 147 | Transport::isConnected() |
| 148 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 149 | return m_isConnected; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 152 | inline bool |
| 153 | Transport::isExpectingData() |
| 154 | { |
| 155 | return m_isExpectingData; |
| 156 | } |
| 157 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 158 | inline void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 159 | Transport::receive(const Block& wire) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 161 | m_receiveCallback(wire); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 164 | } // namespace ndn |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 166 | #endif // NDN_TRANSPORT_TRANSPORT_HPP |