blob: 8b166390d6a51917c47b8011bf1902353e654acb [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
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070031 typedef function<void (const Block& wire)> ReceiveCallback;
32 typedef 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 Afanasyev770827c2014-05-13 17:42:55 -070041 * @brief Connect transport
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080042 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070043 * @throws boost::system::system_error 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 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070050 * @brief 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 Afanasyev770827c2014-05-13 17:42:55 -070056 * @brief Send block of data from @param wire through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080057 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070058 * @param wire A block of data to send
Jeff Thompson432c8be2013-08-09 16:16:08 -070059 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000061 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070062
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080063 /**
64 * @brief Alternative version of sending data, applying scatter/gather I/O concept
65 *
66 * Two non-consecutive memory blocks will be send out together, e.g., as part of the
67 * same message in datagram-oriented transports.
68 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000070 send(const Block& header, const Block& payload) = 0;
71
72 virtual void
73 pause() = 0;
74
75 virtual void
76 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077
78 inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080079 isConnected();
80
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000081 inline bool
82 isExpectingData();
83
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080084protected:
85 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080086 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080088protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080089 boost::asio::io_service* m_ioService;
90 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000091 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080092 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070093};
94
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080095inline
96Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080097 : m_ioService(0)
98 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000099 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800100{
101}
102
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600103inline
104Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800105 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
106{
107}
108
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800109inline
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600110Transport::Error::Error(const std::string& msg)
111 : std::runtime_error(msg)
112{
113}
114
115inline
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800116Transport::~Transport()
117{
118}
119
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800121Transport::connect(boost::asio::io_service& ioService,
122 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800123{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800124 m_ioService = &ioService;
125 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800126}
127
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800129Transport::isConnected()
130{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800131 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800132}
133
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000134inline bool
135Transport::isExpectingData()
136{
137 return m_isExpectingData;
138}
139
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800140inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800141Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800142{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800143 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800144}
145
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800146} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700147
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800148#endif // NDN_TRANSPORT_TRANSPORT_HPP