Class: IO::Stream::Duplex
- Inherits:
-
Object
- Object
- IO::Stream::Duplex
- Defined in:
- lib/io/stream/duplex.rb
Overview
A low-level duplex IO adapter that composes distinct readable and writable endpoints.
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#close ⇒ Object
Close both endpoints.
-
#close_read ⇒ Object
Close the readable endpoint.
-
#close_write ⇒ Object
Close the writable endpoint.
-
#closed? ⇒ Boolean
Check whether both endpoints are closed.
-
#initialize(input, output = input) ⇒ Duplex
constructor
Initialize a duplex transport from separate readable and writable endpoints.
-
#read_nonblock(size, buffer, exception: false) ⇒ Object
Read data from the readable endpoint without blocking.
-
#readable? ⇒ Boolean
Check whether the readable endpoint may still produce data.
-
#timeout ⇒ Object
Return the maximum timeout across both endpoints.
-
#timeout=(duration) ⇒ Object
Update the timeout on both endpoints.
-
#to_io ⇒ Object
Return the underlying IO used to represent this duplex stream.
-
#wait_readable(duration = @timeout) ⇒ Object
Wait until the readable endpoint can be read.
-
#wait_writable(duration = @timeout) ⇒ Object
Wait until the writable endpoint can be written.
-
#write(buffer) ⇒ Object
Write data to the writable endpoint.
Constructor Details
#initialize(input, output = input) ⇒ Duplex
Initialize a duplex transport from separate readable and writable endpoints.
12 13 14 15 |
# File 'lib/io/stream/duplex.rb', line 12 def initialize(input, output = input) @input = input @output = output end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
17 18 19 |
# File 'lib/io/stream/duplex.rb', line 17 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
18 19 20 |
# File 'lib/io/stream/duplex.rb', line 18 def output @output end |
Instance Method Details
#close ⇒ Object
Close both endpoints.
74 75 76 77 |
# File 'lib/io/stream/duplex.rb', line 74 def close @output.close unless @output.closed? @input.close unless @input.closed? end |
#close_read ⇒ Object
Close the readable endpoint.
46 47 48 49 50 51 52 53 54 |
# File 'lib/io/stream/duplex.rb', line 46 def close_read return if @input.closed? if @input.respond_to?(:close_read) @input.close_read else @input.close end end |
#close_write ⇒ Object
Close the writable endpoint.
57 58 59 60 61 62 63 64 65 |
# File 'lib/io/stream/duplex.rb', line 57 def close_write return if @output.closed? if @output.respond_to?(:close_write) @output.close_write else @output.close end end |
#closed? ⇒ Boolean
Check whether both endpoints are closed.
41 42 43 |
# File 'lib/io/stream/duplex.rb', line 41 def closed? @input.closed? && @output.closed? end |
#read_nonblock(size, buffer, exception: false) ⇒ Object
Read data from the readable endpoint without blocking.
91 92 93 |
# File 'lib/io/stream/duplex.rb', line 91 def read_nonblock(size, buffer, exception: false) @input.read_nonblock(size, buffer, exception: exception) end |
#readable? ⇒ Boolean
Check whether the readable endpoint may still produce data.
69 70 71 |
# File 'lib/io/stream/duplex.rb', line 69 def readable? @input.readable? end |
#timeout ⇒ Object
Return the maximum timeout across both endpoints.
28 29 30 |
# File 'lib/io/stream/duplex.rb', line 28 def timeout [@input.timeout, @output.timeout].compact.max end |
#timeout=(duration) ⇒ Object
Update the timeout on both endpoints.
34 35 36 37 |
# File 'lib/io/stream/duplex.rb', line 34 def timeout=(duration) @input.timeout = duration @output.timeout = duration end |
#to_io ⇒ Object
Return the underlying IO used to represent this duplex stream.
22 23 24 |
# File 'lib/io/stream/duplex.rb', line 22 def to_io @input || @output end |
#wait_readable(duration = @timeout) ⇒ Object
Wait until the readable endpoint can be read.
98 99 100 |
# File 'lib/io/stream/duplex.rb', line 98 def wait_readable(duration = @timeout) @input.wait_readable(duration) end |
#wait_writable(duration = @timeout) ⇒ Object
Wait until the writable endpoint can be written.
105 106 107 |
# File 'lib/io/stream/duplex.rb', line 105 def wait_writable(duration = @timeout) @output.wait_writable(duration) end |
#write(buffer) ⇒ Object
Write data to the writable endpoint.
82 83 84 |
# File 'lib/io/stream/duplex.rb', line 82 def write(buffer) @output.write(buffer) end |