From d26dce11de72c6da640c570f179730cb89862377 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Mon, 12 Jan 2015 16:54:44 +0100 Subject: [PATCH] renamed IcmpPinger::handle_ping_done to handle_timeout; handle error_codes from timer async_wait --- src/host/pingscheduler.cpp | 23 ++++++++++++++++++----- src/host/pingscheduler.h | 2 +- src/icmp/icmppinger.cpp | 22 +++++++++++++++++++--- src/icmp/icmppinger.h | 2 +- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 65b5a3e..08f8c3d 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -130,10 +130,11 @@ void PingScheduler::start_pinging() { GlobalLogger.info() << "Delaying first ping by " << FirstDelay << "s"; (void) NextPingTimer.expires_from_now( seconds( FirstDelay ) ); - NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this ) ); + NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this, + boost::asio::placeholders::error ) ); } else - resolve_and_ping(); + resolve_and_ping(boost::system::error_code()); } @@ -142,8 +143,18 @@ void PingScheduler::start_pinging() * call ping; * If name resolution fails too often, reports this to HostAnalyzer */ -void PingScheduler::resolve_and_ping() +void PingScheduler::resolve_and_ping(const boost::system::error_code &error) { + if ( error ) + { + if ( error == boost::asio::error::operation_aborted ) + GlobalLogger.error() << "Timer for resolve_and_ping was cancelled! Stopping" << endl; + else + GlobalLogger.error() << "Received error " << error + << " waiting for resolve_and_ping! Stopping" << endl; + return; + } + bool ips_up_to_date = EverHadAnyIP && !Ping->expired_resolved_ip(); // determine if address resolution is required @@ -175,7 +186,8 @@ void PingScheduler::resolve_and_ping() HostAnalyzer.report_dns_resolution_failure(); } (void) NextAddressTimer.expires_from_now( seconds( AddressResolveIntervalInSec ) ); - NextAddressTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this ) ); + NextAddressTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this, + boost::asio::placeholders::error ) ); } } @@ -206,7 +218,8 @@ void PingScheduler::ping_done_handler( const bool ping_success ) // schedule next ping (void) NextPingTimer.expires_from_now( seconds( PingIntervalInSec ) ); - NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this ) ); + NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this, + boost::asio::placeholders::error ) ); } void PingScheduler::update_ping_interval() diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 1cdeaec..8cd0b59 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -72,7 +72,7 @@ private: // Methods // - void resolve_and_ping(); + void resolve_and_ping(const boost::system::error_code &error); bool resolve_address(); void force_address_resolution(); diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index 7e98d03..f0079e0 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -200,7 +200,7 @@ void IcmpPinger::schedule_timeout_echo_reply() TimeSent + seconds( EchoReplyTimeoutInSec ) ); IcmpPacketReceiveTimer.async_wait( - boost::bind( &IcmpPinger::handle_ping_done, this ) + boost::bind( &IcmpPinger::handle_timeout, this, boost::asio::placeholders::error ) ); } @@ -209,8 +209,23 @@ void IcmpPinger::schedule_timeout_echo_reply() * * @return void **/ -void IcmpPinger::handle_ping_done() +void IcmpPinger::handle_timeout(const boost::system::error_code& error) { + if (error) + { + if ( error == boost::asio::error::operation_aborted ) + { + if (! ReplyReceived) + GlobalLogger.notice() << "Timer waiting for ICMP echo reply was cancelled!" << endl; + // otherwise probably called by IcmpPacketReceiveTimer.cancel in handle_receive_icmp_packet! + } + else + GlobalLogger.notice() << "Error " << error << " waiting for ICMP echo reply!" << endl; + + // Still continue with rest of function, so PingStatus is updated and Callback executed + // when timer was cancelled + } + // Check ReplyReceived as the timer handler // is also called by Timer.cancel(); if ( !ReplyReceived ) @@ -240,7 +255,8 @@ void IcmpPinger::start_receive() // Waiting for a reply, We prepare the buffer to receive up to SOCKET_BUFFER_SIZE bytes Socket.async_receive( ReplyBuffer.prepare( SOCKET_BUFFER_SIZE ), - boost::bind( &IcmpPinger::handle_receive_icmp_packet, this, _1, _2 ) + boost::bind( &IcmpPinger::handle_receive_icmp_packet, this, boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred ) ); ReceiveHandlerInPlace = true; } diff --git a/src/icmp/icmppinger.h b/src/icmp/icmppinger.h index 715ab62..b9fb752 100644 --- a/src/icmp/icmppinger.h +++ b/src/icmp/icmppinger.h @@ -51,7 +51,7 @@ private: bool start_send(); bool send_echo_request( const IcmpPacketItem icmp_packet ); void schedule_timeout_echo_reply(); - void handle_ping_done(); + void handle_timeout(const boost::system::error_code &error); void start_receive(); void handle_receive_icmp_packet( const boost::system::error_code &error, -- 1.7.1