Problem: tcp_connecter_t and tcp_listener_t duplicate code around opening and configuring a TCP socket

Solution: extract common parts into tcp_open_socket function
This commit is contained in:
Simon Giesecke
2019-02-04 07:31:08 -05:00
parent 3f4e64edc0
commit 68d520ef68
4 changed files with 82 additions and 91 deletions

View File

@@ -174,63 +174,17 @@ int zmq::tcp_connecter_t::open ()
_addr->resolved.tcp_addr = new (std::nothrow) tcp_address_t ();
alloc_assert (_addr->resolved.tcp_addr);
int rc = _addr->resolved.tcp_addr->resolve (_addr->address.c_str (), false,
options.ipv6);
if (rc != 0) {
_s = tcp_open_socket (_addr->address.c_str (), options,
_addr->resolved.tcp_addr);
if (_s == retired_fd) {
LIBZMQ_DELETE (_addr->resolved.tcp_addr);
return -1;
}
zmq_assert (_addr->resolved.tcp_addr != NULL);
const tcp_address_t *const tcp_addr = _addr->resolved.tcp_addr;
// Create the socket.
_s = open_socket (tcp_addr->family (), SOCK_STREAM, IPPROTO_TCP);
// IPv6 address family not supported, try automatic downgrade to IPv4.
if (_s == zmq::retired_fd && tcp_addr->family () == AF_INET6
&& errno == EAFNOSUPPORT && options.ipv6) {
rc = _addr->resolved.tcp_addr->resolve (_addr->address.c_str (), false,
false);
if (rc != 0) {
LIBZMQ_DELETE (_addr->resolved.tcp_addr);
return -1;
}
_s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
if (_s == retired_fd) {
return -1;
}
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
if (tcp_addr->family () == AF_INET6)
enable_ipv4_mapping (_s);
// Set the IP Type-Of-Service priority for this socket
if (options.tos != 0)
set_ip_type_of_service (_s, options.tos);
// Bind the socket to a device if applicable
if (!options.bound_device.empty ())
bind_to_device (_s, options.bound_device);
// Set the socket to non-blocking mode so that we get async connect().
unblock_socket (_s);
// Set the socket to loopback fastpath if configured.
if (options.loopback_fastpath)
tcp_tune_loopback_fast_path (_s);
// Set the socket buffer limits for the underlying socket.
if (options.sndbuf >= 0)
set_tcp_send_buffer (_s, options.sndbuf);
if (options.rcvbuf >= 0)
set_tcp_receive_buffer (_s, options.rcvbuf);
// Set the IP Type-Of-Service for the underlying socket
if (options.tos != 0)
set_ip_type_of_service (_s, options.tos);
int rc;
// Set a source address for conversations
if (tcp_addr->has_src_addr ()) {