Class: Resolv::DNS::Requester::ConnectedUDP
- Inherits:
-
Resolv::DNS::Requester
- Object
- Resolv::DNS::Requester
- Resolv::DNS::Requester::ConnectedUDP
- Defined in:
- lib/resolv.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Sender
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(host, port = Port) ⇒ ConnectedUDP
constructor
A new instance of ConnectedUDP.
- #lazy_initialize ⇒ Object
- #recv_reply(readable_socks) ⇒ Object
- #sender(msg, data, host = @host, port = @port) ⇒ Object
Methods inherited from Resolv::DNS::Requester
Constructor Details
#initialize(host, port = Port) ⇒ ConnectedUDP
Returns a new instance of ConnectedUDP.
810 811 812 813 814 815 816 |
# File 'lib/resolv.rb', line 810 def initialize(host, port=Port) super() @host = host @port = port @mutex = Thread::Mutex.new @initialized = false end |
Instance Method Details
#close ⇒ Object
849 850 851 852 853 854 855 856 857 858 859 |
# File 'lib/resolv.rb', line 849 def close @mutex.synchronize do if @initialized super @senders.each_key {|from, id| DNS.free_request_id(@host, @port, id) } @initialized = false end end end |
#lazy_initialize ⇒ Object
818 819 820 821 822 823 824 825 826 827 828 829 830 |
# File 'lib/resolv.rb', line 818 def lazy_initialize @mutex.synchronize { next if @initialized @initialized = true is_ipv6 = @host.index(':') sock = UDPSocket.new(is_ipv6 ? Socket::AF_INET6 : Socket::AF_INET) @socks = [sock] sock.do_not_reverse_lookup = true DNS.bind_random_port(sock, is_ipv6 ? "::" : "0.0.0.0") sock.connect(@host, @port) } self end |
#recv_reply(readable_socks) ⇒ Object
832 833 834 835 836 |
# File 'lib/resolv.rb', line 832 def recv_reply(readable_socks) lazy_initialize reply = readable_socks[0].recv(UDPSize) return reply, nil end |
#sender(msg, data, host = @host, port = @port) ⇒ Object
838 839 840 841 842 843 844 845 846 847 |
# File 'lib/resolv.rb', line 838 def sender(msg, data, host=@host, port=@port) lazy_initialize 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] = [id].pack('n') return @senders[[nil,id]] = Sender.new(request, data, @socks[0]) end |