37 namespace ip = boost::asio::ip;
41 : m_localEndpoint(localEndpoint)
44 , m_wantCongestionMarking(wantCongestionMarking)
45 , m_determineFaceScope(
std::
move(determineFaceScope))
61 m_acceptor.open(m_localEndpoint.protocol());
62 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
63 if (m_localEndpoint.address().is_v6()) {
64 m_acceptor.set_option(ip::v6_only(
true));
66 m_acceptor.bind(m_localEndpoint);
67 m_acceptor.listen(backlog);
69 accept(onFaceCreated, onAcceptFailed);
80 auto it = m_channelFaces.find(remoteEndpoint);
81 if (it != m_channelFaces.end()) {
83 onFaceCreated(it->second);
88 auto timeoutEvent =
getScheduler().schedule(timeout, [=] {
89 handleConnectTimeout(remoteEndpoint, clientSocket, onConnectFailed);
93 clientSocket->async_connect(remoteEndpoint, [=] (
const auto& e) {
94 this->handleConnect(e, remoteEndpoint, clientSocket, params, timeoutEvent, onFaceCreated, onConnectFailed);
104 shared_ptr<Face> face;
105 boost::system::error_code ec;
109 if (onFaceCreationFailed) {
110 onFaceCreationFailed(500,
"Retrieve socket remote endpoint failed: " + ec.message());
115 auto it = m_channelFaces.find(remoteEndpoint);
116 if (it == m_channelFaces.end()) {
117 GenericLinkService::Options options;
123 options.allowCongestionMarking = m_wantCongestionMarking;
136 auto linkService = make_unique<GenericLinkService>(options);
137 auto faceScope = m_determineFaceScope(
socket.local_endpoint().address(),
138 socket.remote_endpoint().address());
141 face->setChannel(shared_from_this());
143 m_channelFaces[remoteEndpoint] = face;
151 boost::system::error_code error;
152 socket.shutdown(ip::tcp::socket::shutdown_both, error);
165 m_acceptor.async_accept(m_socket, [=] (
const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
169 TcpChannel::handleAccept(
const boost::system::error_code& error,
177 onAcceptFailed(500,
"Accept failed: " + error.message());
186 createFace(
std::move(m_socket), params, onFaceCreated, onAcceptFailed);
189 accept(onFaceCreated, onAcceptFailed);
193 TcpChannel::handleConnect(
const boost::system::error_code& error,
195 const shared_ptr<ip::tcp::socket>&
socket,
201 connectTimeoutEvent.
cancel();
205 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
207 onConnectFailed(504,
"Connection failed: " + error.message());
213 createFace(
std::move(*socket), params, onFaceCreated, onConnectFailed);
217 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
218 const shared_ptr<ip::tcp::socket>& socket,
224 boost::system::error_code error;
225 socket->close(error);
228 onConnectFailed(504,
"Connection timed out");
optional< uint64_t > defaultCongestionThreshold
void setUri(const FaceUri &uri)
#define NFD_LOG_INIT(name)
boost::logic::tribool wantCongestionMarking
Accept any value the remote endpoint offers.
std::function< ndn::nfd::FaceScope(const boost::asio::ip::address &local, const boost::asio::ip::address &remote)> DetermineFaceScopeFromAddress
ndn::nfd::FacePersistency persistency
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
std::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 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...
A handle for a scheduled event.
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create TCP channel for the local endpoint.
optional< time::nanoseconds > baseCongestionMarkingInterval
Copyright (c) 2011-2015 Regents of the University of California.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
boost::asio::ip::tcp::endpoint Endpoint
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
represents the underlying protocol and address used by a Face
bool isListening() const final
Returns whether the channel is listening.
void connect(const tcp::Endpoint &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, time::nanoseconds timeout=8_s)
Create a face by establishing a TCP connection to remoteEndpoint.
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Catch-all error for socket component errors that don't fit in other categories.
Class implementing TCP-based channel to create faces.
void cancel() const
Cancel the operation.
boost::chrono::nanoseconds nanoseconds
Parameters used to set Transport properties or LinkService options on a newly created face...