Class: Resolv::DNS::Requester::TCP

Inherits:
Requester
  • Object
show all
Defined in:
lib/resolv.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Sender

Instance Method Summary collapse

Constructor Details

#initialize(host, port = Port) ⇒ TCP

Returns a new instance of TCP.



985
986
987
988
989
990
991
992
993
# File 'lib/resolv.rb', line 985

def initialize(host, port=Port)
  super()
  @host = host
  @port = port
  sock = TCPSocket.new(@host, @port)
  @socks = [sock]
  @senders = {}
  @reusable = true
end

Instance Method Details

#close ⇒ Object



1038
1039
1040
1041
1042
1043
1044
# File 'lib/resolv.rb', line 1038

def close
  @reusable = false
  super
  @senders.each_key {|from,id|
    DNS.free_request_id(@host, @port, id)
  }
end

#recv_reply(readable_socks, timelimit = nil) ⇒ Object



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/resolv.rb', line 1003

def recv_reply(readable_socks, timelimit = nil)
  sock = readable_socks[0]
  len_data = read_exactly(sock, 2, timelimit)
  raise EOFError if len_data.nil? || len_data.bytesize != 2
  len = len_data.unpack('n')[0]
  reply = read_exactly(sock, len, timelimit)
  raise EOFError if reply.nil? || reply.bytesize != len
  @reusable = true
  return reply, nil
rescue EOFError, SystemCallError
  # Whatever the kernel reported, this socket cannot be trusted for
  # another frame.  In practice that is the peer closing or resetting;
  # the rest is rare enough that erring towards reconnecting is right.
  @reusable = false
  raise
end

#reusable? ⇒ Boolean

Returns:

  • (Boolean)


995
996
997
# File 'lib/resolv.rb', line 995

def reusable?
  @reusable
end

#send_failed ⇒ Object



999
1000
1001
# File 'lib/resolv.rb', line 999

def send_failed
  @reusable = false
end

#sender(msg, data, host = @host, port = @port) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/resolv.rb', line 1020

def sender(msg, data, host=@host, port=@port)
  unless host == @host && port == @port
    raise RequestError.new("host/port don't match: #{host}:#{port}")
  end
  id = DNS.allocate_request_id(@host, @port)
  request = msg.encode
  request[0,2] = [request.length, id].pack('nn')
  return @senders[[nil,id]] = Sender.new(request, data, @socks[0])
end