face: Stop using shared_ptr to manage acceptors
This commit also includes a major cleanup of all channels.
Change-Id: I10db9709e0cba6a0691a86482c60b5dbb2956f68
Refs: #2613
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index d64f333..52df192 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -1,11 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014 Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,9 +21,10 @@
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#include "unix-stream-channel.hpp"
+#include "unix-stream-face.hpp"
#include "core/global-io.hpp"
#include <boost/filesystem.hpp>
@@ -35,23 +37,22 @@
using namespace boost::asio::local;
UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint)
- : m_endpoint(endpoint)
- , m_isListening(false)
+ : m_acceptor(getGlobalIoService())
+ , m_endpoint(endpoint)
{
- setUri(FaceUri(endpoint));
+ setUri(FaceUri(m_endpoint));
}
UnixStreamChannel::~UnixStreamChannel()
{
- if (m_isListening)
- {
- // use the non-throwing variants during destruction
- // and ignore any errors
- boost::system::error_code error;
- m_acceptor->close(error);
- NFD_LOG_TRACE("[" << m_endpoint << "] Removing socket file");
- boost::filesystem::remove(m_endpoint.path(), error);
- }
+ if (isListening()) {
+ // use the non-throwing variants during destruction
+ // and ignore any errors
+ boost::system::error_code error;
+ m_acceptor.close(error);
+ NFD_LOG_DEBUG("[" << m_endpoint << "] Removing socket file");
+ boost::filesystem::remove(m_endpoint.path(), error);
+ }
}
void
@@ -59,7 +60,7 @@
const ConnectFailedCallback& onAcceptFailed,
int backlog/* = acceptor::max_connections*/)
{
- if (m_isListening) {
+ if (isListening()) {
NFD_LOG_WARN("[" << m_endpoint << "] Already listening");
return;
}
@@ -69,78 +70,73 @@
fs::path socketPath(m_endpoint.path());
fs::file_type type = fs::symlink_status(socketPath).type();
- if (type == fs::socket_file)
- {
- boost::system::error_code error;
- stream_protocol::socket socket(getGlobalIoService());
- socket.connect(m_endpoint, error);
- NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: "
- + error.message());
- if (!error)
- {
- // someone answered, leave the socket alone
- throw Error("Socket file at " + m_endpoint.path()
- + " belongs to another NFD process");
- }
- else if (error == boost::system::errc::connection_refused ||
- error == boost::system::errc::timed_out)
- {
- // no one is listening on the remote side,
- // we can safely remove the socket file
- NFD_LOG_INFO("[" << m_endpoint << "] Removing stale socket file");
- fs::remove(socketPath);
- }
+ if (type == fs::socket_file) {
+ boost::system::error_code error;
+ stream_protocol::socket socket(getGlobalIoService());
+ socket.connect(m_endpoint, error);
+ NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: "
+ + error.message());
+ if (!error) {
+ // someone answered, leave the socket alone
+ throw Error("Socket file at " + m_endpoint.path()
+ + " belongs to another NFD process");
}
- else if (type != fs::file_not_found)
- {
- throw Error(m_endpoint.path() + " already exists and is not a socket file");
+ else if (error == boost::asio::error::connection_refused ||
+ error == boost::asio::error::timed_out) {
+ // no one is listening on the remote side,
+ // we can safely remove the stale socket
+ NFD_LOG_DEBUG("[" << m_endpoint << "] Removing stale socket file");
+ fs::remove(socketPath);
}
+ }
+ else if (type != fs::file_not_found) {
+ throw Error(m_endpoint.path() + " already exists and is not a socket file");
+ }
- m_acceptor = make_shared<stream_protocol::acceptor>(ref(getGlobalIoService()));
- m_acceptor->open();
- m_acceptor->bind(m_endpoint);
- m_acceptor->listen(backlog);
- m_isListening = true;
+ m_acceptor.open();
+ m_acceptor.bind(m_endpoint);
+ m_acceptor.listen(backlog);
- if (::chmod(m_endpoint.path().c_str(), 0666) < 0)
- {
- throw Error("Failed to chmod() socket file at " + m_endpoint.path());
- }
+ if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
+ throw Error("chmod(" + m_endpoint.path() + ") failed: " + std::strerror(errno));
+ }
- shared_ptr<stream_protocol::socket> clientSocket =
- make_shared<stream_protocol::socket>(ref(getGlobalIoService()));
-
- m_acceptor->async_accept(*clientSocket,
- bind(&UnixStreamChannel::handleSuccessfulAccept, this,
- boost::asio::placeholders::error, clientSocket,
- onFaceCreated, onAcceptFailed));
+ // start accepting connections
+ accept(onFaceCreated, onAcceptFailed);
}
void
-UnixStreamChannel::handleSuccessfulAccept(const boost::system::error_code& error,
- const shared_ptr<stream_protocol::socket>& socket,
- const FaceCreatedCallback& onFaceCreated,
- const ConnectFailedCallback& onAcceptFailed)
+UnixStreamChannel::accept(const FaceCreatedCallback &onFaceCreated,
+ const ConnectFailedCallback &onAcceptFailed)
+{
+ auto socket = make_shared<stream_protocol::socket>(ref(getGlobalIoService()));
+
+ m_acceptor.async_accept(*socket,
+ bind(&UnixStreamChannel::handleAccept, this,
+ boost::asio::placeholders::error,
+ socket, onFaceCreated, onAcceptFailed));
+}
+
+void
+UnixStreamChannel::handleAccept(const boost::system::error_code& error,
+ const shared_ptr<stream_protocol::socket>& socket,
+ const FaceCreatedCallback& onFaceCreated,
+ const ConnectFailedCallback& onAcceptFailed)
{
if (error) {
- if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
+ if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
return;
- NFD_LOG_DEBUG("[" << m_endpoint << "] Connection failed: " << error.message());
- onAcceptFailed("Connection failed: " + error.message());
+ NFD_LOG_DEBUG("[" << m_endpoint << "] Accept failed: " << error.message());
+ if (onAcceptFailed)
+ onAcceptFailed(error.message());
return;
}
- NFD_LOG_DEBUG("[" << m_endpoint << "] << Incoming connection");
-
- shared_ptr<stream_protocol::socket> clientSocket =
- make_shared<stream_protocol::socket>(ref(getGlobalIoService()));
+ NFD_LOG_DEBUG("[" << m_endpoint << "] Incoming connection");
// prepare accepting the next connection
- m_acceptor->async_accept(*clientSocket,
- bind(&UnixStreamChannel::handleSuccessfulAccept, this,
- boost::asio::placeholders::error, clientSocket,
- onFaceCreated, onAcceptFailed));
+ accept(onFaceCreated, onAcceptFailed);
shared_ptr<UnixStreamFace> face = make_shared<UnixStreamFace>(socket);
onFaceCreated(face);