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.



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

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

Class Method Details

.for(stream, body) ⇒ Object



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

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

Instance Method Details

#close(error = nil) ⇒ Object



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

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

#close_writeObject



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

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

#start(parent: Task.current) ⇒ Object



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

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



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

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

#window_updated(size) ⇒ Object



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

def window_updated(size)
  @window_updated.signal
end

#write(chunk) ⇒ Object



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

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