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