userver: userver/net/connect_tcp_by_name.hpp Source File
Loading...
Searching...
No Matches
connect_tcp_by_name.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/net/connect_tcp_by_name.hpp
4/// @brief @copybrief net::ConnectTcpByName
5
6#include <cstdint>
7#include <string>
8
9#include <userver/clients/dns/resolver_fwd.hpp>
10#include <userver/engine/deadline.hpp>
11#include <userver/engine/io/socket.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace net {
16
17/// @brief Establishes a TCP connection to the given host and port using DNS
18/// resolution via the provided @ref clients::dns::Resolver.
19///
20/// Resolves the host name using \p resolver (which may use cache, /etc/hosts,
21/// and network DNS), then tries to connect to each resolved address in turn.
22/// The first successful connection is returned. TCP_NODELAY is set on the
23/// socket.
24///
25/// @param host DNS name or IP address to connect to (e.g. "localhost",
26/// "example.com", "127.0.0.1").
27/// @param port TCP port number.
28/// @param resolver DNS resolver instance (e.g. from @ref clients::dns::Component).
29/// @param deadline Maximum time allowed for the whole operation (resolution +
30/// connection). Same deadline is used for both resolve and connect steps.
31/// @return A connected TCP socket.
32/// @throws clients::dns::ResolverException or @ref clients::dns::NotResolvedException
33/// if the host name cannot be resolved.
34/// @throws engine::io::IoException if connection to all resolved addresses
35/// fails (e.g. connection refused, timeout).
36///
37/// @note Without a Resolver (e.g. in non-component code) use
38/// @ref net::blocking::ConnectTcpByName.
39///
40/// @snippet src/net/connect_tcp_by_name_test.cpp ConnectTcpByName localhost
41engine::io::Socket ConnectTcpByName(
42 const std::string& host,
43 std::uint16_t port,
44 clients::dns::Resolver& resolver,
45 engine::Deadline deadline
46);
47
48} // namespace net
49
50USERVER_NAMESPACE_END