blob: 3ed386c06f860cff1b5d79293658bbe2599767ea [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070011 */
12
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080013#ifndef NDN_TRANSPORT_TRANSPORT_HPP
14#define NDN_TRANSPORT_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070015
Alexander Afanasyev19508852014-01-29 01:01:51 -080016#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080017#include "../encoding/block.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070018
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070019namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070020
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070021class Transport
22{
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070023public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060024 class Error : public std::runtime_error
25 {
26 public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027 inline Error(const boost::system::error_code& code, const std::string& msg);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060028 inline Error(const std::string& msg);
29 };
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030
31 typedef ptr_lib::function<void (const Block& wire)> ReceiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080032 typedef ptr_lib::function<void ()> ErrorCallback;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080034 inline
35 Transport();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080037 inline virtual
38 ~Transport();
39
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070040 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080041 * Connect transport
42 *
43 * @throws If connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080044 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 inline virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080046 connect(boost::asio::io_service& io_service,
47 const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080049 /**
50 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070051 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000053 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080054
Jeff Thompson432c8be2013-08-09 16:16:08 -070055 /**
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080056 * @brief Set data to the host
57 *
Jeff Thompson432c8be2013-08-09 16:16:08 -070058 * @param data A pointer to the buffer of data to send.
59 * @param dataLength The number of bytes in data.
60 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000062 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070063
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080064 /**
65 * @brief Alternative version of sending data, applying scatter/gather I/O concept
66 *
67 * Two non-consecutive memory blocks will be send out together, e.g., as part of the
68 * same message in datagram-oriented transports.
69 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000071 send(const Block& header, const Block& payload) = 0;
72
73 virtual void
74 pause() = 0;
75
76 virtual void
77 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078
79 inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080080 isConnected();
81
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000082 inline bool
83 isExpectingData();
84
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080085protected:
86 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080087 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080089protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080090 boost::asio::io_service* m_ioService;
91 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000092 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080093 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070094};
95
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080096inline
97Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080098 : m_ioService(0)
99 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000100 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800101{
102}
103
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600104inline
105Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800106 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
107{
108}
109
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800110inline
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600111Transport::Error::Error(const std::string& msg)
112 : std::runtime_error(msg)
113{
114}
115
116inline
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800117Transport::~Transport()
118{
119}
120
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800122Transport::connect(boost::asio::io_service& ioService,
123 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800124{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800125 m_ioService = &ioService;
126 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800127}
128
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800130Transport::isConnected()
131{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800132 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800133}
134
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000135inline bool
136Transport::isExpectingData()
137{
138 return m_isExpectingData;
139}
140
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800141inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800143{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800144 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800145}
146
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800147} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700148
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800149#endif // NDN_TRANSPORT_TRANSPORT_HPP