transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_TRANSPORT_TRANSPORT_HPP
23 #define NDN_TRANSPORT_TRANSPORT_HPP
24 
25 #include "../common.hpp"
26 #include "../encoding/block.hpp"
27 
28 #include <boost/system/error_code.hpp>
29 
30 namespace boost {
31 namespace asio {
32 class io_service;
33 } // namespace asio
34 } // namespace boost
35 
36 namespace ndn {
37 
40 class Transport : noncopyable
41 {
42 public:
43  class Error : public std::runtime_error
44  {
45  public:
46  Error(const boost::system::error_code& code, const std::string& msg);
47 
48  explicit
49  Error(const std::string& msg);
50  };
51 
52  typedef function<void(const Block& wire)> ReceiveCallback;
53  typedef function<void()> ErrorCallback;
54 
55  Transport();
56 
57  virtual
58  ~Transport() = default;
59 
65  virtual void
66  connect(boost::asio::io_service& ioService, const ReceiveCallback& receiveCallback);
67 
70  virtual void
71  close() = 0;
72 
75  virtual void
76  send(const Block& wire) = 0;
77 
83  virtual void
84  send(const Block& header, const Block& payload) = 0;
85 
91  virtual void
92  pause() = 0;
93 
99  virtual void
100  resume() = 0;
101 
105  bool
106  isConnected() const;
107 
111  bool
112  isReceiving() const;
113 
114 protected:
117  void
118  receive(const Block& wire);
119 
120 protected:
121  boost::asio::io_service* m_ioService;
124  ReceiveCallback m_receiveCallback;
125 };
126 
127 inline bool
129 {
130  return m_isConnected;
131 }
132 
133 inline bool
135 {
136  return m_isReceiving;
137 }
138 
139 inline void
141 {
142  m_receiveCallback(wire);
143 }
144 
145 } // namespace ndn
146 
147 #endif // NDN_TRANSPORT_TRANSPORT_HPP
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:98
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:128
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
virtual void send(const Block &wire)=0
send a TLV block through the transport
ReceiveCallback m_receiveCallback
Definition: transport.hpp:124
function< void()> ErrorCallback
Definition: transport.hpp:53
virtual void resume()=0
resume the transport
boost::asio::io_service * m_ioService
Definition: transport.hpp:121
void receive(const Block &wire)
invoke the receive callback
Definition: transport.hpp:140
provides TLV-block delivery service
Definition: transport.hpp:40
virtual void pause()=0
pause the transport
virtual ~Transport()=default
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:52
bool isReceiving() const
Definition: transport.hpp:134
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
asynchronously open the connection
Definition: transport.cpp:44