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);
69 bool wantLocalFieldsEnabled,
72 const time::seconds& timeout)
74 auto it = m_channelFaces.find(remoteEndpoint);
75 if (it != m_channelFaces.end()) {
76 onFaceCreated(it->second);
83 bind(&TcpChannel::handleConnectTimeout,
this, clientSocket, onConnectFailed));
85 clientSocket->async_connect(remoteEndpoint,
86 bind(&TcpChannel::handleConnect,
this,
87 boost::asio::placeholders::error, clientSocket,
88 wantLocalFieldsEnabled, connectTimeoutEvent,
89 onFaceCreated, onConnectFailed));
95 return m_channelFaces.size();
99 TcpChannel::createFace(ip::tcp::socket&& socket,
101 bool wantLocalFieldsEnabled,
104 shared_ptr<Face>
face;
107 auto it = m_channelFaces.find(remoteEndpoint);
108 if (it == m_channelFaces.end()) {
111 auto linkService = make_unique<face::GenericLinkService>();
112 auto options = linkService->getOptions();
113 options.allowLocalFields = wantLocalFieldsEnabled;
114 linkService->setOptions(options);
116 auto transport = make_unique<face::TcpTransport>(std::move(socket), persistency);
118 face = make_shared<Face>(std::move(linkService), std::move(transport));
120 m_channelFaces[remoteEndpoint] = face;
122 [
this, remoteEndpoint] {
123 NFD_LOG_TRACE(
"Erasing " << remoteEndpoint <<
" from channel face map");
124 m_channelFaces.erase(remoteEndpoint);
131 boost::system::error_code error;
132 socket.shutdown(ip::tcp::socket::shutdown_both, error);
145 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept,
this,
146 boost::asio::placeholders::error,
147 onFaceCreated, onAcceptFailed));
151 TcpChannel::handleAccept(
const boost::system::error_code& error,
156 if (error == boost::asio::error::operation_aborted)
159 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Accept failed: " << error.message());
161 onAcceptFailed(500,
"Accept failed: " + error.message());
165 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Connection from " << m_acceptSocket.remote_endpoint());
167 createFace(std::move(m_acceptSocket),
true,
false, onFaceCreated);
170 accept(onFaceCreated, onAcceptFailed);
174 TcpChannel::handleConnect(
const boost::system::error_code& error,
175 const shared_ptr<ip::tcp::socket>& socket,
176 bool wantLocalFieldsEnabled,
183 #if (BOOST_VERSION == 105400) 186 boost::system::error_code anotherErrorCode;
187 socket->remote_endpoint(anotherErrorCode);
188 if (error || anotherErrorCode) {
192 if (error == boost::asio::error::operation_aborted)
195 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Connect failed: " << error.message());
200 onConnectFailed(504,
"Connect failed: " + error.message());
204 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Connected to " << socket->remote_endpoint());
206 createFace(std::move(*socket),
false, wantLocalFieldsEnabled, onFaceCreated);
210 TcpChannel::handleConnectTimeout(
const shared_ptr<ip::tcp::socket>& socket,
216 boost::system::error_code error;
217 socket->close(error);
220 onConnectFailed(504,
"Connect to remote endpoint timed out");
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()
#define NFD_LOG_DEBUG(expression)
TcpChannel(const tcp::Endpoint &localEndpoint)
Create TCP channel for the local endpoint.
std::shared_ptr< ns3::EventId > EventId
#define NFD_LOG_TRACE(expression)
Copyright (c) 2011-2015 Regents of the University of California.
boost::asio::ip::tcp::endpoint Endpoint
void connect(const tcp::Endpoint &remoteEndpoint, bool wantLocalFieldsEnabled, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, const time::seconds &timeout=time::seconds(4))
Create a face by establishing connection to remote endpoint.
void setUri(const FaceUri &uri)
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
#define NFD_LOG_WARN(expression)
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
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...
#define NFD_LOG_INIT(name)
size_t size() const
Get number of faces in the channel.