async-tcp-transport.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_ASYNC_TCP_TRANSPORT_HPP
23 #define NDN_ASYNC_TCP_TRANSPORT_HPP
24 
25 #include <string>
26 #include "../common.hpp"
27 #include "../c/encoding/element-reader-types.h"
28 #include "transport.hpp"
29 
30 namespace boost { namespace asio { class io_service; }}
31 
32 namespace ndn {
33 
40 class AsyncTcpTransport : public Transport {
41 public:
47  public:
53  ConnectionInfo(const char *host, unsigned short port = 6363)
54  : host_(host), port_(port)
55  {
56  }
57 
62  const std::string&
63  getHost() const { return host_; }
64 
69  unsigned short
70  getPort() const { return port_; }
71 
72  virtual
73  ~ConnectionInfo();
74 
75  private:
76  std::string host_;
77  unsigned short port_;
78  };
79 
86  AsyncTcpTransport(boost::asio::io_service& ioService);
87 
99  virtual bool
100  isLocal(const Transport::ConnectionInfo& connectionInfo);
101 
106  virtual bool
107  isAsync();
108 
119  virtual void
120  connect
121  (const Transport::ConnectionInfo& connectionInfo,
122  ElementListener& elementListener, const OnConnected& onConnected);
123 
131  virtual void
132  send(const uint8_t *data, size_t dataLength);
133 
137  virtual void
138  processEvents();
139 
140  virtual bool
141  getIsConnected();
142 
146  virtual void
147  close();
148 
149  virtual ~AsyncTcpTransport();
150 
151 private:
152  boost::asio::io_service& ioService_;
153  // We define SocketTransport in the source file so that we don't have to
154  // include the Boost headers for boost::asio::ip::tcp in this header file.
155  // (We cannot forward declare ip::tcp itself because it is an inner class.)
156  class SocketTransport;
157  ptr_lib::shared_ptr<SocketTransport> socketTransport_;
158  ConnectionInfo connectionInfo_;
159  bool isLocal_;
160 };
161 
162 }
163 
164 #endif
virtual bool isAsync()
Override to return true since connect needs to use the onConnected callback.
virtual void close()
Close the connection to the host.
Definition: transport.hpp:32
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
Copyright (C) 2015-2016 Regents of the University of California.
Definition: threadsafe-face.hpp:27
An ElementListener extends an ndn_ElementListener struct to proved an abstract virtual onReceivedElem...
Definition: element-listener.hpp:33
An AsyncTcpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info...
Definition: async-tcp-transport.hpp:46
virtual void processEvents()
Do nothing since the asio io_service reads the socket.
virtual void send(const uint8_t *data, size_t dataLength)
Send data to the host.
unsigned short getPort() const
Get the port given to the constructor.
Definition: async-tcp-transport.hpp:70
const std::string & getHost() const
Get the host given to the constructor.
Definition: async-tcp-transport.hpp:63
AsyncTcpTransport extends Transport for async communication over TCP using Boost's asio io_service...
Definition: async-tcp-transport.hpp:40
virtual void connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener, const OnConnected &onConnected)
Connect according to the info in connectionInfo, and use elementListener.
AsyncTcpTransport(boost::asio::io_service &ioService)
Create an AsyncTcpTransport in the unconnected state.
A Transport::ConnectionInfo is a base class for connection information used by subclasses of Transpor...
Definition: transport.hpp:38
virtual bool isLocal(const Transport::ConnectionInfo &connectionInfo)
Determine whether this transport connecting according to connectionInfo is to a node on the current m...
ConnectionInfo(const char *host, unsigned short port=6363)
Create a ConnectionInfo with the given host and port.
Definition: async-tcp-transport.hpp:53