do not wait for IcmpEchoReply if sending of IcmpEchoRequest has failed completely
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 12 Jan 2015 14:37:13 +0000 (15:37 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 12 Jan 2015 14:37:13 +0000 (15:37 +0100)
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h

index a4e05a6..bcf73a5 100644 (file)
@@ -118,8 +118,13 @@ void IcmpPinger::ping(
 
     set_destination_endpoint( destination_ip );
 
-    start_send();
-    start_receive();
+    if (start_send())
+        start_receive();
+    else
+        GlobalLogger.info()
+           << DestinationEndpoint.address().to_string()
+           << ": not scheduling receive since send failed." << endl;
+        // there might still be an old handler in place... cancel?
 }
 
 void IcmpPinger::set_destination_endpoint( const string &destination_ip )
@@ -131,7 +136,7 @@ void IcmpPinger::set_destination_endpoint( const string &destination_ip )
     DestinationEndpoint = icmp::endpoint( destination_address, port );
 }
 
-void IcmpPinger::start_send()
+bool IcmpPinger::start_send()
 {
     ++SequenceNumber;
 
@@ -139,10 +144,10 @@ void IcmpPinger::start_send()
             Protocol, Identifier, SequenceNumber );
 
     BOOST_ASSERT( PingerStatus == PingStatus_NotSent );
-    send_echo_request( icmp_packet_echo_request );
+    return send_echo_request( icmp_packet_echo_request );
 }
 
-void IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
+bool IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
 {
     boost::asio::streambuf request_buffer;
     ostream os( &request_buffer );
@@ -159,6 +164,7 @@ void IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
     BOOST_ASSERT( !dest_address_string.empty() );
 
     // Send the request
+    size_t bytes_sent = 0;
     try
     {
         GlobalLogger.info()
@@ -166,7 +172,7 @@ void IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
                     << ": sending ping" << endl;
         const_buffers_1 data = request_buffer.data();
         // Block until send the data
-        size_t bytes_sent = Socket.send_to( data, DestinationEndpoint );
+        bytes_sent = Socket.send_to( data, DestinationEndpoint );
         if ( bytes_sent != buffer_size( data ) )
         {
             GlobalLogger.error()
@@ -182,6 +188,8 @@ void IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
     }
 
     schedule_timeout_echo_reply();
+
+    return (bytes_sent > 0);
 }
 
 void IcmpPinger::schedule_timeout_echo_reply()
index ce0c0a7..715ab62 100644 (file)
@@ -48,8 +48,8 @@ public:
 private:
     void set_destination_endpoint( const std::string &destination_ip );
 
-    void start_send();
-    void send_echo_request( const IcmpPacketItem icmp_packet );
+    bool start_send();
+    bool send_echo_request( const IcmpPacketItem icmp_packet );
     void schedule_timeout_echo_reply();
     void handle_ping_done();