Class: Async::HTTP::Protocol::HTTP2::Stream::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/protocol/http2/stream.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, body) ⇒ Output

Returns a new instance of Output.



58
59
60
61
62
63
# File 'lib/async/http/protocol/http2/stream.rb', line 58

def initialize(stream, body)
	@stream = stream
	@body = body
	
	@window_updated = Async::Condition.new
end

Class Method Details

.for(stream, body) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/async/http/protocol/http2/stream.rb', line 50

def self.for(stream, body)
	output = self.new(stream, body)
	
	output.start
	
	return output
end

Instance Method Details

#close(error = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/async/http/protocol/http2/stream.rb', line 98

def close(error = nil)
	if @stream
		if error
			@stream.close(error)
		else
			self.close_write
		end
		
		@stream = nil
	end
end

#close_writeObject



110
111
112
# File 'lib/async/http/protocol/http2/stream.rb', line 110

def close_write
	@stream.send_data(nil, ::Protocol::HTTP2::END_STREAM)
end

#start(parent: Task.current) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/async/http/protocol/http2/stream.rb', line 65

def start(parent: Task.current)
	if @body.respond_to?(:call)
		@task = parent.async(&self.method(:stream))
	else
		@task = parent.async(&self.method(:passthrough))
	end
end

#stop(error) ⇒ Object



73
74
75
76
77
78
# File 'lib/async/http/protocol/http2/stream.rb', line 73

def stop(error)
	# Ensure that invoking #close doesn't try to close the stream.
	@stream = nil
	
	@task&.stop
end

#window_updated(size) ⇒ Object



94
95
96
# File 'lib/async/http/protocol/http2/stream.rb', line 94

def window_updated(size)
	@window_updated.signal
end

#write(chunk) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/async/http/protocol/http2/stream.rb', line 80

def write(chunk)
	until chunk.empty?
		maximum_size = @stream.available_frame_size
		
		while maximum_size <= 0
			@window_updated.wait
			
			maximum_size = @stream.available_frame_size
		end
		
		break unless chunk = send_data(chunk, maximum_size)
	end
end