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.



55
56
57
58
59
60
# File 'lib/async/http/protocol/http2/stream.rb', line 55

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

Class Method Details

.for(stream, body) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/async/http/protocol/http2/stream.rb', line 47

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

Instance Method Details

#close(error = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/async/http/protocol/http2/stream.rb', line 95

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

#close_writeObject



107
108
109
# File 'lib/async/http/protocol/http2/stream.rb', line 107

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

#start(parent: Task.current) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/async/http/protocol/http2/stream.rb', line 62

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



70
71
72
73
74
75
# File 'lib/async/http/protocol/http2/stream.rb', line 70

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

#window_updated(size) ⇒ Object



91
92
93
# File 'lib/async/http/protocol/http2/stream.rb', line 91

def window_updated(size)
	@window_updated.signal
end

#write(chunk) ⇒ Object



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

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