IO::StreamSourceIOStreamDuplex

class Duplex

A low-level duplex IO adapter that composes distinct readable and writable endpoints.

Definitions

def initialize(input, output = input)

Initialize a duplex transport from separate readable and writable endpoints.

Signature

parameter input IO

The readable endpoint.

parameter output IO

The writable endpoint.

Implementation

def initialize(input, output = input)
	@input = input
	@output = output
end

def to_io

Return the underlying IO used to represent this duplex stream.

Signature

returns IO

The readable endpoint if available, otherwise the writable endpoint.

Implementation

def to_io
	@input || @output
end

def timeout

Return the maximum timeout across both endpoints.

Signature

returns Numeric | Nil

The effective timeout, or nil if no timeout is configured.

Implementation

def timeout
	[@input.timeout, @output.timeout].compact.max
end

def timeout=(duration)

Update the timeout on both endpoints.

Signature

parameter duration Numeric | Nil

The timeout to assign.

Implementation

def timeout=(duration)
	@input.timeout = duration
	@output.timeout = duration
end

def closed?

Check whether both endpoints are closed.

Signature

returns Boolean

True if the duplex stream can no longer read or write.

Implementation

def closed?
	@input.closed? && @output.closed?
end

def close_read

Close the readable endpoint.

Implementation

def close_read
	return if @input.closed?
	
	if @input.respond_to?(:close_read)
		@input.close_read
	else
		@input.close
	end
end

def close_write

Close the writable endpoint.

Implementation

def close_write
	return if @output.closed?
	
	if @output.respond_to?(:close_write)
		@output.close_write
	else
		@output.close
	end
end

def readable?

Check whether the readable endpoint may still produce data.

Signature

returns Boolean

True if the readable endpoint reports it is readable.

Implementation

def readable?
	@input.readable?
end

def close

Close both endpoints.

Implementation

def close
	@output.close unless @output.closed?
	@input.close unless @input.closed?
end

def write(buffer)

Write data to the writable endpoint.

Signature

parameter buffer String

The data to write.

returns Integer

The number of bytes written.

Implementation

def write(buffer)
	@output.write(buffer)
end

def read_nonblock(size, buffer, exception: false)

Read data from the readable endpoint without blocking.

Signature

parameter size Integer

The maximum number of bytes to read.

parameter buffer String

The destination buffer.

parameter exception Boolean

Whether to raise on :wait_readable and EOF conditions.

returns String | Symbol | Nil

Data read from the endpoint, or the underlying non-blocking result.

Implementation

def read_nonblock(size, buffer, exception: false)
	@input.read_nonblock(size, buffer, exception: exception)
end

def wait_readable(duration = @timeout)

Wait until the readable endpoint can be read.

Signature

parameter duration Numeric | Nil

The maximum time to wait.

returns Boolean

True if the endpoint became readable.

Implementation

def wait_readable(duration = @timeout)
	@input.wait_readable(duration)
end

def wait_writable(duration = @timeout)

Wait until the writable endpoint can be written.

Signature

parameter duration Numeric | Nil

The maximum time to wait.

returns Boolean

True if the endpoint became writable.

Implementation

def wait_writable(duration = @timeout)
	@output.wait_writable(duration)
end