From: Guilherme Maciel Ferreira Date: Thu, 10 Mar 2011 10:22:43 +0000 (+0100) Subject: Code improvements on BoostPinger X-Git-Tag: v1.0~147 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=040ffdf26c231b9f8c8e8856cff781caa06f1c01;p=pingcheck Code improvements on BoostPinger - included an assert to make sure a non-empty address is sent to the socket - better names for the handlers methods --- diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index d694101..f02db05 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -119,8 +119,11 @@ void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet ) ostream os( &request_buffer ); os << icmp_packet; - // Send the request. TimeSent = microsec_clock::universal_time(); + + // Send the request. + string dest_address_string = DestinationEndpoint.address().to_string(); + BOOST_ASSERT( !dest_address_string.empty() ); Socket.send_to( request_buffer.data(), DestinationEndpoint ); schedule_timeout_echo_reply(); @@ -131,10 +134,12 @@ void BoostPinger::schedule_timeout_echo_reply() // Wait up to N seconds for a reply. RepliesCount = 0; Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) ); - Timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) ); + Timer.async_wait( + boost::bind( &BoostPinger::handle_timeout_echo_reply, this ) + ); } -void BoostPinger::handle_timeout() +void BoostPinger::handle_timeout_echo_reply() { if ( RepliesCount == 0 ) cout << "Request timed out" << endl; @@ -158,11 +163,11 @@ void BoostPinger::start_receive() // Wait for a reply. We prepare the buffer to receive up to 64KB. Socket.async_receive( ReplyBuffer.prepare( 65536 ), - boost::bind( &BoostPinger::handle_receive, this, _2 ) + boost::bind( &BoostPinger::handle_receive_echo_reply, this, _2 ) ); } -void BoostPinger::handle_receive( const size_t &bytes_transferred ) +void BoostPinger::handle_receive_echo_reply( const size_t &bytes_transferred ) { // The actual number of bytes received is committed to the buffer so that we // can extract it using a std::istream object. @@ -178,7 +183,7 @@ void BoostPinger::handle_receive( const size_t &bytes_transferred ) // expected sequence number. if ( is && icmp_packet.match( IcmpHeader::EchoReply, get_identifier(), SequenceNumber ) ) { - // If this is the first reply, interrupt the five second timeout. + // If this is the first reply, interrupt the echo reply timeout. if ( RepliesCount == 0 ) Timer.cancel(); diff --git a/src/ping/boostpinger.h b/src/ping/boostpinger.h index d20f53b..c90dd46 100644 --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@ -32,11 +32,11 @@ private: IcmpPacket create_echo_request(); void send_echo_request( const IcmpPacket &icmp_packet ); void schedule_timeout_echo_reply(); - void handle_timeout(); + void handle_timeout_echo_reply(); void schedule_next_echo_request(); void start_receive(); - void handle_receive( const std::size_t &bytes_transferred ); + void handle_receive_echo_reply( const std::size_t &bytes_transferred ); void print_echo_reply( const IcmpPacket &icmp_packet, const std::size_t &bytes_transferred