Kea: tcp_client.h Source File

| Kea 3.2.0-git

tcp_client.h
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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 TCP_CLIENT_H
8#define TCP_CLIENT_H
9
10#include <asiolink/io_address.h>
11#include <asiolink/io_service.h>
12#include <asiolink/tls_socket.h>
13#include <exceptions/exceptions.h>
14#include <tcp/wire_data.h>
15#include <boost/shared_ptr.hpp>
16#include <functional>
17#include <string>
18#include <thread>
19#include <vector>
20
21namespace isc {
22namespace tcp {
23
25 class TcpClientError : public Exception {
26public:
27 TcpClientError(const char* file, size_t line, const char* what) :
28 isc::Exception(file, line, what) { }
29};
30
31class TcpClientImpl;
32
80 class TcpClient {
81public:
83 struct RequestTimeout {
87 explicit RequestTimeout(long value)
88 : value_(value) {
89 }
90 long value_;
91 };
92
94 typedef std::function<void(const boost::system::error_code&,
95 const WireDataPtr&,
96 const std::string&)> RequestHandler;
97
105 typedef std::function<int(const WireDataPtr&,
106 std::string&)> CompleteCheck;
107
118 typedef std::function<bool(const boost::system::error_code&,
119 const int)> ConnectHandler;
120
132 typedef std::function<bool(const boost::system::error_code&,
133 const int)> HandshakeHandler;
134
138 typedef std::function<void(const int)> CloseHandler;
139
154 explicit TcpClient(const asiolink::IOServicePtr& io_service,
155 bool multi_threading_enabled,
156 size_t thread_pool_size = 0,
157 bool defer_thread_start = false);
158
160 ~TcpClient();
161
244 void asyncSendRequest(const asiolink::IOAddress& address,
245 const uint16_t port,
246 const asiolink::TlsContextPtr& tls_context,
247 const WireDataPtr& request,
248 const WireDataPtr& response,
249 const bool persistent,
250 const CompleteCheck& complete_check,
251 const RequestHandler& request_callback,
252 const RequestTimeout& request_timeout =
253 RequestTimeout(10000),
254 const ConnectHandler& connect_callback =
255 ConnectHandler(),
256 const HandshakeHandler& handshake_callback =
257 HandshakeHandler(),
258 const CloseHandler& close_callback =
259 CloseHandler());
260
266 void checkPermissions();
267
269 void start();
270
275 void pause();
276
281 void resume();
282
288 void stop();
289
302 void closeIfOutOfBand(int socket_fd);
303
309 const asiolink::IOServicePtr getThreadIOService() const;
310
314 uint16_t getThreadPoolSize() const;
315
319 uint16_t getThreadCount() const;
320
325 bool isRunning();
326
331 bool isStopped();
332
337 bool isPaused();
338
339private:
340
342 boost::shared_ptr<TcpClientImpl> impl_;
343};
344
346 typedef boost::shared_ptr<TcpClient> TcpClientPtr;
347
348} // end of namespace isc::tcp
349} // end of namespace isc
350
351#endif
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::tcp::TcpClientError::TcpClientError
TcpClientError(const char *file, size_t line, const char *what)
Definition tcp_client.h:27
isc::tcp::TcpClient::CompleteCheck
std::function< int(const WireDataPtr &, std::string &)> CompleteCheck
Completion check type.
Definition tcp_client.h:106
isc::tcp::TcpClient::closeIfOutOfBand
void closeIfOutOfBand(int socket_fd)
Closes a connection if it has an out-of-band socket event.
Definition tcp_client.cc:2114
isc::tcp::TcpClient::start
void start()
Starts running the client's thread pool, if multi-threaded.
Definition tcp_client.cc:2119
isc::tcp::TcpClient::getThreadCount
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
Definition tcp_client.cc:2154
isc::tcp::TcpClient::RequestHandler
std::function< void(const boost::system::error_code &, const WireDataPtr &, const std::string &)> RequestHandler
Callback type used in call to TcpClient::asyncSendRequest.
Definition tcp_client.h:96
isc::tcp::TcpClient::stop
void stop()
Halts client-side IO activity.
Definition tcp_client.cc:2139
isc::tcp::TcpClient::isPaused
bool isPaused()
Indicates if the thread pool is paused.
Definition tcp_client.cc:2169
isc::tcp::TcpClient::getThreadPoolSize
uint16_t getThreadPoolSize() const
Fetches the maximum size of the thread pool.
Definition tcp_client.cc:2149
isc::tcp::TcpClient::checkPermissions
void checkPermissions()
Check if the current thread can perform thread pool state transition.
Definition tcp_client.cc:2124
isc::tcp::TcpClient::asyncSendRequest
void asyncSendRequest(const asiolink::IOAddress &address, const uint16_t port, const asiolink::TlsContextPtr &tls_context, const WireDataPtr &request, const WireDataPtr &response, const bool persistent, const CompleteCheck &complete_check, const RequestHandler &request_callback, const RequestTimeout &request_timeout=RequestTimeout(10000), const ConnectHandler &connect_callback=ConnectHandler(), const HandshakeHandler &handshake_callback=HandshakeHandler(), const CloseHandler &close_callback=CloseHandler())
Queues new asynchronous TCP request for a given address.
Definition tcp_client.cc:2077
isc::tcp::TcpClient::isRunning
bool isRunning()
Indicates if the thread pool is running.
Definition tcp_client.cc:2159
isc::tcp::TcpClient::ConnectHandler
std::function< bool(const boost::system::error_code &, const int)> ConnectHandler
Optional handler invoked when client connects to the server.
Definition tcp_client.h:119
isc::tcp::TcpClient::pause
void pause()
Pauses the client's thread pool.
Definition tcp_client.cc:2129
isc::tcp::TcpClient::isStopped
bool isStopped()
Indicates if the thread pool is stopped.
Definition tcp_client.cc:2164
isc::tcp::TcpClient::TcpClient
TcpClient(const asiolink::IOServicePtr &io_service, bool multi_threading_enabled, size_t thread_pool_size=0, bool defer_thread_start=false)
Constructor.
Definition tcp_client.cc:2060
isc::tcp::TcpClient::HandshakeHandler
std::function< bool(const boost::system::error_code &, const int)> HandshakeHandler
Optional handler invoked when client performs the TLS handshake with the server.
Definition tcp_client.h:133
isc::tcp::TcpClient::resume
void resume()
Resumes running the client's thread pool.
Definition tcp_client.cc:2134
isc::tcp::TcpClient::getThreadIOService
const asiolink::IOServicePtr getThreadIOService() const
Fetches a pointer to the internal IOService used to drive the thread-pool in multi-threaded mode.
Definition tcp_client.cc:2144
isc::tcp::TcpClient::~TcpClient
~TcpClient()
Destructor.
Definition tcp_client.cc:2072
isc::tcp::TcpClient::CloseHandler
std::function< void(const int)> CloseHandler
Optional handler invoked when client closes the connection to the server.
Definition tcp_client.h:138
exceptions.h
io_address.h
io_service.h
isc::asiolink::TlsContextPtr
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
Definition common_tls.h:37
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::tcp
Definition mt_tcp_listener_mgr.cc:23
isc::tcp::WireDataPtr
boost::shared_ptr< WireData > WireDataPtr
Definition wire_data.h:18
isc::tcp::TcpClientPtr
boost::shared_ptr< TcpClient > TcpClientPtr
Defines a pointer to an TcpClient instance.
Definition tcp_client.h:346
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::tcp::TcpClient::RequestTimeout
TCP request/response timeout value.
Definition tcp_client.h:83
isc::tcp::TcpClient::RequestTimeout::RequestTimeout
RequestTimeout(long value)
Constructor.
Definition tcp_client.h:87
isc::tcp::TcpClient::RequestTimeout::value_
long value_
Timeout value specified.
Definition tcp_client.h:90
tls_socket.h
wire_data.h