Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #include <stdexcept> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include <ndn-cpp/face.hpp> |
| 12 | #include <ndn-cpp/transport/unix-transport.hpp> |
Alexander Afanasyev | 54467af | 2014-01-06 15:45:32 -0800 | [diff] [blame^] | 13 | #include "../c/util/ndn_memory.h" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 14 | |
| 15 | #include <boost/asio.hpp> |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 16 | #if NDN_CPP_HAVE_CXX11 |
| 17 | // In the std library, the placeholders are in a different namespace than boost. |
| 18 | using namespace ndn::func_lib::placeholders; |
| 19 | #endif |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 20 | |
| 21 | using namespace std; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 22 | typedef boost::asio::local::stream_protocol protocol; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | const size_t MAX_LENGTH = 9000; |
| 27 | |
| 28 | class UnixTransport::Impl |
| 29 | { |
| 30 | public: |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 31 | Impl(UnixTransport &transport) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 32 | : transport_(transport) |
| 33 | , socket_(*transport_.ioService_) |
| 34 | , partialDataSize_(0) |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 35 | , connectionInProgress_(false) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 39 | void |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 40 | connectHandler(const boost::system::error_code& error) |
| 41 | { |
| 42 | connectionInProgress_ = false; |
| 43 | |
| 44 | if (!error) |
| 45 | { |
| 46 | partialDataSize_ = 0; |
| 47 | socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0, |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 48 | func_lib::bind(&Impl::handle_async_receive, this, _1, _2)); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 49 | |
| 50 | transport_.isConnected_ = true; |
| 51 | |
| 52 | for (std::list<Block>::iterator i = sendQueue_.begin(); i != sendQueue_.end(); ++i) |
| 53 | socket_.async_send(boost::asio::buffer(i->wire(), i->size()), |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 54 | func_lib::bind(&Impl::handle_async_send, this, _1, *i)); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 55 | |
| 56 | sendQueue_.clear(); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | // may need to throw exception |
| 61 | transport_.isConnected_ = false; |
| 62 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 67 | connect() |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 68 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 69 | if (!connectionInProgress_) { |
| 70 | connectionInProgress_ = true; |
| 71 | socket_.open(); |
| 72 | socket_.async_connect(protocol::endpoint(transport_.unixSocket_), |
| 73 | func_lib::bind(&Impl::connectHandler, this, _1)); |
| 74 | } |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 78 | close() |
| 79 | { |
| 80 | socket_.close(); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 81 | transport_.isConnected_ = false; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 82 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 83 | |
| 84 | void |
| 85 | send(const Block &wire) |
| 86 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 87 | if (!transport_.isConnected_) |
| 88 | sendQueue_.push_back(wire); |
| 89 | else |
| 90 | socket_.async_send(boost::asio::buffer(wire.wire(), wire.size()), |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 91 | func_lib::bind(&Impl::handle_async_send, this, _1, wire)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 92 | } |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 93 | |
| 94 | inline void |
| 95 | processAll(uint8_t *buffer, size_t &offset, size_t availableSize) |
| 96 | { |
| 97 | while(offset < availableSize) |
| 98 | { |
| 99 | Block element(buffer + offset, availableSize - offset); |
| 100 | transport_.receive(element); |
| 101 | |
| 102 | offset += element.size(); |
| 103 | } |
| 104 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 105 | |
| 106 | void |
| 107 | handle_async_receive(const boost::system::error_code& error, std::size_t bytes_recvd) |
| 108 | { |
| 109 | /// @todo The socket is not datagram, so need to have internal buffer to handle partial data reception |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 111 | if (error) |
| 112 | { |
| 113 | socket_.close(); // closing at this point may not be that necessary |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 114 | transport_.isConnected_ = true; |
| 115 | throw Transport::Error(error, "error while receiving data from socket"); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 118 | if (!error && bytes_recvd > 0) |
| 119 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 120 | // inputBuffer_ has bytes_recvd received bytes of data |
| 121 | if (partialDataSize_ > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 122 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 123 | size_t newDataSize = std::min(bytes_recvd, MAX_LENGTH-partialDataSize_); |
| 124 | ndn_memcpy(partialData_ + partialDataSize_, inputBuffer_, newDataSize); |
| 125 | partialDataSize_ += newDataSize; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 127 | size_t offset = 0; |
| 128 | try |
| 129 | { |
| 130 | processAll(partialData_, offset, partialDataSize_); |
| 131 | |
| 132 | // no exceptions => processed the whole thing |
| 133 | if (bytes_recvd - newDataSize > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 134 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 135 | // there is a little bit more data available |
| 136 | |
| 137 | offset = 0; |
| 138 | partialDataSize_ = bytes_recvd - newDataSize; |
| 139 | ndn_memcpy(partialData_, inputBuffer_ + newDataSize, partialDataSize_); |
| 140 | |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 141 | processAll(partialData_, offset, partialDataSize_); |
| 142 | |
| 143 | // no exceptions => processed the whole thing |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 144 | partialDataSize_ = 0; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 145 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 146 | else |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 147 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 148 | // done processing |
| 149 | partialDataSize_ = 0; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 152 | catch(Tlv::Error &) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 153 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 154 | if (offset > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 155 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 156 | partialDataSize_ -= offset; |
| 157 | ndn_memcpy(partialData_, partialData_ + offset, partialDataSize_); |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 158 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 159 | else if (offset == 0 && partialDataSize_ == MAX_LENGTH) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 161 | // very bad... should close connection |
| 162 | socket_.close(); |
| 163 | transport_.isConnected_ = true; |
| 164 | throw Transport::Error(boost::system::error_code(), "input buffer full, but a valid TLV cannot be decoded"); |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 168 | else |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 169 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 170 | size_t offset = 0; |
| 171 | try |
| 172 | { |
| 173 | processAll(inputBuffer_, offset, bytes_recvd); |
| 174 | } |
| 175 | catch(Tlv::Error &error) |
| 176 | { |
| 177 | if (offset > 0) |
| 178 | { |
| 179 | partialDataSize_ = bytes_recvd - offset; |
| 180 | ndn_memcpy(partialData_, inputBuffer_ + offset, partialDataSize_); |
| 181 | } |
| 182 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0, |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 187 | func_lib::bind(&Impl::handle_async_receive, this, _1, _2)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | handle_async_send(const boost::system::error_code& error, const Block &wire) |
| 192 | { |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 193 | // pass (needed to keep data block alive during the send) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 194 | } |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 195 | |
| 196 | private: |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 197 | UnixTransport &transport_; |
| 198 | |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 199 | protocol::socket socket_; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 200 | uint8_t inputBuffer_[MAX_LENGTH]; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 201 | |
| 202 | uint8_t partialData_[MAX_LENGTH]; |
| 203 | size_t partialDataSize_; |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 204 | |
| 205 | std::list< Block > sendQueue_; |
| 206 | bool connectionInProgress_; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | UnixTransport::UnixTransport(const std::string &unixSocket/* = "/tmp/.ndnd.sock"*/) |
| 210 | : unixSocket_(unixSocket) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 211 | { |
| 212 | } |
| 213 | |
| 214 | UnixTransport::~UnixTransport() |
| 215 | { |
| 216 | } |
| 217 | |
| 218 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 219 | UnixTransport::connect(boost::asio::io_service &ioService, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 220 | const ReceiveCallback &receiveCallback) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 221 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 222 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 223 | |
| 224 | impl_ = std::auto_ptr<UnixTransport::Impl> (new UnixTransport::Impl(*this)); |
| 225 | impl_->connect(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 229 | UnixTransport::send(const Block &wire) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 230 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 231 | impl_->send(wire); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void |
| 235 | UnixTransport::close() |
| 236 | { |
| 237 | impl_->close(); |
| 238 | } |
| 239 | |
| 240 | } |