32 #include <boost/filesystem.hpp>
40 bool wantCongestionMarking)
41 : m_endpoint(endpoint)
45 , m_wantCongestionMarking(wantCongestionMarking)
47 setUri(FaceUri(m_endpoint));
56 boost::system::error_code error;
57 m_acceptor.close(error);
59 boost::filesystem::remove(m_endpoint.path(), error);
73 namespace fs = boost::filesystem;
75 fs::path socketPath(m_endpoint.path());
76 fs::file_type type = fs::symlink_status(socketPath).type();
78 if (type == fs::socket_file) {
79 boost::system::error_code error;
81 socket.connect(m_endpoint, error);
85 NDN_THROW(
Error(
"Socket file at " + m_endpoint.path() +
" belongs to another NFD process"));
87 else if (error == boost::asio::error::connection_refused ||
88 error == boost::asio::error::timed_out) {
92 fs::remove(socketPath);
95 else if (type != fs::file_not_found) {
96 NDN_THROW(
Error(m_endpoint.path() +
" already exists and is not a socket file"));
100 m_acceptor.bind(m_endpoint);
101 m_acceptor.listen(backlog);
103 if (::chmod(m_endpoint.path().data(), 0666) < 0) {
104 NDN_THROW_ERRNO(
Error(
"Failed to chmod " + m_endpoint.path()));
107 accept(onFaceCreated, onAcceptFailed);
115 m_acceptor.async_accept(m_socket, [=] (
const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
119 UnixStreamChannel::handleAccept(
const boost::system::error_code& error,
124 if (error != boost::asio::error::operation_aborted) {
127 onAcceptFailed(500,
"Accept failed: " + error.message());
134 GenericLinkService::Options options;
135 options.allowCongestionMarking = m_wantCongestionMarking;
136 auto linkService = make_unique<GenericLinkService>(options);
137 auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
138 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
139 face->setChannel(weak_from_this());
147 accept(onFaceCreated, onAcceptFailed);
void setUri(const FaceUri &uri) noexcept
UnixStreamChannel-related error.
bool isListening() const final
Returns whether the channel is listening.
UnixStreamChannel(const unix_stream::Endpoint &endpoint, bool wantCongestionMarking)
Create UnixStream channel for the specified endpoint.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Start listening.
~UnixStreamChannel() final
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
boost::asio::local::stream_protocol::endpoint Endpoint
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.