31#include <asio/connect.hpp>
32#include <asio/error_code.hpp>
33#include <asio/ip/basic_resolver_entry.hpp>
34#include <asio/ip/basic_resolver_iterator.hpp>
35#include <asio/ip/tcp.hpp>
36#include <fmt/format.h>
37#include <fmt/ostream.h>
58 public std::enable_shared_from_this<TCPConnector> {
82 TCPConnector(
const std::shared_ptr<executors::IExecutor>& executor,
84 std::shared_ptr<logging::Logger> logger)
95 const auto resolved_endpoints =
resolve(uri);
97 auto socket_ptr = std::make_unique<AsioSocket>(
100 asio::async_connect(socket, resolved_endpoints,
101 [self = this->shared_from_this(),
102 socket_ptr_moved = std::move(socket_ptr),
103 on_connected_moved = std::move(on_connected),
104 uri](
const asio::error_code& error,
106 self->on_connect(error, std::move(*socket_ptr_moved),
107 on_connected_moved, asio_address, uri);
119 [[nodiscard]] AsioResolver::results_type
resolve(
123 fmt::format(
"Scheme is different with the resolver: "
124 "expected={}, actual={}",
130 const std::string service = fmt::format(
131 "{}", uri.
port_number().value_or(
static_cast<std::uint16_t
>(0)));
132 asio::error_code error;
136 fmt::format(
"Failed to resolve {}: {}", uri, error.message());
142 for (
const auto& result : results) {
144 log_name_, uri, fmt::streamed(result.endpoint()));
164 const auto message = fmt::format(
165 "Failed to connect to {}: {}", uri, error.message());
168 Status(StatusCode::CONNECTION_FAILURE, message),
nullptr);
172 fmt::streamed(asio_address));
174 auto connection = std::make_shared<ConnectionType>(
176 on_connected(
Status(), std::move(connection));
187 const auto message = std::string(
"Executor is not set.");
190 StatusCode::PRECONDITION_NOT_MET, message);
Class of addresses of TCP.
Class of URIs (Uniform Resource Identifiers) to specify endpoints in this library.
std::string_view scheme() const noexcept
Get the scheme.
std::optional< std::uint16_t > port_number() const noexcept
Get the port number.
std::string_view host_or_path() const noexcept
Get the host name or file path.
Class of exceptions in cpp-msgpack-rpc library.
Class of configuration of parsers of messages.
std::function< void(const Status &, std::shared_ptr< IConnection >)> ConnectionCallback
Type of callback functions called when connecting process finished (even for failures).
asio::ip::tcp::resolver AsioResolver
Type of resolvers in asio library.
std::string log_name_
Name of the connection for logs.
config::MessageParserConfig message_parser_config_
Configuration of parsers of messages.
void on_connect(const asio::error_code &error, AsioSocket &&socket, const ConnectionCallback &on_connected, const AsioAddress &asio_address, const addresses::URI &uri)
Handle the result of connect operation.
Connection< AsioSocket, ConcreteAddress > ConnectionType
Type of connections.
void async_connect(const addresses::URI &uri, ConnectionCallback on_connected) override
Asynchronously connect to an endpoint.
std::weak_ptr< executors::IExecutor > executor_
Executor.
std::string scheme_
Scheme.
std::shared_ptr< logging::Logger > logger_
Logger.
TCPConnector(const std::shared_ptr< executors::IExecutor > &executor, const config::MessageParserConfig &message_parser_config, std::shared_ptr< logging::Logger > logger)
Constructor.
addresses::TCPAddress ConcreteAddress
Type of concrete addresses.
std::shared_ptr< executors::IExecutor > get_executor()
Get the executor.
asio::ip::tcp::socket AsioSocket
Type of sockets in asio library.
asio::ip::tcp::endpoint AsioAddress
Type of addresses in asio library.
AsioResolver::results_type resolve(const addresses::URI &uri)
Resolve a URI.
AsioResolver resolver_
Resolver.
Definition of Connection class.
Definition of IConnector class.
Definition of IExecutor class.
Definition of LogLevel enumeration.
Definition of Logger class.
#define MSGPACK_RPC_CRITICAL(LOGGER_PTR,...)
Write a critical log.
#define MSGPACK_RPC_WARN(LOGGER_PTR,...)
Write a warning log.
#define MSGPACK_RPC_TRACE(LOGGER_PTR,...)
Write a trace log.
#define MSGPACK_RPC_ERROR(LOGGER_PTR,...)
Write a error log.
Definition of MessageParserConfig class.
Definition of MsgpackRPCException class.
Namespace of fmt library.
Namespace of executors to process asynchronous tasks.
@ TRACE
Trace. (Internal operations to send and receive messages.)
Namespace of transport of messages via TCP.
Definition of OperationType enumeration.
Definition of Status class.
Definition of StatusCode enumeration.
Definition of TCPAddress.