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.



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

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

Class Method Details

.for(stream, body) ⇒ Object



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

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

Instance Method Details

#close(error = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/async/http/protocol/http2/stream.rb', line 110

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

#close_writeObject



122
123
124
# File 'lib/async/http/protocol/http2/stream.rb', line 122

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

#start(parent: Task.current) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/async/http/protocol/http2/stream.rb', line 77

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



85
86
87
88
89
90
# File 'lib/async/http/protocol/http2/stream.rb', line 85

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

#window_updated(size) ⇒ Object



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

def window_updated(size)
  @window_updated.signal
end

#write(chunk) ⇒ Object



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

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