MCPcopy Create free account
hub / github.com/boostorg/asio / handle_receive

Method handle_receive

example/cpp11/icmp/ping.cpp:84–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82 }
83
84 void handle_receive(std::size_t length)
85 {
86 // The actual number of bytes received is committed to the buffer so that we
87 // can extract it using a std::istream object.
88 reply_buffer_.commit(length);
89
90 // Decode the reply packet.
91 std::istream is(&reply_buffer_);
92 ipv4_header ipv4_hdr;
93 icmp_header icmp_hdr;
94 is >> ipv4_hdr >> icmp_hdr;
95
96 // We can receive all ICMP packets received by the host, so we need to
97 // filter out only the echo replies that match the our identifier and
98 // expected sequence number.
99 if (is && icmp_hdr.type() == icmp_header::echo_reply
100 && icmp_hdr.identifier() == get_identifier()
101 && icmp_hdr.sequence_number() == sequence_number_)
102 {
103 // If this is the first reply, interrupt the five second timeout.
104 if (num_replies_++ == 0)
105 timer_.cancel();
106
107 // Print out some information about the reply packet.
108 chrono::steady_clock::time_point now = chrono::steady_clock::now();
109 chrono::steady_clock::duration elapsed = now - time_sent_;
110 std::cout << length - ipv4_hdr.header_length()
111 << " bytes from " << ipv4_hdr.source_address()
112 << ": icmp_seq=" << icmp_hdr.sequence_number()
113 << ", ttl=" << ipv4_hdr.time_to_live()
114 << ", time="
115 << chrono::duration_cast<chrono::milliseconds>(elapsed).count()
116 << std::endl;
117 }
118
119 start_receive();
120 }
121
122 static unsigned short get_identifier()
123 {

Callers

nothing calls this directly

Calls 10

identifierMethod · 0.80
sequence_numberMethod · 0.80
header_lengthMethod · 0.80
source_addressMethod · 0.80
time_to_liveMethod · 0.80
nowFunction · 0.50
commitMethod · 0.45
typeMethod · 0.45
cancelMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected