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-2017 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_TRANSPORT_TRANSPORT_HPP
23 #define NDN_TRANSPORT_TRANSPORT_HPP
24 
25 #include "../common.hpp"
26 #include "../encoding/block.hpp"
27 #include "../net/asio-fwd.hpp"
28 
29 #include <boost/system/error_code.hpp>
30 
31 namespace ndn {
32 
35 class Transport : noncopyable
36 {
37 public:
38  class Error : public std::runtime_error
39  {
40  public:
41  Error(const boost::system::error_code& code, const std::string& msg);
42 
43  explicit
44  Error(const std::string& msg);
45  };
46 
47  typedef function<void(const Block& wire)> ReceiveCallback;
48  typedef function<void()> ErrorCallback;
49 
50  Transport();
51 
52  virtual
53  ~Transport() = default;
54 
60  virtual void
61  connect(boost::asio::io_service& ioService, const ReceiveCallback& receiveCallback);
62 
65  virtual void
66  close() = 0;
67 
70  virtual void
71  send(const Block& wire) = 0;
72 
78  virtual void
79  send(const Block& header, const Block& payload) = 0;
80 
86  virtual void
87  pause() = 0;
88 
94  virtual void
95  resume() = 0;
96 
100  bool
101  isConnected() const;
102 
106  bool
107  isReceiving() const;
108 
109 protected:
112  void
113  receive(const Block& wire);
114 
115 protected:
116  boost::asio::io_service* m_ioService;
119  ReceiveCallback m_receiveCallback;
120 };
121 
122 inline bool
124 {
125  return m_isConnected;
126 }
127 
128 inline bool
130 {
131  return m_isReceiving;
132 }
133 
134 inline void
136 {
137  m_receiveCallback(wire);
138 }
139 
140 } // namespace ndn
141 
142 #endif // NDN_TRANSPORT_TRANSPORT_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
Error(const boost::system::error_code &code, const std::string &msg)
Definition: transport.cpp:26
virtual void close()=0
Close the connection.
bool isConnected() const
Definition: transport.hpp:123
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
virtual void send(const Block &wire)=0
send a TLV block through the transport
ReceiveCallback m_receiveCallback
Definition: transport.hpp:119
function< void()> ErrorCallback
Definition: transport.hpp:48
virtual void resume()=0
resume the transport
boost::asio::io_service * m_ioService
Definition: transport.hpp:116
void receive(const Block &wire)
invoke the receive callback
Definition: transport.hpp:135
provides TLV-block delivery service
Definition: transport.hpp:35
virtual void pause()=0
pause the transport
virtual ~Transport()=default
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:47
bool isReceiving() const
Definition: transport.hpp:129
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
asynchronously open the connection
Definition: transport.cpp:44