blob: c309288611e4a48dca06c6d96294a21b3ef43a64 [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 Afanasyev5964fb72014-02-18 12:42:45 -080041 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 Afanasyev5964fb72014-02-18 12:42:45 -080050 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
59 send(const Block& header, const Block& payload) =0;
60
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080061 inline bool
62 isConnected();
63
64protected:
65 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080066 receive(const Block& wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070067
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080068protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080069 boost::asio::io_service* m_ioService;
70 bool m_isConnected;
71 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070072};
73
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080074inline
75Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080076 : m_ioService(0)
77 , m_isConnected(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080078{
79}
80
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080081inline Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080082 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
83{
84}
85
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080086inline
87Transport::~Transport()
88{
89}
90
91inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080092Transport::connect(boost::asio::io_service& ioService,
93 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080094{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080095 m_ioService = &ioService;
96 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080097}
98
99inline bool
100Transport::isConnected()
101{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800102 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800103}
104
105inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800106Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800107{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800108 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800109}
110
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800111} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700112
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800113#endif // NDN_TRANSPORT_TRANSPORT_HPP