Class: IO::Stream::Duplex

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#inputObject (readonly)

Returns the value of attribute input.



17
18
19
# File 'lib/io/stream/duplex.rb', line 17

def input
  @input
end

#outputObject (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

#closeObject

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_readObject

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_writeObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


69
70
71
# File 'lib/io/stream/duplex.rb', line 69

def readable?
	@input.readable?
end

#timeoutObject

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_ioObject

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