38 : m_localEndpoint(localEndpoint)
51 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Already listening");
55 m_acceptor.open(m_localEndpoint.protocol());
56 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
57 if (m_localEndpoint.address().is_v6())
58 m_acceptor.set_option(ip::v6_only(
true));
60 m_acceptor.bind(m_localEndpoint);
61 m_acceptor.listen(backlog);
64 accept(onFaceCreated, onAcceptFailed);
71 const time::seconds& timeout)
73 auto it = m_channelFaces.find(remoteEndpoint);
74 if (it != m_channelFaces.end()) {
75 onFaceCreated(it->second);
82 bind(&TcpChannel::handleConnectTimeout,
this, clientSocket, onConnectFailed));
84 clientSocket->async_connect(remoteEndpoint,
85 bind(&TcpChannel::handleConnect,
this,
86 boost::asio::placeholders::error,
87 clientSocket, connectTimeoutEvent,
88 onFaceCreated, onConnectFailed));
94 return m_channelFaces.size();
98 TcpChannel::createFace(ip::tcp::socket&& socket,
102 shared_ptr<Face>
face;
105 auto it = m_channelFaces.find(remoteEndpoint);
106 if (it == m_channelFaces.end()) {
109 auto linkService = make_unique<face::GenericLinkService>();
110 auto transport = make_unique<face::TcpTransport>(std::move(socket), persistency);
111 face = make_shared<Face>(std::move(linkService), std::move(transport));
113 m_channelFaces[remoteEndpoint] = face;
115 [
this, remoteEndpoint] {
116 NFD_LOG_TRACE(
"Erasing " << remoteEndpoint <<
" from channel face map");
117 m_channelFaces.erase(remoteEndpoint);
124 boost::system::error_code error;
125 socket.shutdown(ip::tcp::socket::shutdown_both, error);
138 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept,
this,
139 boost::asio::placeholders::error,
140 onFaceCreated, onAcceptFailed));
144 TcpChannel::handleAccept(
const boost::system::error_code& error,
149 if (error == boost::asio::error::operation_aborted)
152 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Accept failed: " << error.message());
154 onAcceptFailed(error.message());
158 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Connection from " << m_acceptSocket.remote_endpoint());
160 createFace(std::move(m_acceptSocket), onFaceCreated,
true);
163 accept(onFaceCreated, onAcceptFailed);
167 TcpChannel::handleConnect(
const boost::system::error_code& error,
168 const shared_ptr<ip::tcp::socket>& socket,
175 #if (BOOST_VERSION == 105400) 178 boost::system::error_code anotherErrorCode;
179 socket->remote_endpoint(anotherErrorCode);
180 if (error || anotherErrorCode) {
184 if (error == boost::asio::error::operation_aborted)
187 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Connect failed: " << error.message());
192 onConnectFailed(error.message());
196 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Connected to " << socket->remote_endpoint());
198 createFace(std::move(*socket), onFaceCreated,
false);
202 TcpChannel::handleConnectTimeout(
const shared_ptr<ip::tcp::socket>& socket,
208 boost::system::error_code error;
209 socket->close(error);
212 onConnectFailed(
"Connect to remote endpoint timed out");
#define NFD_LOG_DEBUG(expression)
function< void(const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
void cancel(const EventId &eventId)
cancel a scheduled event
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...
represents the underlying protocol and address used by a Face
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
detail::SimulatorIo & getGlobalIoService()
void connect(const tcp::Endpoint &remoteEndpoint, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, const time::seconds &timeout=time::seconds(4))
Create a face by establishing connection to remote endpoint.
TcpChannel(const tcp::Endpoint &localEndpoint)
Create TCP channel for the local endpoint.
std::shared_ptr< ns3::EventId > EventId
#define NFD_LOG_WARN(expression)
Copyright (c) 2011-2015 Regents of the University of California.
boost::asio::ip::tcp::endpoint Endpoint
void setUri(const FaceUri &uri)
#define NFD_LOG_INIT(name)
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
#define NFD_LOG_TRACE(expression)
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...
size_t size() const
Get number of faces in the channel.