Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 24 | */ |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 28 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 29 | #include "channel.hpp" |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 30 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 31 | #include <boost/asio/local/stream_protocol.hpp> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::unix_stream { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 34 | using Endpoint = boost::asio::local::stream_protocol::endpoint; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | } // namespace nfd::unix_stream |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | namespace nfd::face { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 38 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 39 | /** |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 40 | * \brief Class implementing a local channel to create faces. |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 41 | * |
| 42 | * Channel can create faces as a response to incoming IPC connections |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 43 | * (UnixStreamChannel::listen() needs to be called for that to work). |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 44 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 45 | class UnixStreamChannel final : public Channel |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 46 | { |
| 47 | public: |
| 48 | /** |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 49 | * \brief Create a UnixStream channel for the specified \p endpoint. |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 50 | * |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 51 | * To enable the creation of faces upon incoming connections, one needs to |
| 52 | * explicitly call listen(). |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 53 | */ |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 54 | UnixStreamChannel(const unix_stream::Endpoint& endpoint, bool wantCongestionMarking); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 55 | |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 56 | ~UnixStreamChannel() final; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 57 | |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 58 | bool |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 59 | isListening() const final |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 60 | { |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 61 | return m_isListening; |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | size_t |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 65 | size() const final |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 66 | { |
| 67 | return m_size; |
| 68 | } |
| 69 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 70 | /** |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 71 | * \brief Start listening. |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 72 | * |
| 73 | * Enable listening on the Unix socket, waiting for incoming connections, |
| 74 | * and creating a face when a connection is made. |
| 75 | * |
| 76 | * Faces created in this way will have on-demand persistency. |
| 77 | * |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 78 | * \param onFaceCreated Callback to notify successful creation of the face |
| 79 | * \param onAcceptFailed Callback to notify when channel fails (accept call |
| 80 | * returns an error) |
| 81 | * \param backlog The maximum length of the queue of pending incoming |
| 82 | * connections |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame^] | 83 | * \throw std::system_error |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 84 | */ |
| 85 | void |
| 86 | listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 87 | const FaceCreationFailedCallback& onAcceptFailed, |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 88 | int backlog = boost::asio::socket_base::max_listen_connections); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 89 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 90 | private: |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 91 | void |
| 92 | accept(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 93 | const FaceCreationFailedCallback& onAcceptFailed); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 94 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 95 | private: |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 96 | const unix_stream::Endpoint m_endpoint; |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 97 | const bool m_wantCongestionMarking; |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 98 | bool m_isListening = false; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 99 | boost::asio::local::stream_protocol::acceptor m_acceptor; |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 100 | size_t m_size = 0; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 101 | }; |
| 102 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 103 | } // namespace nfd::face |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 104 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 105 | #endif // NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |