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.

Direct Known Subclasses

BasicSocket, SSLSocket

Constant Summary collapse

WRAPPERS =
{}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.wrapped_klassObject (readonly)

Returns the value of attribute wrapped_klass.



50
51
52
# File 'lib/async/io/generic.rb', line 50

def wrapped_klass
  @wrapped_klass
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.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/async/io/generic.rb', line 64

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

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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/async/io/generic.rb', line 36

def wrap_blocking_method(new_name, method_name, invert: true)
	define_method(new_name) do |*args|
		async_send(method_name, *args)
	end
	
	if invert
		# We define the original _nonblock method to call the async variant. We ignore options.
		# define_method(method_name) do |*args, **options|
		# 	self.__send__(new_name, *args)
		# end
		def_delegators :@io, method_name
	end
end

.wraps(klass, *additional_methods) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/async/io/generic.rb', line 52

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

#readObject

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.read(512)


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

wrap_blocking_method :read, :read_nonblock

#writeObject

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.write("Hello World")


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

wrap_blocking_method :write, :write_nonblock