congestion detection now working; also add case that if all IPs get timeout despite...
[pingcheck] / src / icmp / icmppinger.cpp
index c0415bf..2415c83 100644 (file)
@@ -16,6 +16,7 @@
 #include <boost/uuid/uuid.hpp>
 #include <boost/uuid/uuid_generators.hpp>
 #include <boost/foreach.hpp>
+#include <boost/system/system_error.hpp>
 
 #include <logfunc.hpp>
 
@@ -208,18 +209,47 @@ bool IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
         bytes_sent = PacketDistributor->get_socket()->send_to( data, DestinationEndpoint );
         if ( bytes_sent != buffer_size( data ) )
         {
-            GlobalLogger.error() << LogPrefix << "fail sending ping data."
-                                 << endl;
+            GlobalLogger.error() << LogPrefix << "fail sending ping data. Only"
+                                 << bytes_sent << " of " << buffer_size(data)
+                                 << " bytes were sent!" << endl;
         }
+
+        ReplyReceived = false;
+        schedule_timeout_echo_reply();
+    }
+    catch ( const boost::system::system_error &boost_err )
+    {
+        boost::system::error_code err_code = boost_err.code();
+        GlobalLogger.error() << LogPrefix << "fail sending ping data: "
+                 << boost_err.what() << " (code " << err_code.value()
+                 << ", category " << err_code.category().name() << ")" << endl;
+
+        // do not wait for timeout but fail at once
+        set_ping_status(PingStatus_SendFailed);
+        ReplyReceived = true;   // flag for handler to leave ping status as is
+        //handle_timeout( err_code );
+        handle_timeout( boost::system::error_code() );
     }
     catch ( const exception &ex )
     {
-        GlobalLogger.error() << LogPrefix << "fail sending ping data. "
+        GlobalLogger.error() << LogPrefix << "fail sending ping data: "
                              << ex.what() << endl;
+
+        // do not wait for timeout but fail at once
+        set_ping_status(PingStatus_SendFailed);
+        ReplyReceived = true;   // flag for handler to leave ping status as is
+        handle_timeout( boost::system::error_code() );
     }
+    catch ( ... )
+    {
+        GlobalLogger.error() << LogPrefix << "fail sending ping data: "
+                             << "Unknown exception" << endl;
 
-    ReplyReceived = false;
-    schedule_timeout_echo_reply();
+        // do not wait for timeout but fail at once
+        set_ping_status(PingStatus_SendFailed);
+        ReplyReceived = true;   // flag for handler to leave ping status as is
+        handle_timeout( boost::system::error_code() );
+    }
 
     return (bytes_sent > 0);
 }
@@ -262,6 +292,8 @@ void IcmpPinger::handle_timeout(const boost::system::error_code& error)
                 << " waiting for ICMP echo reply!" << endl;
             set_ping_status( PingStatus_FailureAsyncError );
         }
+        // could check here for more details if error is forwarded from
+        // send_echo_request
 
         // Still continue with rest of function, so PingStatus is updated and Callback executed
         //   when timer was cancelled
@@ -272,6 +304,7 @@ void IcmpPinger::handle_timeout(const boost::system::error_code& error)
 
         set_ping_status( PingStatus_FailureTimeout );
     }
+    // otherwise assume that ping status was set already
 
     // Call ping-done handler
     PingDoneCallback( PingerStatus, static_cast<long>(