Skip to content

Commit

Permalink
Made sure we are taking the time AFTER sendto() returns. Also not cre…
Browse files Browse the repository at this point in the history
…ating a new socket for every ping send.
  • Loading branch information
jakedouglas committed Feb 13, 2009
1 parent d0b82bc commit d42c3ab
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/icmp4em/icmpv4.rb
Expand Up @@ -89,11 +89,7 @@ def receive(seq, time)
def ping_send
@seq = (@seq + 1) % 65536

socket = Socket.new(
Socket::PF_INET,
Socket::SOCK_RAW,
Socket::IPPROTO_ICMP
)
socket = self.class.recvsocket

if @bind_host
saddr = Socket.pack_sockaddr_in(0, @bind_host)
Expand All @@ -104,18 +100,18 @@ def ping_send
msg = [ICMP_ECHO, ICMP_SUBCODE, 0, @id, @seq, @data].pack("C2 n3 A*")
msg[2..3] = [generate_checksum(msg)].pack('n')

# Enqueue
# Enqueue so we can expire properly if there is an exception raised during #send
@waiting[seq] = Time.now

begin
# Fire it off
socket.send(msg, 0, @ipv4_sockaddr)
# Re-enqueue AFTER sendto() returns. This ensures we aren't adding latency if the socket blocks.
@waiting[seq] = Time.now
# Return sequence number to caller
@seq
rescue Exception => err
expire(@seq, err)
ensure
socket.close if socket
end
end

Expand Down

0 comments on commit d42c3ab

Please sign in to comment.