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