blob: 3b62a218ed3b174c7366820e3d7697a3d61f2322 [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.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_TRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070010
Alexander Afanasyev19508852014-01-29 01:01:51 -080011#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../encoding/block.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070013
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070014namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070015
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070016class Transport {
17public:
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080018 struct Error : public std::runtime_error { inline Error(const boost::system::error_code &code, const std::string &msg); };
19
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080020 typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080021 typedef ptr_lib::function<void ()> ErrorCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080022
23 inline
24 Transport();
25
26 inline virtual
27 ~Transport();
28
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070029 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080030 * Connect transport
31 *
32 * @throws If connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080033 */
34 inline virtual void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080035 connect(boost::asio::io_service &io_service,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080036 const ReceiveCallback &receiveCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080037
38 /**
39 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070040 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070041 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080042 close() = 0;
43
Jeff Thompson432c8be2013-08-09 16:16:08 -070044 /**
45 * Set data to the host
46 * @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 Afanasyeve2e0d752014-01-03 13:30:30 -080050 send(const Block &wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070051
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080052 inline bool
53 isConnected();
54
55protected:
56 inline void
57 receive(const Block &wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070058
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080059protected:
60 boost::asio::io_service *ioService_;
61 bool isConnected_;
62 ReceiveCallback receiveCallback_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070063};
64
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080065inline
66Transport::Transport()
67 : ioService_(0)
68 , isConnected_(false)
69{
70}
71
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080072inline Transport::Error::Error(const boost::system::error_code &code, const std::string &msg)
73 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
74{
75}
76
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080077inline
78Transport::~Transport()
79{
80}
81
82inline void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080083Transport::connect(boost::asio::io_service &ioService,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080084 const ReceiveCallback &receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080085{
86 ioService_ = &ioService;
87 receiveCallback_ = receiveCallback;
88}
89
90inline bool
91Transport::isConnected()
92{
93 return isConnected_;
94}
95
96inline void
97Transport::receive(const Block &wire)
98{
99 receiveCallback_(wire);
100}
101
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700102}
103
104#endif