36 namespace ip = boost::asio::ip;
39 : m_localEndpoint(localEndpoint)
43 setUri(FaceUri(m_localEndpoint));
57 m_acceptor.open(m_localEndpoint.protocol());
58 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
59 if (m_localEndpoint.address().is_v6()) {
60 m_acceptor.set_option(ip::v6_only(
true));
62 m_acceptor.bind(m_localEndpoint);
63 m_acceptor.listen(backlog);
65 accept(onFaceCreated, onAcceptFailed);
71 ndn::nfd::FacePersistency persistency,
73 bool wantLpReliability,
76 time::nanoseconds timeout)
78 auto it = m_channelFaces.find(remoteEndpoint);
79 if (it != m_channelFaces.end()) {
81 onFaceCreated(it->second);
86 auto timeoutEvent =
scheduler::schedule(timeout, bind(&TcpChannel::handleConnectTimeout,
this,
87 remoteEndpoint, clientSocket, onConnectFailed));
90 clientSocket->async_connect(remoteEndpoint,
91 bind(&TcpChannel::handleConnect,
this,
92 boost::asio::placeholders::error, remoteEndpoint, clientSocket,
95 ConnectParams{persistency, wantLocalFields, wantLpReliability},
96 timeoutEvent, onFaceCreated, onConnectFailed));
100 TcpChannel::createFace(ip::tcp::socket&& socket,
101 ndn::nfd::FacePersistency persistency,
102 bool wantLocalFields,
103 bool wantLpReliability,
106 shared_ptr<Face> face;
109 auto it = m_channelFaces.find(remoteEndpoint);
110 if (it == m_channelFaces.end()) {
114 auto linkService = make_unique<GenericLinkService>(options);
116 auto transport = make_unique<TcpTransport>(std::move(socket), persistency);
117 face = make_shared<Face>(std::move(linkService), std::move(transport));
119 m_channelFaces[remoteEndpoint] = face;
127 boost::system::error_code error;
128 socket.shutdown(ip::tcp::socket::shutdown_both, error);
141 m_acceptor.async_accept(m_socket, bind(&TcpChannel::handleAccept,
this,
142 boost::asio::placeholders::error,
143 onFaceCreated, onAcceptFailed));
147 TcpChannel::handleAccept(
const boost::system::error_code& error,
152 if (error != boost::asio::error::operation_aborted) {
155 onAcceptFailed(500,
"Accept failed: " + error.message());
161 createFace(std::move(m_socket), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
false,
false, onFaceCreated);
164 accept(onFaceCreated, onAcceptFailed);
168 TcpChannel::handleConnect(
const boost::system::error_code& error,
170 const shared_ptr<ip::tcp::socket>& socket,
171 TcpChannel::ConnectParams params,
172 const scheduler::EventId& connectTimeoutEvent,
178 #if (BOOST_VERSION == 105400) 181 boost::system::error_code anotherErrorCode;
182 socket->remote_endpoint(anotherErrorCode);
183 if (error || anotherErrorCode) {
187 if (error != boost::asio::error::operation_aborted) {
188 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
190 onConnectFailed(504,
"Connection failed: " + error.message());
196 createFace(std::move(*socket), params.persistency, params.wantLocalFields,
197 params.wantLpReliability, onFaceCreated);
201 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
202 const shared_ptr<ip::tcp::socket>& socket,
208 boost::system::error_code error;
209 socket->close(error);
212 onConnectFailed(504,
"Connection timed out");
TcpChannel(const tcp::Endpoint &localEndpoint)
Create TCP channel for the local endpoint.
void cancel(const EventId &eventId)
cancel a scheduled event
bool isEnabled
enables link-layer reliability
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
bool isListening() const override
Returns whether the channel is listening.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
boost::asio::ip::tcp::endpoint Endpoint
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::ip::tcp::acceptor::max_connections)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
LpReliability::Options reliabilityOptions
options for reliability
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
void setUri(const FaceUri &uri)
Options that control the behavior of GenericLinkService.
#define NFD_LOG_INIT(name)
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy ...
void connect(const tcp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, bool wantLocalFields, bool wantLpReliability, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, time::nanoseconds timeout=time::seconds(4))
Create a face by establishing a TCP connection to remoteEndpoint.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
boost::asio::io_service & getGlobalIoService()