Esempio n. 1
0
void connectSocket(boost::asio::io_service& io_service, tcp::socket& socket, const std::string& url, const std::string &port) {
	boost::system::error_code error = boost::asio::error::host_not_found;

	// connect
	if (isIpAddress(url.c_str())) {
		boost::asio::ip::address addr = boost::asio::ip::address::from_string(url);
		boost::asio::ip::tcp::endpoint endpoint = boost::asio::ip::tcp::endpoint(addr, lexical_cast<int>(port));
		socket.close();
		socket.connect(endpoint);
	}
	else {
		tcp::resolver::query query(url, "http");
		tcp::resolver resolver(io_service);
		tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
		tcp::resolver::iterator end;

		while (error && endpoint_iterator != end)
		{
			socket.close();
			socket.connect(*endpoint_iterator++, error);
		}
		if (error)
			throw boost::system::system_error(error);
	}
}
	// 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());
		}
	}
Esempio n. 3
0
void
controller::listen_for_command()
{
    LOG(INFO) << "Listening for commands...";
    boost::system::error_code error;
    asio::streambuf buffer;

    try
    {
        uint16_t port = global::config()->get("controller.port", 3103);
        tcp::acceptor acceptor(this->io_service_,
                               tcp::endpoint(tcp::v4(), port));

        while (true)
        {
            acceptor.accept(controll_socket_);
            while (true)
            {
                asio::read_until(controll_socket_, buffer, regex("\n"), error);
                if (error)
                    break;

                std::istream is(&buffer);
                string line;
                std::getline(is, line);
                process_command(line);
            }
            controll_socket_.close();
        }
    }
    catch (std::exception& e)
    {
        LOG(ERROR) << e.what();
    }
}
Esempio n. 4
0
    void accept()
    {
        // Clean up any previous connection.
        boost::beast::error_code ec;
        socket_.close(ec);
        buffer_.consume(buffer_.size());

        acceptor_.async_accept(
            socket_,
            [this](boost::beast::error_code ec)
            {
                if (ec)
                {
                    accept();
                }
                else
                {
                    // Request must be fully processed within 60 seconds.
                    request_deadline_.expires_after(
                        std::chrono::seconds(60));

                    read_request();
                }
            });
    }
Esempio n. 5
0
		~Client()
		{
			try {
				tcp_socket->close();
			} catch (bs::system_error e) {}
			
			delete tcp_socket;
		}
Esempio n. 6
0
 static void CloseConnection(tcp::acceptor &_acceptor, tcp::socket &_socket) {
   log_info(LOG_PREFIX, "disconnecting");
   if (_acceptor.is_open()) {
     _acceptor.close();
   }
   if (_socket.is_open()) {
     _socket.close();
   }
 }
Esempio n. 7
0
static void really_disconnect(tcp::socket& socket, Logger* logger)
{
	try {
		socket.shutdown(socket_base::shutdown_both);
		socket.close();
	} catch(const system::system_error& e) {
		// This might be totally okay. Depends on the error.
		logger->warning(format("Failed to shut down the socket. This shouldn't happen! (%1%)") % e.what());
	}
}
Esempio n. 8
0
File: cmd.cpp Progetto: snyh/WHMon
void CMD::reply(tcp::socket& s, std::string message)
{
  try {
      boost::asio::write(s, boost::asio::buffer(
						str(boost::format("%1% %2%\r\n") % id % message)
					       )
			);
      s.close();
  } catch (boost::system::system_error &e) {
      std::cerr <<  "Exception:" << e.what() << std::endl;
  }
}
Esempio n. 9
0
inline bool TCPServerSession::Close()
{
	try
	{
		if (!mSocket.is_open()) return false;
		mSocket.shutdown(socket_base::shutdown_both);
		mSocket.close();
		return true;
	}
	catch (...)
	{
		return false;
	}
}
Esempio n. 10
0
    void check_deadline()
    {
        // The deadline may have moved, so check it has really passed.
        if (request_deadline_.expiry() <= std::chrono::steady_clock::now())
        {
            // Close socket to cancel any outstanding operation.
            boost::beast::error_code ec;
            socket_.close();

            // Sleep indefinitely until we're given a new deadline.
            request_deadline_.expires_at(
                std::chrono::steady_clock::time_point::max());
        }

        request_deadline_.async_wait(
            [this](boost::beast::error_code)
            {
                check_deadline();
            });
    }
Esempio n. 11
0
void L2Socket::Disconnect(tcp::socket& _socket,size_t _agentOID)
{
    //return;
    if( _agentOID != -1 )
    {
        UIntAry::iterator _it = find( agentOIDAry.begin(),agentOIDAry.end(),_agentOID );
        
        if( _it != agentOIDAry.end() )
        {
            shared_ptr<L2Agent> agt = L2_FIND_OBJ(L2Agent, _agentOID);
            agt->Disconnect();
            agentOIDAry.erase( _it );
            
            LOG( "Disconnect !!! ip: "+_socket.remote_endpoint().address().to_string()+" port: "+ _socket.remote_endpoint().port() );
        }
    }
    else
        LOG( "Disconnect !!!" );
    _socket.close();
    SocketAry::iterator _it = find( sockAry.begin(),sockAry.end(),&_socket );
    
    if( _it != sockAry.end() )  sockAry.erase( _it );
}
Esempio n. 12
0
	NetworkDevice(Stats &stats, const char *address)
	: Device(stats), socket(io_service)
	{
		stringstream portstr;
		portstr << SERVER_PORT;

		tcp::resolver resolver(io_service);
		tcp::resolver::query query(address, portstr.str());
		tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
		tcp::resolver::iterator end;

		boost::system::error_code error = boost::asio::error::host_not_found;
		while(error && endpoint_iterator != end)
		{
			socket.close();
			socket.connect(*endpoint_iterator++, error);
		}

		if(error)
			throw boost::system::system_error(error);

		mem_counter = 0;
	}
Esempio n. 13
0
 void do_close() {
     clientSocket.close();
 };
Esempio n. 14
0
 void do_close()
 {
     socket_.close();
 }
Esempio n. 15
0
void
session::perform(tcp::socket& s, asio::yield_context) {
    s.close();
}
	void DoClose(){
		socket_.close();
	}
Esempio n. 17
0
File: http.cpp Progetto: zmij/gapp
	~SocketConnection()
	{
		socket.close();
	}