blob: da06da4c87f9f8fb913c9ea93731b40c82c2e08e [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07004 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyev5964fb72014-02-18 12:42:45 -08007#ifndef NDN_TRANSPORT_TRANSPORT_HPP
8#define NDN_TRANSPORT_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07009
Alexander Afanasyev19508852014-01-29 01:01:51 -080010#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080011#include "../encoding/block.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070012
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070013namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070014
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070015class Transport {
16public:
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080017 struct Error : public std::runtime_error { inline Error(const boost::system::error_code &code, const std::string &msg); };
18
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080019 typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080020 typedef ptr_lib::function<void ()> ErrorCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080021
22 inline
23 Transport();
24
25 inline virtual
26 ~Transport();
27
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070028 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080029 * Connect transport
30 *
31 * @throws If connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080032 */
33 inline virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080034 connect(boost::asio::io_service& io_service,
35 const ReceiveCallback& receiveCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080036
37 /**
38 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070039 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070040 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000041 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080042
Jeff Thompson432c8be2013-08-09 16:16:08 -070043 /**
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080044 * @brief Set data to the host
45 *
Jeff Thompson432c8be2013-08-09 16:16:08 -070046 * @param data A pointer to the buffer of data to send.
47 * @param dataLength The number of bytes in data.
48 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070049 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000050 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070051
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080052 /**
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 Afanasyev52afb3f2014-03-07 09:05:35 +000059 send(const Block& header, const Block& payload) = 0;
60
61 virtual void
62 pause() = 0;
63
64 virtual void
65 resume() = 0;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080066
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080067 inline bool
68 isConnected();
69
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000070 inline bool
71 isExpectingData();
72
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080073protected:
74 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080075 receive(const Block& wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070076
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080077protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080078 boost::asio::io_service* m_ioService;
79 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000080 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080081 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070082};
83
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080084inline
85Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080086 : m_ioService(0)
87 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000088 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080089{
90}
91
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080092inline Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080093 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
94{
95}
96
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080097inline
98Transport::~Transport()
99{
100}
101
102inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800103Transport::connect(boost::asio::io_service& ioService,
104 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800105{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800106 m_ioService = &ioService;
107 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800108}
109
110inline bool
111Transport::isConnected()
112{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800113 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800114}
115
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000116inline bool
117Transport::isExpectingData()
118{
119 return m_isExpectingData;
120}
121
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800122inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800123Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800124{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800125 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800126}
127
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800128} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700129
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800130#endif // NDN_TRANSPORT_TRANSPORT_HPP