Save the addrinfo values for reuse on subsequent connect() call

This commit is contained in:
Florent Le Coz
2014-02-22 23:35:14 +01:00
parent 99aba5667d
commit 730cc6e1bd
2 changed files with 58 additions and 21 deletions
+34 -8
View File
@@ -8,6 +8,7 @@
#include <sys/types.h>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <cstring>
#include <fcntl.h>
@@ -42,14 +43,18 @@ void SocketHandler::init_socket()
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->port = port;
utils::ScopeGuard sg;
struct addrinfo* addr_res;
if (!this->connecting)
{
log_info("Trying to connect to " << address << ":" << port);
// Get the addrinfo from getaddrinfo, only if this is the first call
// of this function.
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = 0;
@@ -57,7 +62,6 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
struct addrinfo* addr_res;
const int res = ::getaddrinfo(address.c_str(), port.c_str(), &hints, &addr_res);
if (res != 0)
@@ -67,10 +71,29 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
this->on_connection_failed(gai_strerror(res));
return ;
}
// Make sure the alloced structure is always freed at the end of the
// function
utils::ScopeGuard sg([&addr_res](){ freeaddrinfo(addr_res); });
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)
{
@@ -87,6 +110,9 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
// is ready to be written on.
log_debug("Need to retry connecting later..." << strerror(errno));
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 ;
}
log_info("Connection failed:" << strerror(errno));
+11
View File
@@ -1,6 +1,10 @@
#ifndef SOCKET_HANDLER_INCLUDED
# define SOCKET_HANDLER_INCLUDED
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string>
#include <utility>
@@ -105,6 +109,13 @@ protected:
* Port we are connected/connecting to
*/
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 connecting;