Kea  2.5.3
mt_tcp_listener_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2022-2023 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef MT_TCP_LISTENER_MGR_H
8 #define MT_TCP_LISTENER_MGR_H
9 
10 #include <asiolink/io_address.h>
11 #include <asiolink/io_service.h>
13 #include <tcp/tcp_listener.h>
14 #include <thread>
15 #include <vector>
16 
17 namespace isc {
18 namespace tcp {
19 
21 const long TCP_IDLE_CONNECTION_TIMEOUT = 300 * 1000;
22 
24 typedef std::function<
26  const asiolink::IOAddress& server_address,
27  const unsigned short server_port,
28  const asiolink::TlsContextPtr& tls_context,
29  const TcpListener::IdleTimeout& idle_timeout,
30  const TcpConnectionFilterCallback& connection_filter)
32 
44 public:
57  MtTcpListenerMgr(TcpListenerFactory listener_factory,
58  const asiolink::IOAddress& address,
59  const uint16_t port,
60  const uint16_t thread_pool_size = 1,
62  TcpConnectionFilterCallback connection_filter = 0);
63 
65  virtual ~MtTcpListenerMgr();
66 
72  void checkPermissions();
73 
75  void start();
76 
80  void pause();
81 
85  void resume();
86 
88  void stop();
89 
94  bool isRunning();
95 
100  bool isStopped();
101 
106  bool isPaused();
107 
112  return (address_);
113  }
114 
118  uint16_t getPort() const {
119  return (port_);
120  }
121 
125  uint16_t getThreadPoolSize() const {
126  return (thread_pool_size_);
127  }
128 
133  return (tls_context_);
134  }
135 
139  uint16_t getThreadCount() const {
140  if (!thread_pool_) {
141  return (0);
142  }
143 
144  return (thread_pool_->getThreadCount());
145  }
146 
153  return (thread_io_service_);
154  }
155 
158  return(tcp_listener_);
159  }
160 
164  void setIdleTimeout(long milliseconds) {
165  idle_timeout_ = TcpListener::IdleTimeout(milliseconds);
166  }
167 
168  long getIdleTimeout() {
169  return (idle_timeout_.value_);
170  }
171 
172 private:
174  TcpListenerFactory listener_factory_;
175 
177  isc::asiolink::IOAddress address_;
178 
180  uint16_t port_;
181 
183  isc::asiolink::IOServicePtr thread_io_service_;
184 
186  TcpListenerPtr tcp_listener_;
187 
189  std::size_t thread_pool_size_;
190 
193 
195  asiolink::TlsContextPtr tls_context_;
196 
198  TcpConnectionFilterCallback connection_filter_;
199 
202  TcpListener::IdleTimeout idle_timeout_;
203 };
204 
206 typedef boost::shared_ptr<MtTcpListenerMgr> MtTcpListenerMgrPtr;
207 
208 } // namespace isc::tcp
209 } // namespace isc
210 
211 #endif // MT_TCP_LISTENER_MGR_H
Manages a thread-pool that is used to drive a TcpListener.
isc::asiolink::IOAddress getAddress() const
Fetches the IP address on which to listen.
asiolink::TlsContextPtr getTlsContext() const
Fetches the TLS context.
void pause()
Pauses the listener's thread pool.
const TcpListenerPtr getTcpListener()
Fetch a pointer to the listener.
void start()
Starts running the listener's thread pool.
bool isPaused()
Indicates if the thread pool is paused.
void checkPermissions()
Check if the current thread can perform thread pool state transition.
bool isStopped()
Indicates if the thread pool is stopped.
MtTcpListenerMgr(TcpListenerFactory listener_factory, const asiolink::IOAddress &address, const uint16_t port, const uint16_t thread_pool_size=1, asiolink::TlsContextPtr context=asiolink::TlsContextPtr(), TcpConnectionFilterCallback connection_filter=0)
Constructor.
uint16_t getPort() const
Fetches the port number on which to listen.
void setIdleTimeout(long milliseconds)
Sets the idle time per connection.
uint16_t getThreadPoolSize() const
Fetches the maximum size of the thread pool.
void resume()
Resumes running the listener's thread pool.
void stop()
Stops the listener's thread pool.
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
virtual ~MtTcpListenerMgr()
Destructor.
bool isRunning()
Indicates if the thread pool is running.
asiolink::IOServicePtr getThreadIOService() const
Fetches a pointer to the internal IOService used to drive the thread-pool in multi-threaded mode.
std::function< bool(const boost::asio::ip::tcp::endpoint &)> TcpConnectionFilterCallback
Type of the callback for filtering new connections by ip address.
const long TCP_IDLE_CONNECTION_TIMEOUT
Default connection idle timeout in milliseconds.
boost::shared_ptr< MtTcpListenerMgr > MtTcpListenerMgrPtr
Defines a shared pointer to MtTcpListenerMgr.
boost::shared_ptr< TcpListener > TcpListenerPtr
Pointer to a TcpListener.
Definition: tcp_listener.h:164
std::function< TcpListenerPtr(asiolink::IOService &io_service, const asiolink::IOAddress &server_address, const unsigned short server_port, const asiolink::TlsContextPtr &tls_context, const TcpListener::IdleTimeout &idle_timeout, const TcpConnectionFilterCallback &connection_filter) > TcpListenerFactory
Defines a factory function for creating TcpListeners.
Defines the logger used by the top-level component of kea-lfc.
Idle connection timeout.
Definition: tcp_listener.h:31
long value_
Connection idle timeout value specified.
Definition: tcp_listener.h:38