blob: 2704df1b6beaebadbfc4d186a4799fa9d5f05a7c [file] [log] [blame]
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -08001/* -*- 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 Afanasyev54467af2014-01-06 15:45:32 -080013#include "../c/util/ndn_memory.h"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080014
15#include <boost/asio.hpp>
Alexander Afanasyev3c034202014-01-06 00:06:48 -080016#if NDN_CPP_HAVE_CXX11
17// In the std library, the placeholders are in a different namespace than boost.
18using namespace ndn::func_lib::placeholders;
19#endif
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080020
21using namespace std;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080022typedef boost::asio::local::stream_protocol protocol;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080023
24namespace ndn {
25
26const size_t MAX_LENGTH = 9000;
27
28class UnixTransport::Impl
29{
30public:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080031 Impl(UnixTransport &transport)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -080032 : transport_(transport)
33 , socket_(*transport_.ioService_)
34 , partialDataSize_(0)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080035 , connectionInProgress_(false)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080036 {
37 }
38
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080039 void
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080040 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 Afanasyev3c034202014-01-06 00:06:48 -080048 func_lib::bind(&Impl::handle_async_receive, this, _1, _2));
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080049
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 Afanasyev3c034202014-01-06 00:06:48 -080054 func_lib::bind(&Impl::handle_async_send, this, _1, *i));
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080055
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 Afanasyeve2e0d752014-01-03 13:30:30 -080067 connect()
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080068 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080069 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 Afanasyevfe3b1502013-12-18 16:45:03 -080075 }
76
77 void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080078 close()
79 {
80 socket_.close();
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080081 transport_.isConnected_ = false;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080082 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080083
84 void
85 send(const Block &wire)
86 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080087 if (!transport_.isConnected_)
88 sendQueue_.push_back(wire);
89 else
90 socket_.async_send(boost::asio::buffer(wire.wire(), wire.size()),
Alexander Afanasyev3c034202014-01-06 00:06:48 -080091 func_lib::bind(&Impl::handle_async_send, this, _1, wire));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080092 }
Alexander Afanasyev328e23d2013-12-28 20:47:38 -080093
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 Afanasyeve2e0d752014-01-03 13:30:30 -0800105
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 Afanasyev328e23d2013-12-28 20:47:38 -0800110
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800111 if (error)
112 {
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800113 if (error == boost::system::errc::operation_canceled) {
114 // async receive has been explicitly cancelled (e.g., socket close)
115 return;
116 }
117
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800118 socket_.close(); // closing at this point may not be that necessary
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800119 transport_.isConnected_ = true;
120 throw Transport::Error(error, "error while receiving data from socket");
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800121 }
122
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800123 if (!error && bytes_recvd > 0)
124 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800125 // inputBuffer_ has bytes_recvd received bytes of data
126 if (partialDataSize_ > 0)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800127 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800128 size_t newDataSize = std::min(bytes_recvd, MAX_LENGTH-partialDataSize_);
129 ndn_memcpy(partialData_ + partialDataSize_, inputBuffer_, newDataSize);
130 partialDataSize_ += newDataSize;
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800131
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800132 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 Afanasyev328e23d2013-12-28 20:47:38 -0800139 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800140 // 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 Afanasyev328e23d2013-12-28 20:47:38 -0800146 processAll(partialData_, offset, partialDataSize_);
147
148 // no exceptions => processed the whole thing
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800149 partialDataSize_ = 0;
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800150 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800151 else
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800152 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800153 // done processing
154 partialDataSize_ = 0;
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800155 }
156 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800157 catch(Tlv::Error &)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800158 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800159 if (offset > 0)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800160 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800161 partialDataSize_ -= offset;
162 ndn_memcpy(partialData_, partialData_ + offset, partialDataSize_);
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800163 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800164 else if (offset == 0 && partialDataSize_ == MAX_LENGTH)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800165 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800166 // 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 Afanasyev328e23d2013-12-28 20:47:38 -0800170 }
171 }
172 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800173 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800174 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800175 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 Afanasyeve2e0d752014-01-03 13:30:30 -0800188 }
189 }
190
191 socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0,
Alexander Afanasyev3c034202014-01-06 00:06:48 -0800192 func_lib::bind(&Impl::handle_async_receive, this, _1, _2));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800193 }
194
195 void
196 handle_async_send(const boost::system::error_code& error, const Block &wire)
197 {
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800198 // pass (needed to keep data block alive during the send)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800199 }
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800200
201private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800202 UnixTransport &transport_;
203
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800204 protocol::socket socket_;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800205 uint8_t inputBuffer_[MAX_LENGTH];
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800206
207 uint8_t partialData_[MAX_LENGTH];
208 size_t partialDataSize_;
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800209
210 std::list< Block > sendQueue_;
211 bool connectionInProgress_;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800212};
213
214UnixTransport::UnixTransport(const std::string &unixSocket/* = "/tmp/.ndnd.sock"*/)
215 : unixSocket_(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800216{
217}
218
219UnixTransport::~UnixTransport()
220{
221}
222
223void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800224UnixTransport::connect(boost::asio::io_service &ioService,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800225 const ReceiveCallback &receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800226{
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800227 Transport::connect(ioService, receiveCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800228
229 impl_ = std::auto_ptr<UnixTransport::Impl> (new UnixTransport::Impl(*this));
230 impl_->connect();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800231}
232
233void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800234UnixTransport::send(const Block &wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800235{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800236 impl_->send(wire);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800237}
238
239void
240UnixTransport::close()
241{
242 impl_->close();
243}
244
245}