blob: 0463916a6ab9dc7cb95be292333ac7d4bcec60e4 [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 {
113 socket_.close(); // closing at this point may not be that necessary
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800114 transport_.isConnected_ = true;
115 throw Transport::Error(error, "error while receiving data from socket");
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800116 }
117
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800118 if (!error && bytes_recvd > 0)
119 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800120 // inputBuffer_ has bytes_recvd received bytes of data
121 if (partialDataSize_ > 0)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800122 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800123 size_t newDataSize = std::min(bytes_recvd, MAX_LENGTH-partialDataSize_);
124 ndn_memcpy(partialData_ + partialDataSize_, inputBuffer_, newDataSize);
125 partialDataSize_ += newDataSize;
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800126
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800127 size_t offset = 0;
128 try
129 {
130 processAll(partialData_, offset, partialDataSize_);
131
132 // no exceptions => processed the whole thing
133 if (bytes_recvd - newDataSize > 0)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800134 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800135 // there is a little bit more data available
136
137 offset = 0;
138 partialDataSize_ = bytes_recvd - newDataSize;
139 ndn_memcpy(partialData_, inputBuffer_ + newDataSize, partialDataSize_);
140
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800141 processAll(partialData_, offset, partialDataSize_);
142
143 // no exceptions => processed the whole thing
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800144 partialDataSize_ = 0;
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800145 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800146 else
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800147 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800148 // done processing
149 partialDataSize_ = 0;
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800150 }
151 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800152 catch(Tlv::Error &)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800153 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800154 if (offset > 0)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800155 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800156 partialDataSize_ -= offset;
157 ndn_memcpy(partialData_, partialData_ + offset, partialDataSize_);
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800158 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800159 else if (offset == 0 && partialDataSize_ == MAX_LENGTH)
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800160 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800161 // very bad... should close connection
162 socket_.close();
163 transport_.isConnected_ = true;
164 throw Transport::Error(boost::system::error_code(), "input buffer full, but a valid TLV cannot be decoded");
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800165 }
166 }
167 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800168 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800169 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800170 size_t offset = 0;
171 try
172 {
173 processAll(inputBuffer_, offset, bytes_recvd);
174 }
175 catch(Tlv::Error &error)
176 {
177 if (offset > 0)
178 {
179 partialDataSize_ = bytes_recvd - offset;
180 ndn_memcpy(partialData_, inputBuffer_ + offset, partialDataSize_);
181 }
182 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800183 }
184 }
185
186 socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0,
Alexander Afanasyev3c034202014-01-06 00:06:48 -0800187 func_lib::bind(&Impl::handle_async_receive, this, _1, _2));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800188 }
189
190 void
191 handle_async_send(const boost::system::error_code& error, const Block &wire)
192 {
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800193 // pass (needed to keep data block alive during the send)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800194 }
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800195
196private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800197 UnixTransport &transport_;
198
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800199 protocol::socket socket_;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800200 uint8_t inputBuffer_[MAX_LENGTH];
Alexander Afanasyev328e23d2013-12-28 20:47:38 -0800201
202 uint8_t partialData_[MAX_LENGTH];
203 size_t partialDataSize_;
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800204
205 std::list< Block > sendQueue_;
206 bool connectionInProgress_;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800207};
208
209UnixTransport::UnixTransport(const std::string &unixSocket/* = "/tmp/.ndnd.sock"*/)
210 : unixSocket_(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800211{
212}
213
214UnixTransport::~UnixTransport()
215{
216}
217
218void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800219UnixTransport::connect(boost::asio::io_service &ioService,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800220 const ReceiveCallback &receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800221{
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800222 Transport::connect(ioService, receiveCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800223
224 impl_ = std::auto_ptr<UnixTransport::Impl> (new UnixTransport::Impl(*this));
225 impl_->connect();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800226}
227
228void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800229UnixTransport::send(const Block &wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800230{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800231 impl_->send(wire);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800232}
233
234void
235UnixTransport::close()
236{
237 impl_->close();
238}
239
240}