Class: Async::IO::Generic

Inherits:
Wrapper
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/async/io/generic.rb

Overview

Represents an asynchronous IO within a reactor.

Constant Summary collapse

WRAPPERS =
{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.wrapped_klassObject (readonly)

Returns the value of attribute wrapped_klass.



72
73
74
# File 'lib/async/io/generic.rb', line 72

def wrapped_klass
  @wrapped_klass
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



208
209
210
# File 'lib/async/io/generic.rb', line 208

def timeout
  @timeout
end

Class Method Details

.wrap(*args) ⇒ Object

Instantiate a wrapped instance of the class, and optionally yield it to a given block, closing it afterwards.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/async/io/generic.rb', line 86

def wrap(*args)
	wrapper = self.new(@wrapped_klass.new(*args))
	
	return wrapper unless block_given?
	
	begin
		yield wrapper
	ensure
		wrapper.close
	end
end

.wrap_blocking_method(new_name, method_name, invert: true, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/async/io/generic.rb', line 55

def wrap_blocking_method(new_name, method_name, invert: true, &block)
	if block_given?
		define_method(new_name, &block)
	else
		define_method(new_name) do |*args|
			async_send(method_name, *args)
		end
	end
	
	if invert
		# We wrap the original _nonblock method, ignoring options.
		define_method(method_name) do |*args, exception: false|
			async_send(method_name, *args)
		end
	end
end

.wraps(klass, *additional_methods) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/async/io/generic.rb', line 74

def wraps(klass, *additional_methods)
	@wrapped_klass = klass
	WRAPPERS[klass] = self
	
	# These are methods implemented by the wrapped class, that we aren't overriding, that may be of interest:
	# fallback_methods = klass.instance_methods(false) - instance_methods
	# puts "Forwarding #{klass} methods #{fallback_methods} to @io"
	
	def_delegators :@io, *additional_methods
end

Instance Method Details

#<<(buffer) ⇒ Object



168
169
170
171
# File 'lib/async/io/generic.rb', line 168

def << buffer
	write(buffer)
	return self
end

#connected?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/async/io/generic.rb', line 204

def connected?
	!@io.closed?
end

#dupObject



173
174
175
176
177
# File 'lib/async/io/generic.rb', line 173

def dup
	super.tap do |copy|
		copy.timeout = self.timeout
	end
end

#nonblockObject



192
193
194
# File 'lib/async/io/generic.rb', line 192

def nonblock
	true
end

#nonblock=(value) ⇒ Object



196
197
198
# File 'lib/async/io/generic.rb', line 196

def nonblock= value
	true
end

#nonblock?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/async/io/generic.rb', line 200

def nonblock?
	true
end

#read(length = nil, buffer = nil) ⇒ Object

Read length bytes of data from the underlying I/O. If length is unspecified, read everything.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/async/io/generic.rb', line 107

def read(length = nil, buffer = nil)
	if buffer
		buffer.clear
	else
		buffer = String.new
	end
	
	if length
		return "" if length <= 0
		
		# Fast path:
		if buffer = self.sysread(length, buffer)
			
			# Slow path:
			while buffer.bytesize < length
				# Slow path:
				if chunk = self.sysread(length - buffer.bytesize)
					buffer << chunk
				else
					break
				end
			end
			
			return buffer
		else
			return nil
		end
	else
		buffer = self.sysread(BLOCK_SIZE, buffer)
		
		while chunk = self.sysread(BLOCK_SIZE)
			buffer << chunk
		end
		
		return buffer
	end
end

#sysreadObject

Read the specified number of bytes from the input stream. This is fast path. Invokes read_nonblock on the underlying io. If the operation would block, the current task is paused until the operation can succeed, at which point it's resumed and the operation is completed.

Examples:

data = io.sysread(512)


104
# File 'lib/async/io/generic.rb', line 104

wrap_blocking_method :sysread, :read_nonblock

#syswriteObject

Write entire buffer to output stream. This is fast path. Invokes write_nonblock on the underlying io. If the operation would block, the current task is paused until the operation can succeed, at which point it's resumed and the operation is completed.

Examples:

io.syswrite("Hello World")


148
# File 'lib/async/io/generic.rb', line 148

wrap_blocking_method :syswrite, :write_nonblock

#wait(timeout = self.timeout, mode = :read) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/async/io/generic.rb', line 179

def wait(timeout = self.timeout, mode = :read)
	case mode
	when :read
		wait_readable(timeout)
	when :write
		wait_writable(timeout)
	else
		wait_any(:rw, timeout)
	end
rescue TimeoutError
	return nil
end

#write(buffer) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/async/io/generic.rb', line 152

def write(buffer)
	# Fast path:
	written = self.syswrite(buffer)
	remaining = buffer.bytesize - written
	
	while remaining > 0
		# Slow path:
		length = self.syswrite(buffer.byteslice(written, remaining))
		
		remaining -= length
		written += length
	end
	
	return written
end