module Async::HTTP::Protocol::HTTP2::Connection

def as_json(...)

@returns [String] A JSON-compatible representation.
def as_json(...)
	to_s
end

def close(error = nil)

Close the connection and stop the background reader.
def close(error = nil)
	# Ensure the reader task is stopped.
	if @reader
		reader = @reader
		@reader = nil
		reader.stop
	end
	
	super
end

def concurrency

@returns [Integer] The maximum number of concurrent streams allowed.
def concurrency
	self.maximum_concurrent_streams
end

def http1?

@returns [Boolean] Whether this is an HTTP/1 connection.
def http1?
	false
end

def http2?

@returns [Boolean] Whether this is an HTTP/2 connection.
def http2?
	true
end

def initialize(...)

Initialize the connection state.
def initialize(...)
	super
	
	@reader = nil
	
	# Writing multiple frames at the same time can cause odd problems if frames are only partially written. So we use a semaphore to ensure frames are written in their entirety.
	@write_frame_guard = Async::Semaphore.new(1)
end

def peer

@returns [Protocol::HTTP::Peer] The peer information for this connection.
def peer
	@peer ||= ::Protocol::HTTP::Peer.for(@stream.io)
end

def read_in_background(parent: Task.current)

Start a transient background task that reads frames from the connection.
def read_in_background(parent: Task.current)
	raise RuntimeError, "Connection is closed!" if closed?
	
	parent.async(transient: true) do |task|
		@reader = task
		
		task.annotate("#{version} reading data for #{self.class}.")
		
		# We don't need to defer stop here as this is already a transient task (ignores stop):
		begin
			while !self.closed?
				self.consume_window
				self.read_frame
			end
		rescue => error
			# Close with error.
		ensure
			# Don't call #close twice.
			if @reader
				@reader = nil
				
				self.close(error)
			end
		end
	end
end

def reusable?

@returns [Boolean] Whether the connection can be reused.
def reusable?
	!self.closed?
end

def start_connection

Start the background reader task if it is not already running.
def start_connection
	@reader || read_in_background
end

def synchronize(&block)

@yields {|...| ...} The block to execute while holding the write lock.
Synchronize write access to the connection.
def synchronize(&block)
	@write_frame_guard.acquire(&block)
end

def to_json(...)

@returns [String] A JSON string representation.
def to_json(...)
	as_json.to_json(...)
end

def to_s

@returns [String] A string representation of this connection.
def to_s
	"\#<#{self.class} #{@streams.count} active streams>"
end

def version

@returns [String] The HTTP version string.
def version
	VERSION
end

def viable?

Can we use this connection to make requests?
def viable?
	@stream&.readable?
end

def write_frame(frame)

Write a single frame, deferring stop until the frame is written.
def write_frame(frame)
	Task.current.defer_stop{super}
end

def write_frames

Write multiple frames, deferring stop until the frames are written.
def write_frames
	Task.current.defer_stop{super}
end