class Connection
Definitions
def maximum_frame_size
The size of a frame payload is limited by the maximum size that a receiver advertises in the SETTINGS_MAX_FRAME_SIZE setting.
Implementation
def maximum_frame_size
@remote_settings.maximum_frame_size
end
def maximum_concurrent_streams
The maximum number of concurrent streams that this connection can initiate. This is a setting that can be changed by the remote peer.
It is not the same as the number of streams that can be accepted by the connection. The number of streams that can be accepted is determined by the local settings, and the number of streams that can be initiated is determined by the remote settings.
Implementation
def maximum_concurrent_streams
@remote_settings.maximum_concurrent_streams
end
attr_accessor :state
Connection state (:new, :open, :closed).
attr_accessor :local_settings
Current settings value for local and peer
attr :local_window
Our window for receiving data. When we receive data, it reduces this window. If the window gets too small, we must send a window update.
attr :remote_window
Our window for sending data. When we send data, it reduces this window.
attr :remote_stream_id
The highest stream_id that has been successfully accepted by this connection.
def closed?
Whether the connection is effectively or actually closed.
Implementation
def closed?
@state == :closed || @framer.nil?
end
def close(error = nil)
Close the underlying framer and all streams.
Implementation
def close(error = nil)
# The underlying socket may already be closed by this point.
@streams.each_value{|stream| stream.close(error)}
@streams.clear
ensure
if @framer
@framer.close
@framer = nil
end
end
def next_stream_id
Streams are identified with an unsigned 31-bit integer. Streams initiated by a client MUST use odd-numbered stream identifiers; those initiated by the server MUST use even-numbered stream identifiers. A stream identifier of zero (0x0) is used for connection control messages; the stream identifier of zero cannot be used to establish a new stream.
Implementation
def next_stream_id
id = @local_stream_id
@local_stream_id += 2
return id
end
def ignore_frame?(frame)
6.8. GOAWAY There is an inherent race condition between an endpoint starting new streams and the remote sending a GOAWAY frame. To deal with this case, the GOAWAY contains the stream identifier of the last peer-initiated stream that was or might be processed on the sending endpoint in this connection. For instance, if the server sends a GOAWAY frame, the identified stream is the highest-numbered stream initiated by the client. Once sent, the sender will ignore frames sent on streams initiated by the receiver if the stream has an identifier higher than the included last stream identifier. Receivers of a GOAWAY frame MUST NOT open additional streams on the connection, although a new connection can be established for new streams.
Implementation
def ignore_frame?(frame)
if self.closed?
# puts "ignore_frame? #{frame.stream_id} -> #{valid_remote_stream_id?(frame.stream_id)} > #{@remote_stream_id}"
if valid_remote_stream_id?(frame.stream_id)
return frame.stream_id > @remote_stream_id
end
end
end
def read_frame
Reads one frame from the network and processes. Processing the frame updates the state of the connection and related streams. If the frame triggers an error, e.g. a protocol error, the connection will typically emit a goaway frame and re-raise the exception. You should continue processing frames until the underlying connection is closed.
Implementation
def read_frame
frame = @framer.read_frame(@local_settings.maximum_frame_size)
# puts "#{self.class} #{@state} read_frame: class=#{frame.class} stream_id=#{frame.stream_id} flags=#{frame.flags} length=#{frame.length} (remote_stream_id=#{@remote_stream_id})"
# puts "Windows: local_window=#{@local_window.inspect}; remote_window=#{@remote_window.inspect}"
return if ignore_frame?(frame)
yield frame if block_given?
frame.apply(self)
return frame
rescue GoawayError => error
# Go directly to jail. Do not pass go, do not collect $200.
raise
rescue ProtocolError => error
send_goaway(error.code || PROTOCOL_ERROR, error.message)
raise
rescue HPACK::Error => error
send_goaway(COMPRESSION_ERROR, error.message)
raise
end
def close!
Transition the connection into the closed state.
Implementation
def close!
@state = :closed
return self
end
def send_goaway(error_code = 0, message = "")
Tell the remote end that the connection is being shut down. If the error_code
is 0, this is a graceful shutdown. The other end of the connection should not make any new streams, but existing streams may be completed.
Implementation
def send_goaway(error_code = 0, message = "")
frame = GoawayFrame.new
frame.pack @remote_stream_id, error_code, message
write_frame(frame)
ensure
self.close!
end
def process_settings(frame)
In addition to changing the flow-control window for streams that are not yet active, a SETTINGS frame can alter the initial flow-control window size for streams with active flow-control windows (that is, streams in the "open" or "half-closed (remote)" state). When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST adjust the size of all stream flow-control windows that it maintains by the difference between the new value and the old value.
Implementation
def process_settings(frame)
if frame.acknowledgement?
# The remote end has confirmed the settings have been received:
changes = @local_settings.acknowledge
update_local_settings(changes)
return true
else
# The remote end is updating the settings, we reply with acknowledgement:
reply = frame.acknowledge
write_frame(reply)
changes = frame.unpack
@remote_settings.update(changes)
update_remote_settings(changes)
return false
end
end
def accept_stream(stream_id, &block)
Accept an incoming stream from the other side of the connnection. On the server side, we accept requests.
Implementation
def accept_stream(stream_id, &block)
unless valid_remote_stream_id?(stream_id)
raise ProtocolError, "Invalid stream id: #{stream_id}"
end
create_stream(stream_id, &block)
end
def accept_push_promise_stream(stream_id, &block)
Accept an incoming push promise from the other side of the connection. On the client side, we accept push promise streams. On the server side, existing streams create push promise streams.
Implementation
def accept_push_promise_stream(stream_id, &block)
accept_stream(stream_id, &block)
end
def create_stream(id = next_stream_id, &block)
Create a stream, defaults to an outgoing stream. On the client side, we create requests.
Implementation
def create_stream(id = next_stream_id, &block)
if @streams.key?(id)
raise ProtocolError, "Cannot create stream with id #{id}, already exists!"
end
if block_given?
return yield(self, id)
else
return Stream.create(self, id)
end
end
def receive_headers(frame)
On the server side, starts a new request.
Implementation
def receive_headers(frame)
stream_id = frame.stream_id
if stream_id.zero?
raise ProtocolError, "Cannot receive headers for stream 0!"
end
if stream = @streams[stream_id]
stream.receive_headers(frame)
else
if stream_id <= @remote_stream_id
raise ProtocolError, "Invalid stream id: #{stream_id} <= #{@remote_stream_id}!"
end
# We need to validate that we have less streams than the specified maximum:
if @streams.size < @local_settings.maximum_concurrent_streams
stream = accept_stream(stream_id)
@remote_stream_id = stream_id
stream.receive_headers(frame)
else
raise ProtocolError, "Exceeded maximum concurrent streams"
end
end
end
def receive_priority(frame)
Sets the priority for an incoming stream.
Implementation
def receive_priority(frame)
if dependency = @dependencies[frame.stream_id]
dependency.receive_priority(frame)
elsif idle_stream_id?(frame.stream_id)
Dependency.create(self, frame.stream_id, frame.unpack)
end
end
def closed_stream_id?(id)
This is only valid if the stream doesn't exist in @streams
.
Implementation
def closed_stream_id?(id)
if id.zero?
# The connection "stream id" can never be closed:
false
else
!idle_stream_id?(id)
end
end
def consume_window(size = self.available_size)
Traverse active streams in order of priority and allow them to consume the available flow-control window.
Implementation
def consume_window(size = self.available_size)
# Return if there is no window to consume:
return unless size > 0
# Console.info(self) do |buffer|
# @dependencies.each do |id, dependency|
# buffer.puts "- #{dependency}"
# end
#
# buffer.puts
#
# @dependency.print_hierarchy(buffer)
# end
@dependency.consume_window(size)
end