Save the addrinfo values for reuse on subsequent connect() call
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -42,35 +43,57 @@ void SocketHandler::init_socket()
|
|||||||
|
|
||||||
void SocketHandler::connect(const std::string& address, const std::string& port)
|
void SocketHandler::connect(const std::string& address, const std::string& port)
|
||||||
{
|
{
|
||||||
if (!this->connecting)
|
|
||||||
{
|
|
||||||
log_info("Trying to connect to " << address << ":" << port);
|
|
||||||
}
|
|
||||||
this->connecting = true;
|
|
||||||
this->address = address;
|
this->address = address;
|
||||||
this->port = port;
|
this->port = port;
|
||||||
|
|
||||||
struct addrinfo hints;
|
utils::ScopeGuard sg;
|
||||||
memset(&hints, 0, sizeof(struct addrinfo));
|
|
||||||
hints.ai_flags = 0;
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = 0;
|
|
||||||
|
|
||||||
struct addrinfo* addr_res;
|
struct addrinfo* addr_res;
|
||||||
const int res = ::getaddrinfo(address.c_str(), port.c_str(), &hints, &addr_res);
|
|
||||||
|
|
||||||
if (res != 0)
|
if (!this->connecting)
|
||||||
{
|
{
|
||||||
log_warning(std::string("getaddrinfo failed: ") + gai_strerror(res));
|
log_info("Trying to connect to " << address << ":" << port);
|
||||||
this->close();
|
// Get the addrinfo from getaddrinfo, only if this is the first call
|
||||||
this->on_connection_failed(gai_strerror(res));
|
// of this function.
|
||||||
return ;
|
struct addrinfo hints;
|
||||||
}
|
memset(&hints, 0, sizeof(struct addrinfo));
|
||||||
|
hints.ai_flags = 0;
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_protocol = 0;
|
||||||
|
|
||||||
// Make sure the alloced structure is always freed at the end of the
|
const int res = ::getaddrinfo(address.c_str(), port.c_str(), &hints, &addr_res);
|
||||||
// function
|
|
||||||
utils::ScopeGuard sg([&addr_res](){ freeaddrinfo(addr_res); });
|
if (res != 0)
|
||||||
|
{
|
||||||
|
log_warning(std::string("getaddrinfo failed: ") + gai_strerror(res));
|
||||||
|
this->close();
|
||||||
|
this->on_connection_failed(gai_strerror(res));
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
// Make sure the alloced structure is always freed at the end of the
|
||||||
|
// function
|
||||||
|
sg.add_callback([&addr_res](){ freeaddrinfo(addr_res); });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// This function is called again, use the saved addrinfo structure,
|
||||||
|
// instead of re-doing the whole getaddrinfo process. We insert only
|
||||||
|
// the meaningful values in the structure, and indicate that these are
|
||||||
|
// the only possible values with ai_next = NULL.
|
||||||
|
addr_res = (struct addrinfo*)malloc(sizeof(struct addrinfo));
|
||||||
|
if (!addr_res)
|
||||||
|
{
|
||||||
|
this->close();
|
||||||
|
this->on_connection_failed("memory error");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
sg.add_callback([&addr_res](){ free(addr_res); });
|
||||||
|
addr_res->ai_next = NULL;
|
||||||
|
addr_res->ai_addr = &this->ai_addr;
|
||||||
|
addr_res->ai_addrlen = this->ai_addrlen;
|
||||||
|
}
|
||||||
|
this->connecting = true;
|
||||||
|
|
||||||
for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next)
|
for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next)
|
||||||
{
|
{
|
||||||
@@ -87,6 +110,9 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
|
|||||||
// is ready to be written on.
|
// is ready to be written on.
|
||||||
log_debug("Need to retry connecting later..." << strerror(errno));
|
log_debug("Need to retry connecting later..." << strerror(errno));
|
||||||
this->poller->watch_send_events(this);
|
this->poller->watch_send_events(this);
|
||||||
|
// Save the addrinfo structure, to use it on the next call
|
||||||
|
this->ai_addrlen = rp->ai_addrlen;
|
||||||
|
memcpy(&this->ai_addr, rp->ai_addr, this->ai_addrlen);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
log_info("Connection failed:" << strerror(errno));
|
log_info("Connection failed:" << strerror(errno));
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#ifndef SOCKET_HANDLER_INCLUDED
|
#ifndef SOCKET_HANDLER_INCLUDED
|
||||||
# define SOCKET_HANDLER_INCLUDED
|
# define SOCKET_HANDLER_INCLUDED
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@@ -105,6 +109,13 @@ protected:
|
|||||||
* Port we are connected/connecting to
|
* Port we are connected/connecting to
|
||||||
*/
|
*/
|
||||||
std::string port;
|
std::string port;
|
||||||
|
/**
|
||||||
|
* Keep the details of the addrinfo the triggered a EINPROGRESS error when
|
||||||
|
* connect()ing to it, to reuse it directly when connect() is called
|
||||||
|
* again.
|
||||||
|
*/
|
||||||
|
struct sockaddr ai_addr;
|
||||||
|
socklen_t ai_addrlen;
|
||||||
|
|
||||||
bool connected;
|
bool connected;
|
||||||
bool connecting;
|
bool connecting;
|
||||||
|
|||||||
Reference in New Issue
Block a user