BoostPinger::BoostPinger(
boost::asio::io_service& io_service
) :
++ io_service( io_service ),
Resolver( io_service ),
DestinationEndpoint(),
Socket( io_service, icmp::v4() ),
void BoostPinger::ping( const Host &host )
{
++ BOOST_ASSERT( !host.get_address().empty() );
++ BOOST_ASSERT( ( 0 < host.get_interval() ) && ( host.get_interval() < UINT_MAX ) );
++
std::string destination = host.get_address();
icmp::resolver::query query( icmp::v4(), destination, "" );
DestinationEndpoint = *Resolver.resolve( query );
start_send();
start_receive();
++
++ io_service.run();
}
void BoostPinger::start_send()
IcmpBody body( "ping message" );
// Create an ICMP header for an echo request.
--
SequenceNumber++;
IcmpHeader::IcmpType type = IcmpHeader::EchoRequest;
uint8_t code = 0;
std::ostream os( &request_buffer );
os << echo_request << body;
++ int ping_times = SequenceNumber;
++ if ( ping_times < 4 )
++ {
++ send( request_buffer );
++ }
++ else
++ {
++ io_service.stop();
++ }
++}
++
++void BoostPinger::send( const boost::asio::streambuf& request_buffer )
++{
// Send the request.
TimeSent = posix_time::microsec_clock::universal_time();
Socket.send_to( request_buffer.data(), DestinationEndpoint );
);
}
--void BoostPinger::handle_receive( std::size_t length )
++void BoostPinger::handle_receive( const std::size_t length )
{
// The actual number of bytes received is committed to the buffer so that we
// can extract it using a std::istream object.
&& icmp_hdr.get_identifier() == get_identifier()
&& icmp_hdr.get_sequence_number() == SequenceNumber )
{
--
// If this is the first reply, interrupt the five second timeout.
if ( NumReplies == 0 )
Timer.cancel();
private:
void start_send();
--
++ void send( const boost::asio::streambuf& request_buffer );
void handle_timeout();
++
void start_receive();
-- void handle_receive( std::size_t length );
++ void handle_receive( const std::size_t length );
static uint16_t get_identifier();
private:
++ boost::asio::io_service& io_service;
icmp::resolver Resolver;
icmp::endpoint DestinationEndpoint;
icmp::socket Socket;