37 : m_localEndpoint(localEndpoint)
50 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Already listening");
54 m_acceptor.open(m_localEndpoint.protocol());
55 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
56 if (m_localEndpoint.address().is_v6())
57 m_acceptor.set_option(ip::v6_only(
true));
59 m_acceptor.bind(m_localEndpoint);
60 m_acceptor.listen(backlog);
63 accept(onFaceCreated, onAcceptFailed);
70 const time::seconds& timeout)
72 auto it = m_channelFaces.find(remoteEndpoint);
73 if (it != m_channelFaces.end()) {
74 onFaceCreated(it->second);
81 bind(&TcpChannel::handleConnectTimeout,
this, clientSocket, onConnectFailed));
83 clientSocket->async_connect(remoteEndpoint,
84 bind(&TcpChannel::handleConnect,
this,
85 boost::asio::placeholders::error,
86 clientSocket, connectTimeoutEvent,
87 onFaceCreated, onConnectFailed));
93 return m_channelFaces.size();
97 TcpChannel::createFace(ip::tcp::socket socket,
101 shared_ptr<Face> face;
104 auto it = m_channelFaces.find(remoteEndpoint);
105 if (it == m_channelFaces.end()) {
108 if (localEndpoint.address().is_loopback() &&
109 remoteEndpoint.address().is_loopback())
110 face = make_shared<TcpLocalFace>(
FaceUri(remoteEndpoint),
FaceUri(localEndpoint),
111 std::move(socket), isOnDemand);
113 face = make_shared<TcpFace>(
FaceUri(remoteEndpoint),
FaceUri(localEndpoint),
114 std::move(socket), isOnDemand);
116 face->onFail.connectSingleShot([
this, remoteEndpoint] (
const std::string&) {
117 NFD_LOG_TRACE(
"Erasing " << remoteEndpoint <<
" from channel face map");
118 m_channelFaces.erase(remoteEndpoint);
120 m_channelFaces[remoteEndpoint] = face;
126 boost::system::error_code error;
127 socket.shutdown(ip::tcp::socket::shutdown_both, error);
140 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept,
this,
141 boost::asio::placeholders::error,
142 onFaceCreated, onAcceptFailed));
146 TcpChannel::handleAccept(
const boost::system::error_code& error,
151 if (error == boost::asio::error::operation_aborted)
154 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Accept failed: " << error.message());
156 onAcceptFailed(error.message());
160 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Connection from " << m_acceptSocket.remote_endpoint());
162 createFace(std::move(m_acceptSocket), onFaceCreated,
true);
165 accept(onFaceCreated, onAcceptFailed);
169 TcpChannel::handleConnect(
const boost::system::error_code& error,
170 const shared_ptr<ip::tcp::socket>& socket,
177 #if (BOOST_VERSION == 105400) 180 boost::system::error_code anotherErrorCode;
181 socket->remote_endpoint(anotherErrorCode);
182 if (error || anotherErrorCode) {
186 if (error == boost::asio::error::operation_aborted)
189 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Connect failed: " << error.message());
194 onConnectFailed(error.message());
198 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Connected to " << socket->remote_endpoint());
200 createFace(std::move(*socket), onFaceCreated,
false);
204 TcpChannel::handleConnectTimeout(
const shared_ptr<ip::tcp::socket>& socket,
210 boost::system::error_code error;
211 socket->close(error);
214 onConnectFailed(
"Connect to remote endpoint timed out");
#define NFD_LOG_DEBUG(expression)
void cancel(const EventId &eventId)
cancel a scheduled event
represents the underlying protocol and address used by a Face
std::shared_ptr< ns3::EventId > EventId
TcpChannel(const tcp::Endpoint &localEndpoint)
Create TCP channel for the local endpoint.
#define NFD_LOG_WARN(expression)
Copyright (c) 2011-2015 Regents of the University of California.
boost::asio::ip::tcp::endpoint Endpoint
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
void setUri(const FaceUri &uri)
function< void(const std::string &reason)> ConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
void listen(const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &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...
void connect(const tcp::Endpoint &remoteEndpoint, const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onConnectFailed, const time::seconds &timeout=time::seconds(4))
Create a face by establishing connection to remote endpoint.
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
boost::asio::io_service & getGlobalIoService()
size_t size() const
Get number of faces in the channel.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...