Kea 3.1.9
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>
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
25class 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
80class TcpClient {
81public:
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 =
256 const HandshakeHandler& handshake_callback =
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
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
346typedef boost::shared_ptr<TcpClient> TcpClientPtr;
347
348} // end of namespace isc::tcp
349} // end of namespace isc
350
351#endif
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
TcpClientError(const char *file, size_t line, const char *what)
Definition tcp_client.h:27
std::function< int(const WireDataPtr &, std::string &)> CompleteCheck
Completion check type.
Definition tcp_client.h:106
void closeIfOutOfBand(int socket_fd)
Closes a connection if it has an out-of-band socket event.
void start()
Starts running the client's thread pool, if multi-threaded.
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
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
void stop()
Halts client-side IO activity.
bool isPaused()
Indicates if the thread pool is paused.
uint16_t getThreadPoolSize() const
Fetches the maximum size of the thread pool.
void checkPermissions()
Check if the current thread can perform thread pool state transition.
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.
bool isRunning()
Indicates if the thread pool is running.
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
void pause()
Pauses the client's thread pool.
bool isStopped()
Indicates if the thread pool is stopped.
TcpClient(const asiolink::IOServicePtr &io_service, bool multi_threading_enabled, size_t thread_pool_size=0, bool defer_thread_start=false)
Constructor.
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
void resume()
Resumes running the client's thread pool.
const asiolink::IOServicePtr getThreadIOService() const
Fetches a pointer to the internal IOService used to drive the thread-pool in multi-threaded mode.
~TcpClient()
Destructor.
std::function< void(const int)> CloseHandler
Optional handler invoked when client closes the connection to the server.
Definition tcp_client.h:138
boost::shared_ptr< WireData > WireDataPtr
Definition wire_data.h:18
boost::shared_ptr< TcpClient > TcpClientPtr
Defines a pointer to an TcpClient instance.
Definition tcp_client.h:346
Defines the logger used by the top-level component of kea-lfc.
TCP request/response timeout value.
Definition tcp_client.h:83
RequestTimeout(long value)
Constructor.
Definition tcp_client.h:87
long value_
Timeout value specified.
Definition tcp_client.h:90