tcp-channel.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
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.
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/>.
24  */
25 
26 #ifndef NFD_DAEMON_FACE_TCP_CHANNEL_HPP
27 #define NFD_DAEMON_FACE_TCP_CHANNEL_HPP
28 
29 #include "channel.hpp"
30 
31 #include <ndn-cxx/util/scheduler.hpp>
32 
33 #include <boost/asio/ip/tcp.hpp>
34 
35 #include <map>
36 
37 namespace nfd::tcp {
38 using Endpoint = boost::asio::ip::tcp::endpoint;
39 } // namespace nfd::tcp
40 
41 namespace nfd::face {
42 
43 using DetermineFaceScopeFromAddress = std::function<ndn::nfd::FaceScope(const boost::asio::ip::address& local,
44  const boost::asio::ip::address& remote)>;
45 
52 class TcpChannel final : public Channel
53 {
54 public:
61  TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
62  DetermineFaceScopeFromAddress determineFaceScope);
63 
64  bool
65  isListening() const final
66  {
67  return m_acceptor.is_open();
68  }
69 
70  size_t
71  size() const final
72  {
73  return m_channelFaces.size();
74  }
75 
85  void
86  listen(const FaceCreatedCallback& onFaceCreated,
87  const FaceCreationFailedCallback& onAcceptFailed,
88  int backlog = boost::asio::socket_base::max_listen_connections);
89 
93  void
94  connect(const tcp::Endpoint& remoteEndpoint,
95  const FaceParams& params,
96  const FaceCreatedCallback& onFaceCreated,
97  const FaceCreationFailedCallback& onConnectFailed,
98  time::nanoseconds timeout = 8_s);
99 
100 private:
101  void
102  createFace(boost::asio::ip::tcp::socket&& socket,
103  const FaceParams& params,
104  const FaceCreatedCallback& onFaceCreated,
105  const FaceCreationFailedCallback& onFaceCreationFailed);
106 
107  void
108  accept(const FaceCreatedCallback& onFaceCreated,
109  const FaceCreationFailedCallback& onAcceptFailed);
110 
111  void
112  handleConnect(const boost::system::error_code& error,
113  const tcp::Endpoint& remoteEndpoint,
114  const shared_ptr<boost::asio::ip::tcp::socket>& socket,
115  const FaceParams& params,
116  const ndn::scheduler::EventId& connectTimeoutEvent,
117  const FaceCreatedCallback& onFaceCreated,
118  const FaceCreationFailedCallback& onConnectFailed);
119 
120  void
121  handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
122  const shared_ptr<boost::asio::ip::tcp::socket>& socket,
123  const FaceCreationFailedCallback& onConnectFailed);
124 
125 private:
126  const tcp::Endpoint m_localEndpoint;
127  const bool m_wantCongestionMarking;
128  boost::asio::ip::tcp::acceptor m_acceptor;
129  std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
130  DetermineFaceScopeFromAddress m_determineFaceScope;
131 };
132 
133 } // namespace nfd::face
134 
135 #endif // NFD_DAEMON_FACE_TCP_CHANNEL_HPP
Represents a channel that listens on a local endpoint.
Definition: channel.hpp:43
Class implementing a TCP-based channel to create faces.
Definition: tcp-channel.hpp:53
size_t size() const final
Returns the number of faces in the channel.
Definition: tcp-channel.hpp:71
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create a TCP channel for the specified localEndpoint.
Definition: tcp-channel.cpp:40
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::socket_base::max_listen_connections)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
Definition: tcp-channel.cpp:52
void connect(const tcp::Endpoint &remoteEndpoint, const FaceParams &params, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, time::nanoseconds timeout=8_s)
Create a face by establishing a TCP connection to remoteEndpoint.
Definition: tcp-channel.cpp:74
bool isListening() const final
Returns whether the channel is listening.
Definition: tcp-channel.hpp:65
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.
Definition: channel.hpp:94
std::function< ndn::nfd::FaceScope(const boost::asio::ip::address &local, const boost::asio::ip::address &remote)> DetermineFaceScopeFromAddress
Definition: tcp-channel.hpp:44
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...
Definition: channel.hpp:90
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp-channel.hpp:38
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:87