Class: Doh::DohSocket

Inherits:
Object show all
Defined in:
lib/doh/util/doh_socket.rb

Constant Summary collapse

@@socket_end_times =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port) ⇒ DohSocket

Returns a new instance of DohSocket.



12
13
14
15
16
# File 'lib/doh/util/doh_socket.rb', line 12

def initialize(server, port)
  #note -- this may hang -- should change to nonblocking also if you're going to use this
  @sock = TCPSocket.new(server, port)
  @timeout = 300
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/doh/util/doh_socket.rb', line 10

def timeout
  @timeout
end

Class Method Details

.timeout_block(duration) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/doh/util/doh_socket.rb', line 18

def self.timeout_block(duration)
  push_duration(duration)
  begin
    yield
  ensure
    pop_duration
  end
end

Instance Method Details

#timed_readlnObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/doh/util/doh_socket.rb', line 31

def timed_readln
  received = ''
  while (received =~ /$/) == received.size
    if (!select([@sock], nil, nil, time_left))
      raise TimeoutError.new('DohSocket timed_readln timed out')
    end
    received += @sock.recvfrom(65536)[0]
  end
  received
end

#write(str) ⇒ Object



27
28
29
# File 'lib/doh/util/doh_socket.rb', line 27

def write(str)
  @sock.write(str)
end