face: use new-style async_accept API
Change-Id: Id9de72c0922ec9ea13e5b1d9ded27dd7edf67e38
diff --git a/daemon/face/ethernet-channel.hpp b/daemon/face/ethernet-channel.hpp
index 8c11824..302b6e7 100644
--- a/daemon/face/ethernet-channel.hpp
+++ b/daemon/face/ethernet-channel.hpp
@@ -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 @@
};
/**
- * \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 @@
}
/**
- * \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 @@
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.
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index 17f2786..38fcd05 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -40,9 +40,8 @@
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 @@
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
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index 1220687..cc000ea 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -105,11 +105,6 @@
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,
const shared_ptr<boost::asio::ip::tcp::socket>& socket,
@@ -125,10 +120,9 @@
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;
};
diff --git a/daemon/face/udp-channel.hpp b/daemon/face/udp-channel.hpp
index f4f53da..e594f64 100644
--- a/daemon/face/udp-channel.hpp
+++ b/daemon/face/udp-channel.hpp
@@ -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 @@
}
/**
- * \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 @@
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 @@
/**
* \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 @@
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
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index b2bb84a..906d23e 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -39,10 +39,8 @@
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 @@
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
diff --git a/daemon/face/unix-stream-channel.hpp b/daemon/face/unix-stream-channel.hpp
index 705d6f8..7c7f287 100644
--- a/daemon/face/unix-stream-channel.hpp
+++ b/daemon/face/unix-stream-channel.hpp
@@ -101,17 +101,11 @@
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
diff --git a/daemon/face/websocket-channel.hpp b/daemon/face/websocket-channel.hpp
index d40bc70..a793a7c 100644
--- a/daemon/face/websocket-channel.hpp
+++ b/daemon/face/websocket-channel.hpp
@@ -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 @@
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 @@
/**
* \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 @@
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);