class Async::HTTP::Protocol::HTTP2

def receive_requests(task: Task.current, &block)

def receive_requests(task: Task.current, &block)
	# emits new streams opened by the client
	@controller.on(:stream) do |stream|
		request = Request.new
		request.version = self.version
		request.headers = Headers.new
		body = Body::Writable.new
		request.body = body
		
		stream.on(:headers) do |headers|
			headers.each do |key, value|
				if key == METHOD
					request.method = value
				elsif key == PATH
					request.path = value
				elsif key == AUTHORITY
					request.authority = value
				else
					request.headers[key] = value
				end
			end
		end
		
		stream.on(:data) do |chunk|
			# puts "Got request data: #{chunk.inspect}"
			body.write(chunk.to_s) unless chunk.empty?
		end
		
		stream.on(:close) do |error|
			if error
				body.stop(EOFError.new(error))
			end
		end
		
		stream.on(:half_close) do
			# The requirements for this to be in lock-step with other opertaions is minimal.
			# TODO consider putting this in it's own async task.
			begin
				# We are no longer receiving any more data frames:
				body.finish
				
				# Generate the response:
				response = yield request
				
				headers = {STATUS => response.status.to_s}
				headers.update(response.headers)
				
				if response.body.nil? or response.body.empty?
					stream.headers(headers, end_stream: true)
					response.body.read if response.body
				else
					stream.headers(headers, end_stream: false)
					
					response.body.each do |chunk|
						stream.data(chunk, end_stream: false)
					end
					
					stream.data("", end_stream: true)
				end
			rescue
				Async.logger.error(self) {$!}
				
				# Generating the response failed.
				stream.close(:internal_error)
			end
		end
	end
	
	start_connection
	@reader.wait
end