Class: RawLine::NonBlockingInput

Inherits:
Object
  • Object
show all
Defined in:
lib/rawline/non_blocking_input.rb

Constant Summary collapse

DEFAULT_WAIT_TIMEOUT_IN_SECONDS =
0.01

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ NonBlockingInput

Returns a new instance of NonBlockingInput.



7
8
9
10
# File 'lib/rawline/non_blocking_input.rb', line 7

def initialize(input)
  @input = input
  restore_default_timeout
end

Instance Attribute Details

#wait_timeout_in_secondsObject

Returns the value of attribute wait_timeout_in_seconds.



5
6
7
# File 'lib/rawline/non_blocking_input.rb', line 5

def wait_timeout_in_seconds
  @wait_timeout_in_seconds
end

Instance Method Details

#readObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rawline/non_blocking_input.rb', line 16

def read
  bytes = []
  begin
    file_descriptor_flags = @input.fcntl(Fcntl::F_GETFL, 0)
    loop do
      string = @input.read_nonblock(4096)
      bytes.concat string.bytes
    end
  rescue IO::WaitReadable
    # reset flags so O_NONBLOCK is turned off on the file descriptor
    # if it was turned on during the read_nonblock above
    retry if IO.select([@input], [], [], @wait_timeout_in_seconds)

    @input.fcntl(Fcntl::F_SETFL, file_descriptor_flags)
  end
  bytes
end

#restore_default_timeoutObject



12
13
14
# File 'lib/rawline/non_blocking_input.rb', line 12

def restore_default_timeout
  @wait_timeout_in_seconds = DEFAULT_WAIT_TIMEOUT_IN_SECONDS
end