face: use new-style async_accept API

Change-Id: Id9de72c0922ec9ea13e5b1d9ded27dd7edf67e38
This commit is contained in:
Davide Pesavento
2023-10-05 02:12:02 -04:00
parent d91fe6d921
commit 9bf64cc60d
7 changed files with 69 additions and 95 deletions
+8 -8
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -36,13 +36,13 @@
namespace nfd::face {
/**
* \brief Class implementing Ethernet-based channel to create faces
* \brief Class implementing an Ethernet-based channel to create faces.
*/
class EthernetChannel final : public Channel
{
public:
/**
* \brief EthernetChannel-related error
* \brief EthernetChannel-related error.
*/
class Error : public std::runtime_error
{
@@ -51,10 +51,10 @@ public:
};
/**
* \brief Create an Ethernet channel on the given \p localEndpoint (network interface)
* \brief Create an Ethernet channel on the given \p localEndpoint (network interface).
*
* To enable creation of faces upon incoming connections,
* one needs to explicitly call EthernetChannel::listen method.
* To enable the creation of faces upon incoming connections, one needs to
* explicitly call listen().
*/
EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
time::nanoseconds idleTimeout);
@@ -72,7 +72,7 @@ public:
}
/**
* \brief Create a unicast Ethernet face toward \p remoteEndpoint
* \brief Create a unicast Ethernet face toward \p remoteEndpoint.
*/
void
connect(const ethernet::Address& remoteEndpoint,
@@ -81,7 +81,7 @@ public:
const FaceCreationFailedCallback& onConnectFailed);
/**
* \brief Start listening
* \brief Start listening.
*
* Enable listening on the local endpoint, waiting for incoming frames,
* and creating a face when a frame is received from a new remote host.
+16 -23
View File
@@ -40,9 +40,8 @@ namespace ip = boost::asio::ip;
TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
DetermineFaceScopeFromAddress determineFaceScope)
: m_localEndpoint(localEndpoint)
, m_acceptor(getGlobalIoService())
, m_socket(getGlobalIoService())
, m_wantCongestionMarking(wantCongestionMarking)
, m_acceptor(getGlobalIoService())
, m_determineFaceScope(std::move(determineFaceScope))
{
setUri(FaceUri(m_localEndpoint));
@@ -163,31 +162,25 @@ void
TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed)
{
m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
}
void
TcpChannel::handleAccept(const boost::system::error_code& error,
const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed)
{
if (error) {
if (error != boost::asio::error::operation_aborted) {
NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
if (onAcceptFailed)
onAcceptFailed(500, "Accept failed: " + error.message());
m_acceptor.async_accept([=] (const boost::system::error_code& error, ip::tcp::socket socket) {
if (error) {
if (error != boost::asio::error::operation_aborted) {
NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
if (onAcceptFailed)
onAcceptFailed(500, "Accept failed: " + error.message());
}
return;
}
return;
}
NFD_LOG_CHAN_TRACE("Incoming connection from " << m_socket.remote_endpoint());
NFD_LOG_CHAN_TRACE("Incoming connection from " << socket.remote_endpoint());
FaceParams params;
params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
createFace(std::move(m_socket), params, onFaceCreated, onAcceptFailed);
FaceParams params;
params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
createFace(std::move(socket), params, onFaceCreated, onAcceptFailed);
// prepare accepting the next connection
accept(onFaceCreated, onAcceptFailed);
// prepare accepting the next connection
accept(onFaceCreated, onAcceptFailed);
});
}
void
+1 -7
View File
@@ -104,11 +104,6 @@ private:
accept(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed);
void
handleAccept(const boost::system::error_code& error,
const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed);
void
handleConnect(const boost::system::error_code& error,
const tcp::Endpoint& remoteEndpoint,
@@ -125,10 +120,9 @@ private:
private:
const tcp::Endpoint m_localEndpoint;
const bool m_wantCongestionMarking;
boost::asio::ip::tcp::acceptor m_acceptor;
boost::asio::ip::tcp::socket m_socket;
std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
bool m_wantCongestionMarking;
DetermineFaceScopeFromAddress m_determineFaceScope;
};
+9 -10
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -34,17 +34,16 @@
namespace nfd::face {
/**
* \brief Class implementing UDP-based channel to create faces
* \brief Class implementing a UDP-based channel to create faces.
*/
class UdpChannel final : public Channel
{
public:
/**
* \brief Create a UDP channel on the given \p localEndpoint
* \brief Create a UDP channel on the given \p localEndpoint.
*
* To enable creation of faces upon incoming connections,
* one needs to explicitly call UdpChannel::listen method.
* The created socket is bound to \p localEndpoint.
* To enable the creation of faces upon incoming connections, one needs to
* explicitly call listen(). The created socket is bound to \p localEndpoint.
*/
UdpChannel(const udp::Endpoint& localEndpoint,
time::nanoseconds idleTimeout,
@@ -64,7 +63,7 @@ public:
}
/**
* \brief Create a unicast UDP face toward \p remoteEndpoint
* \brief Create a unicast UDP face toward \p remoteEndpoint.
*/
void
connect(const udp::Endpoint& remoteEndpoint,
@@ -73,7 +72,7 @@ public:
const FaceCreationFailedCallback& onConnectFailed);
/**
* \brief Start listening
* \brief Start listening.
*
* Enable listening on the local endpoint, waiting for incoming datagrams,
* and creating a face when a datagram is received from a new remote host.
@@ -94,7 +93,7 @@ private:
/**
* \brief The channel has received a packet from a remote
* endpoint not associated with any UDP face yet
* endpoint not associated with any UDP face yet.
*/
void
handleNewPeer(const boost::system::error_code& error,
@@ -113,7 +112,7 @@ private:
std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces;
const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
bool m_wantCongestionMarking;
const bool m_wantCongestionMarking;
};
} // namespace nfd::face
+23 -30
View File
@@ -39,10 +39,8 @@ NFD_LOG_INIT(UnixStreamChannel);
UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint,
bool wantCongestionMarking)
: m_endpoint(endpoint)
, m_acceptor(getGlobalIoService())
, m_socket(getGlobalIoService())
, m_size(0)
, m_wantCongestionMarking(wantCongestionMarking)
, m_acceptor(getGlobalIoService())
{
setUri(FaceUri(m_endpoint));
NFD_LOG_CHAN_INFO("Creating channel");
@@ -112,39 +110,34 @@ void
UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed)
{
m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
}
void
UnixStreamChannel::handleAccept(const boost::system::error_code& error,
const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed)
{
if (error) {
if (error != boost::asio::error::operation_aborted) {
NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
if (onAcceptFailed)
onAcceptFailed(500, "Accept failed: " + error.message());
m_acceptor.async_accept([=] (const boost::system::error_code& error,
boost::asio::local::stream_protocol::socket socket) {
if (error) {
if (error != boost::asio::error::operation_aborted) {
NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
if (onAcceptFailed)
onAcceptFailed(500, "Accept failed: " + error.message());
}
return;
}
return;
}
NFD_LOG_CHAN_TRACE("Incoming connection via fd " << m_socket.native_handle());
NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle());
GenericLinkService::Options options;
options.allowCongestionMarking = m_wantCongestionMarking;
auto linkService = make_unique<GenericLinkService>(options);
auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
auto face = make_shared<Face>(std::move(linkService), std::move(transport));
face->setChannel(weak_from_this());
GenericLinkService::Options options;
options.allowCongestionMarking = m_wantCongestionMarking;
auto linkService = make_unique<GenericLinkService>(options);
auto transport = make_unique<UnixStreamTransport>(std::move(socket));
auto face = make_shared<Face>(std::move(linkService), std::move(transport));
face->setChannel(weak_from_this());
++m_size;
connectFaceClosedSignal(*face, [this] { --m_size; });
++m_size;
connectFaceClosedSignal(*face, [this] { --m_size; });
onFaceCreated(face);
onFaceCreated(face);
// prepare accepting the next connection
accept(onFaceCreated, onAcceptFailed);
// prepare accepting the next connection
accept(onFaceCreated, onAcceptFailed);
});
}
} // namespace nfd::face
+2 -8
View File
@@ -101,17 +101,11 @@ private:
accept(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed);
void
handleAccept(const boost::system::error_code& error,
const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed);
private:
const unix_stream::Endpoint m_endpoint;
const bool m_wantCongestionMarking;
boost::asio::local::stream_protocol::acceptor m_acceptor;
boost::asio::local::stream_protocol::socket m_socket;
size_t m_size;
bool m_wantCongestionMarking;
size_t m_size = 0;
};
} // namespace nfd::face
+10 -9
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -36,17 +36,16 @@ using Endpoint = boost::asio::ip::tcp::endpoint;
namespace nfd::face {
/**
* \brief Class implementing WebSocket-based channel to create faces
* \brief Class implementing a WebSocket-based channel to create faces.
*/
class WebSocketChannel final : public Channel
{
public:
/**
* \brief Create WebSocket channel for the local endpoint
* \brief Create a WebSocket channel for the given \p localEndpoint.
*
* To enable creation of faces upon incoming connections,
* one needs to explicitly call WebSocketChannel::listen method.
* The created channel is bound to the localEndpoint.
* To enable the creation of faces upon incoming connections, one needs to
* explicitly call listen(). The created channel is bound to \p localEndpoint.
*/
explicit
WebSocketChannel(const websocket::Endpoint& localEndpoint);
@@ -65,7 +64,7 @@ public:
/**
* \brief Enable listening on the local endpoint, accept connections,
* and create faces when remote host makes a connection
* and create faces when remote host makes a connection.
*
* \param onFaceCreated Callback to notify successful creation of a face
*/
@@ -73,12 +72,14 @@ public:
listen(const FaceCreatedCallback& onFaceCreated);
NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
/** \pre listen hasn't been invoked
/**
* \pre listen() has not been invoked.
*/
void
setPingInterval(time::milliseconds interval);
/** \pre listen hasn't been invoked
/**
* \pre listen() has not been invoked.
*/
void
setPongTimeout(time::milliseconds timeout);