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 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 11 | #include "face.hpp" |
| 12 | #include "transport/unix-transport.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 13 | |
| 14 | #include <boost/asio.hpp> |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 15 | #if NDN_CPP_HAVE_CXX11 |
| 16 | // In the std library, the placeholders are in a different namespace than boost. |
| 17 | using namespace ndn::func_lib::placeholders; |
| 18 | #endif |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace std; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 21 | typedef boost::asio::local::stream_protocol protocol; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 22 | |
| 23 | namespace ndn { |
| 24 | |
| 25 | const size_t MAX_LENGTH = 9000; |
| 26 | |
| 27 | class UnixTransport::Impl |
| 28 | { |
| 29 | public: |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 30 | Impl(UnixTransport &transport) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 31 | : transport_(transport) |
| 32 | , socket_(*transport_.ioService_) |
| 33 | , partialDataSize_(0) |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 34 | , connectionInProgress_(false) |
Alexander Afanasyev | 0102be9 | 2014-01-09 14:31:09 -0800 | [diff] [blame] | 35 | , connectTimer_(*transport_.ioService_) |
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; |
Alexander Afanasyev | 0102be9 | 2014-01-09 14:31:09 -0800 | [diff] [blame] | 43 | connectTimer_.cancel(); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 44 | |
| 45 | if (!error) |
| 46 | { |
| 47 | partialDataSize_ = 0; |
| 48 | socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0, |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 49 | func_lib::bind(&Impl::handle_async_receive, this, _1, _2)); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 50 | |
| 51 | transport_.isConnected_ = true; |
| 52 | |
| 53 | for (std::list<Block>::iterator i = sendQueue_.begin(); i != sendQueue_.end(); ++i) |
| 54 | socket_.async_send(boost::asio::buffer(i->wire(), i->size()), |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 55 | func_lib::bind(&Impl::handle_async_send, this, _1, *i)); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 56 | |
| 57 | sendQueue_.clear(); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | // may need to throw exception |
| 62 | transport_.isConnected_ = false; |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 63 | transport_.close(); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 64 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 65 | } |
| 66 | } |
Alexander Afanasyev | 0102be9 | 2014-01-09 14:31:09 -0800 | [diff] [blame] | 67 | |
| 68 | void |
| 69 | connectTimeoutHandler(const boost::system::error_code& error) |
| 70 | { |
| 71 | if (error) // e.g., cancelled timer |
| 72 | return; |
| 73 | |
| 74 | connectionInProgress_ = false; |
| 75 | transport_.isConnected_ = false; |
| 76 | socket_.close(); |
| 77 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 78 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 79 | |
| 80 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 81 | connect() |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 82 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 83 | if (!connectionInProgress_) { |
| 84 | connectionInProgress_ = true; |
Alexander Afanasyev | 0102be9 | 2014-01-09 14:31:09 -0800 | [diff] [blame] | 85 | |
| 86 | // Wait at most 4 seconds to connect |
| 87 | /// @todo Decide whether this number should be configurable |
| 88 | connectTimer_.expires_from_now(boost::posix_time::seconds(4)); |
| 89 | connectTimer_.async_wait(func_lib::bind(&Impl::connectTimeoutHandler, this, _1)); |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 90 | |
| 91 | socket_.open(); |
| 92 | socket_.async_connect(protocol::endpoint(transport_.unixSocket_), |
| 93 | func_lib::bind(&Impl::connectHandler, this, _1)); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 94 | } |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 98 | close() |
| 99 | { |
Alexander Afanasyev | 0102be9 | 2014-01-09 14:31:09 -0800 | [diff] [blame] | 100 | connectTimer_.cancel(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 101 | socket_.close(); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 102 | transport_.isConnected_ = false; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 104 | |
| 105 | void |
| 106 | send(const Block &wire) |
| 107 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 108 | if (!transport_.isConnected_) |
| 109 | sendQueue_.push_back(wire); |
| 110 | else |
| 111 | socket_.async_send(boost::asio::buffer(wire.wire(), wire.size()), |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 112 | func_lib::bind(&Impl::handle_async_send, this, _1, wire)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 113 | } |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 114 | |
| 115 | inline void |
| 116 | processAll(uint8_t *buffer, size_t &offset, size_t availableSize) |
| 117 | { |
| 118 | while(offset < availableSize) |
| 119 | { |
| 120 | Block element(buffer + offset, availableSize - offset); |
| 121 | transport_.receive(element); |
| 122 | |
| 123 | offset += element.size(); |
| 124 | } |
| 125 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 126 | |
| 127 | void |
| 128 | handle_async_receive(const boost::system::error_code& error, std::size_t bytes_recvd) |
| 129 | { |
| 130 | /// @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] | 131 | |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 132 | if (error) |
| 133 | { |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 134 | if (error == boost::system::errc::operation_canceled) { |
| 135 | // async receive has been explicitly cancelled (e.g., socket close) |
| 136 | return; |
| 137 | } |
| 138 | |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 139 | socket_.close(); // closing at this point may not be that necessary |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 140 | transport_.isConnected_ = true; |
| 141 | throw Transport::Error(error, "error while receiving data from socket"); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 144 | if (!error && bytes_recvd > 0) |
| 145 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 146 | // inputBuffer_ has bytes_recvd received bytes of data |
| 147 | if (partialDataSize_ > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 148 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 149 | size_t newDataSize = std::min(bytes_recvd, MAX_LENGTH-partialDataSize_); |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 150 | std::copy(inputBuffer_, inputBuffer_ + newDataSize, partialData_ + partialDataSize_); |
| 151 | |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 152 | partialDataSize_ += newDataSize; |
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 | size_t offset = 0; |
| 155 | try |
| 156 | { |
| 157 | processAll(partialData_, offset, partialDataSize_); |
| 158 | |
| 159 | // no exceptions => processed the whole thing |
| 160 | if (bytes_recvd - newDataSize > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 161 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 162 | // there is a little bit more data available |
| 163 | |
| 164 | offset = 0; |
| 165 | partialDataSize_ = bytes_recvd - newDataSize; |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 166 | std::copy(inputBuffer_ + newDataSize, inputBuffer_ + newDataSize + partialDataSize_, partialData_); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 168 | processAll(partialData_, offset, partialDataSize_); |
| 169 | |
| 170 | // no exceptions => processed the whole thing |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 171 | partialDataSize_ = 0; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 172 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 173 | else |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 174 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 175 | // done processing |
| 176 | partialDataSize_ = 0; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 179 | catch(Tlv::Error &) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 180 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 181 | if (offset > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 182 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 183 | partialDataSize_ -= offset; |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 184 | std::copy(partialData_ + offset, partialData_ + offset + partialDataSize_, partialData_); |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 185 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 186 | else if (offset == 0 && partialDataSize_ == MAX_LENGTH) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 187 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 188 | // very bad... should close connection |
| 189 | socket_.close(); |
| 190 | transport_.isConnected_ = true; |
| 191 | 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] | 192 | } |
| 193 | } |
| 194 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 195 | else |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 196 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 197 | size_t offset = 0; |
| 198 | try |
| 199 | { |
| 200 | processAll(inputBuffer_, offset, bytes_recvd); |
| 201 | } |
| 202 | catch(Tlv::Error &error) |
| 203 | { |
| 204 | if (offset > 0) |
| 205 | { |
| 206 | partialDataSize_ = bytes_recvd - offset; |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 207 | std::copy(inputBuffer_ + offset, inputBuffer_ + offset + partialDataSize_, partialData_); |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0, |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 214 | func_lib::bind(&Impl::handle_async_receive, this, _1, _2)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
| 218 | handle_async_send(const boost::system::error_code& error, const Block &wire) |
| 219 | { |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 220 | // pass (needed to keep data block alive during the send) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 221 | } |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 222 | |
| 223 | private: |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 224 | UnixTransport &transport_; |
| 225 | |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 226 | protocol::socket socket_; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 227 | uint8_t inputBuffer_[MAX_LENGTH]; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 228 | |
| 229 | uint8_t partialData_[MAX_LENGTH]; |
| 230 | size_t partialDataSize_; |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 231 | |
| 232 | std::list< Block > sendQueue_; |
| 233 | bool connectionInProgress_; |
Alexander Afanasyev | 0102be9 | 2014-01-09 14:31:09 -0800 | [diff] [blame] | 234 | |
| 235 | boost::asio::deadline_timer connectTimer_; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | UnixTransport::UnixTransport(const std::string &unixSocket/* = "/tmp/.ndnd.sock"*/) |
| 239 | : unixSocket_(unixSocket) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 240 | { |
| 241 | } |
| 242 | |
| 243 | UnixTransport::~UnixTransport() |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 248 | UnixTransport::connect(boost::asio::io_service &ioService, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 249 | const ReceiveCallback &receiveCallback) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 250 | { |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 251 | if (!static_cast<bool>(impl_)) { |
| 252 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 253 | |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 254 | impl_ = ptr_lib::make_shared<UnixTransport::Impl> (ptr_lib::ref(*this)); |
| 255 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 256 | impl_->connect(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 260 | UnixTransport::send(const Block &wire) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 261 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 262 | impl_->send(wire); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void |
| 266 | UnixTransport::close() |
| 267 | { |
| 268 | impl_->close(); |
| 269 | } |
| 270 | |
| 271 | } |