transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
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.
20  */
21 
22 #ifndef NDN_CXX_TRANSPORT_TRANSPORT_HPP
23 #define NDN_CXX_TRANSPORT_TRANSPORT_HPP
24 
28 
29 #include <boost/system/error_code.hpp>
30 
31 namespace ndn {
32 
36 class Transport : noncopyable
37 {
38 public:
39  class Error : public std::runtime_error
40  {
41  public:
42  using std::runtime_error::runtime_error;
43 
44  Error(const boost::system::error_code& code, const std::string& msg);
45  };
46 
47  enum class State {
48  CLOSED,
49  CONNECTING,
50  RUNNING,
51  PAUSED,
52  };
53 
54  using ReceiveCallback = std::function<void(const Block&)>;
55 
56 public:
57  virtual
58  ~Transport() = default;
59 
66  virtual void
67  connect(boost::asio::io_context& ioCtx, ReceiveCallback receiveCallback);
68 
72  virtual void
73  close() = 0;
74 
78  virtual void
79  send(const Block& block) = 0;
80 
87  virtual void
88  pause() = 0;
89 
96  virtual void
97  resume() = 0;
98 
102  State
103  getState() const noexcept
104  {
105  return m_state;
106  }
107 
108 protected:
109  void
110  setState(State state) noexcept
111  {
112  m_state = state;
113  }
114 
115 protected:
116  boost::asio::io_context* m_ioCtx = nullptr;
118 
119 private:
120  State m_state = State::CLOSED;
121 };
122 
123 } // namespace ndn
124 
125 #endif // NDN_CXX_TRANSPORT_TRANSPORT_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Error(const boost::system::error_code &code, const std::string &msg)
Definition: transport.cpp:26
Provides a "TLV-oriented" delivery service.
Definition: transport.hpp:37
std::function< void(const Block &)> ReceiveCallback
Definition: transport.hpp:54
virtual void resume()=0
Resume the transport.
virtual void connect(boost::asio::io_context &ioCtx, ReceiveCallback receiveCallback)
Asynchronously open the connection.
Definition: transport.cpp:32
virtual void send(const Block &block)=0
Send a TLV block through the transport.
State getState() const noexcept
Return the current state of the transport.
Definition: transport.hpp:103
virtual ~Transport()=default
void setState(State state) noexcept
Definition: transport.hpp:110
ReceiveCallback m_receiveCallback
Definition: transport.hpp:117
virtual void close()=0
Close the connection.
boost::asio::io_context * m_ioCtx
Definition: transport.hpp:116
virtual void pause()=0
Pause the transport, canceling all pending operations.
Common includes and macros used throughout the library.
Definition: data.cpp:25