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 | { |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 113 | if (error == boost::system::errc::operation_canceled) { |
| 114 | // async receive has been explicitly cancelled (e.g., socket close) |
| 115 | return; |
| 116 | } |
| 117 | |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 118 | socket_.close(); // closing at this point may not be that necessary |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 119 | transport_.isConnected_ = true; |
| 120 | throw Transport::Error(error, "error while receiving data from socket"); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 123 | if (!error && bytes_recvd > 0) |
| 124 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 125 | // inputBuffer_ has bytes_recvd received bytes of data |
| 126 | if (partialDataSize_ > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 127 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 128 | size_t newDataSize = std::min(bytes_recvd, MAX_LENGTH-partialDataSize_); |
| 129 | ndn_memcpy(partialData_ + partialDataSize_, inputBuffer_, newDataSize); |
| 130 | partialDataSize_ += newDataSize; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 132 | size_t offset = 0; |
| 133 | try |
| 134 | { |
| 135 | processAll(partialData_, offset, partialDataSize_); |
| 136 | |
| 137 | // no exceptions => processed the whole thing |
| 138 | if (bytes_recvd - newDataSize > 0) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 139 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 140 | // there is a little bit more data available |
| 141 | |
| 142 | offset = 0; |
| 143 | partialDataSize_ = bytes_recvd - newDataSize; |
| 144 | ndn_memcpy(partialData_, inputBuffer_ + newDataSize, partialDataSize_); |
| 145 | |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 146 | processAll(partialData_, offset, partialDataSize_); |
| 147 | |
| 148 | // no exceptions => processed the whole thing |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 149 | partialDataSize_ = 0; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 150 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 151 | else |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 152 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 153 | // done processing |
| 154 | partialDataSize_ = 0; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 157 | catch(Tlv::Error &) |
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 | if (offset > 0) |
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 | partialDataSize_ -= offset; |
| 162 | ndn_memcpy(partialData_, partialData_ + offset, partialDataSize_); |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 163 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 164 | else if (offset == 0 && partialDataSize_ == MAX_LENGTH) |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 165 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 166 | // very bad... should close connection |
| 167 | socket_.close(); |
| 168 | transport_.isConnected_ = true; |
| 169 | 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] | 170 | } |
| 171 | } |
| 172 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 173 | else |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 174 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 175 | size_t offset = 0; |
| 176 | try |
| 177 | { |
| 178 | processAll(inputBuffer_, offset, bytes_recvd); |
| 179 | } |
| 180 | catch(Tlv::Error &error) |
| 181 | { |
| 182 | if (offset > 0) |
| 183 | { |
| 184 | partialDataSize_ = bytes_recvd - offset; |
| 185 | ndn_memcpy(partialData_, inputBuffer_ + offset, partialDataSize_); |
| 186 | } |
| 187 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0, |
Alexander Afanasyev | 3c03420 | 2014-01-06 00:06:48 -0800 | [diff] [blame] | 192 | func_lib::bind(&Impl::handle_async_receive, this, _1, _2)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
| 196 | handle_async_send(const boost::system::error_code& error, const Block &wire) |
| 197 | { |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 198 | // pass (needed to keep data block alive during the send) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 199 | } |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 200 | |
| 201 | private: |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 202 | UnixTransport &transport_; |
| 203 | |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 204 | protocol::socket socket_; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 205 | uint8_t inputBuffer_[MAX_LENGTH]; |
Alexander Afanasyev | 328e23d | 2013-12-28 20:47:38 -0800 | [diff] [blame] | 206 | |
| 207 | uint8_t partialData_[MAX_LENGTH]; |
| 208 | size_t partialDataSize_; |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 209 | |
| 210 | std::list< Block > sendQueue_; |
| 211 | bool connectionInProgress_; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | UnixTransport::UnixTransport(const std::string &unixSocket/* = "/tmp/.ndnd.sock"*/) |
| 215 | : unixSocket_(unixSocket) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 216 | { |
| 217 | } |
| 218 | |
| 219 | UnixTransport::~UnixTransport() |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 224 | UnixTransport::connect(boost::asio::io_service &ioService, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 225 | const ReceiveCallback &receiveCallback) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 226 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 227 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 228 | |
| 229 | impl_ = std::auto_ptr<UnixTransport::Impl> (new UnixTransport::Impl(*this)); |
| 230 | impl_->connect(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 234 | UnixTransport::send(const Block &wire) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 235 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 236 | impl_->send(wire); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void |
| 240 | UnixTransport::close() |
| 241 | { |
| 242 | impl_->close(); |
| 243 | } |
| 244 | |
| 245 | } |