39 : m_localEndpoint(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);
73 bool wantLpReliability,
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));
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,
155 onAcceptFailed(500,
"Accept failed: " + error.message());
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,
178 #if (BOOST_VERSION == 105400) 181 boost::system::error_code anotherErrorCode;
182 socket->remote_endpoint(anotherErrorCode);
183 if (error || anotherErrorCode) {
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");
void setUri(const FaceUri &uri)
TcpChannel(const tcp::Endpoint &localEndpoint)
Create TCP channel for the local endpoint.
bool isListening() const override
Returns whether the channel is listening.
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.
Accept any value the remote endpoint offers.
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy ...
void cancel(const EventId &eventId)
cancel a scheduled event
LpReliability::Options reliabilityOptions
options for reliability
detail::SimulatorIo & getGlobalIoService()
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...
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
bool isEnabled
enables link-layer reliability
std::shared_ptr< ns3::EventId > EventId
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
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
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Options that control the behavior of GenericLinkService.
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
Catch-all error for socket component errors that don't fit in other categories.
#define NFD_LOG_INIT(name)
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...