30 #include <boost/filesystem.hpp> 38 : m_endpoint(endpoint)
50 boost::system::error_code error;
51 m_acceptor.close(error);
53 boost::filesystem::remove(m_endpoint.path(), error);
63 NFD_LOG_WARN(
"[" << m_endpoint <<
"] Already listening");
67 namespace fs = boost::filesystem;
69 fs::path socketPath(m_endpoint.path());
70 fs::file_type type = fs::symlink_status(socketPath).type();
72 if (type == fs::socket_file) {
73 boost::system::error_code error;
75 socket.connect(m_endpoint, error);
76 NFD_LOG_TRACE(
"[" << m_endpoint <<
"] connect() on existing socket file returned: " 80 BOOST_THROW_EXCEPTION(
Error(
"Socket file at " + m_endpoint.path()
81 +
" belongs to another NFD process"));
83 else if (error == boost::asio::error::connection_refused ||
84 error == boost::asio::error::timed_out) {
87 NFD_LOG_DEBUG(
"[" << m_endpoint <<
"] Removing stale socket file");
88 fs::remove(socketPath);
91 else if (type != fs::file_not_found) {
92 BOOST_THROW_EXCEPTION(
Error(m_endpoint.path() +
" already exists and is not a socket file"));
96 m_acceptor.bind(m_endpoint);
97 m_acceptor.listen(backlog);
99 if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
100 BOOST_THROW_EXCEPTION(
Error(
"chmod(" + m_endpoint.path() +
") failed: " +
101 std::strerror(errno)));
105 accept(onFaceCreated, onAcceptFailed);
112 m_acceptor.async_accept(m_socket, bind(&UnixStreamChannel::handleAccept,
this,
113 boost::asio::placeholders::error,
114 onFaceCreated, onAcceptFailed));
118 UnixStreamChannel::handleAccept(
const boost::system::error_code& error,
123 if (error == boost::asio::error::operation_aborted)
126 NFD_LOG_DEBUG(
"[" << m_endpoint <<
"] Accept failed: " << error.message());
128 onAcceptFailed(error.message());
135 auto localUri =
FaceUri(m_socket.local_endpoint());
136 auto face = make_shared<UnixStreamFace>(remoteUri, localUri, std::move(m_socket));
140 accept(onFaceCreated, onAcceptFailed);
#define NFD_LOG_DEBUG(expression)
represents the underlying protocol and address used by a Face
UnixStreamChannel-related error.
#define NFD_LOG_WARN(expression)
Copyright (c) 2011-2015 Regents of the University of California.
UnixStreamChannel(const unix_stream::Endpoint &endpoint)
Create UnixStream channel for the specified endpoint.
void setUri(const FaceUri &uri)
function< void(const std::string &reason)> ConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
boost::asio::local::stream_protocol::endpoint Endpoint
boost::asio::io_service & getGlobalIoService()
void listen(const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Enable listening on the local endpoint, accept connections, and create faces when a connection is mad...
static FaceUri fromFd(int fd)
create fd FaceUri from file descriptor
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...