Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [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 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 7 | #ifndef NDN_TRANSPORT_TRANSPORT_HPP |
| 8 | #define NDN_TRANSPORT_TRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 9 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 10 | #include "../common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 11 | #include "../encoding/block.hpp" |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 13 | namespace ndn { |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 15 | class Transport { |
| 16 | public: |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 17 | struct Error : public std::runtime_error { inline Error(const boost::system::error_code &code, const std::string &msg); }; |
| 18 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 19 | typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback; |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 20 | typedef ptr_lib::function<void ()> ErrorCallback; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 21 | |
| 22 | inline |
| 23 | Transport(); |
| 24 | |
| 25 | inline virtual |
| 26 | ~Transport(); |
| 27 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 28 | /** |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 29 | * Connect transport |
| 30 | * |
| 31 | * @throws If connection cannot be established |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 32 | */ |
| 33 | inline virtual void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 34 | connect(boost::asio::io_service& io_service, |
| 35 | const ReceiveCallback& receiveCallback); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Close the connection. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 39 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 40 | virtual void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 41 | close() = 0; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 42 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 43 | /** |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 44 | * @brief Set data to the host |
| 45 | * |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 46 | * @param data A pointer to the buffer of data to send. |
| 47 | * @param dataLength The number of bytes in data. |
| 48 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 49 | virtual void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 50 | send(const Block& wire) = 0; |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 52 | /** |
| 53 | * @brief Alternative version of sending data, applying scatter/gather I/O concept |
| 54 | * |
| 55 | * Two non-consecutive memory blocks will be send out together, e.g., as part of the |
| 56 | * same message in datagram-oriented transports. |
| 57 | */ |
| 58 | virtual void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 59 | send(const Block& header, const Block& payload) = 0; |
| 60 | |
| 61 | virtual void |
| 62 | pause() = 0; |
| 63 | |
| 64 | virtual void |
| 65 | resume() = 0; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 67 | inline bool |
| 68 | isConnected(); |
| 69 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 70 | inline bool |
| 71 | isExpectingData(); |
| 72 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 73 | protected: |
| 74 | inline void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 75 | receive(const Block& wire); |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 77 | protected: |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 78 | boost::asio::io_service* m_ioService; |
| 79 | bool m_isConnected; |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 80 | bool m_isExpectingData; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 81 | ReceiveCallback m_receiveCallback; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 84 | inline |
| 85 | Transport::Transport() |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 86 | : m_ioService(0) |
| 87 | , m_isConnected(false) |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 88 | , m_isExpectingData(false) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 92 | inline 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] | 93 | : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : "")) |
| 94 | { |
| 95 | } |
| 96 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 97 | inline |
| 98 | Transport::~Transport() |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | inline void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 103 | Transport::connect(boost::asio::io_service& ioService, |
| 104 | const ReceiveCallback& receiveCallback) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 105 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 106 | m_ioService = &ioService; |
| 107 | m_receiveCallback = receiveCallback; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | inline bool |
| 111 | Transport::isConnected() |
| 112 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 113 | return m_isConnected; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 116 | inline bool |
| 117 | Transport::isExpectingData() |
| 118 | { |
| 119 | return m_isExpectingData; |
| 120 | } |
| 121 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 122 | inline void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 123 | Transport::receive(const Block& wire) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 124 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 125 | m_receiveCallback(wire); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 128 | } // namespace ndn |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 130 | #endif // NDN_TRANSPORT_TRANSPORT_HPP |