// Conexion al servidor
	void handle_connect(const boost::system::error_code& error, tcp::resolver::iterator endpoint_iterator){
		debugOUT("handle_connect");
		if (!error){
			connected = true;
			debugOUT("Connected");

			boost::asio::async_read(socket_,
									boost::asio::buffer(receiveBuffer, transferAtLeast),
									boost::asio::transfer_at_least(transferAtLeast),
									boost::bind(&TCPClient::handle_read, this,
									boost::asio::placeholders::error,
									boost::asio::placeholders::bytes_transferred));

			debugOUT("async_read dispatched");
		}
		else if (endpoint_iterator != tcp::resolver::iterator()){
			debugOUT("Next Endpoint");
			// Conexion fallida, intentemos con el siguiente endpoint
			socket_.close();
			tcp::endpoint endpoint = *endpoint_iterator;
			
			socket_.async_connect(	endpoint,
									boost::bind(&TCPClient::handle_connect, this,
									boost::asio::placeholders::error, ++endpoint_iterator));
		}
		else{
			debugOUT("Error: " << error.message());
		}
	}
	// Resolucion de la direccion del servidor
	void handle_resolve(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator){
		debugOUT("handle_resolve");
		if (!err){
			// Intentamos conectarnos al primer endpoint en la lista
			tcp::endpoint endpoint = *endpoint_iterator;
			socket_.async_connect(	endpoint,
									boost::bind(&TCPClient::handle_connect, this,
									boost::asio::placeholders::error, ++endpoint_iterator));
		}
		else{
			debugOUT("Error: " << err.message());
		}
	}
Esempio n. 3
0
void TCPMethod::connectTo(boost::asio::ip::address_v4 address, tcp::socket & socket)
{
	uint64_t startTime = getMicroTime();
	socket.async_connect(
		tcp::endpoint(address, 22),
		[address, startTime] (const boost::system::error_code & error) {
			if(error == operation_aborted)
				;
			else if(error)
				connectionerr << "TCPMethod::connectTo(" << address << "): " << error.message() << "\n";
			else
			{
				uint64_t endTime = getMicroTime();
				if(startTime < endTime)
					Measurement::addMeasurement(address, "tcp", endTime - startTime);
			}
		}
	);
}
void resolve_handler(const boost::system::error_code &ec,
                     tcp::resolver::iterator it)
{
    if (!ec)
        tcp_socket.async_connect(*it, connect_handler);
}