class Protocol::HTTP2::Client

connection preface handling, and push promise processing.
Manages client-side protocol semantics including stream ID allocation,
Represents an HTTP/2 client connection.

def create_push_promise_stream

@raises [ProtocolError] Always, as clients cannot initiate push promises.
Clients cannot create push promise streams.
def create_push_promise_stream
	raise ProtocolError, "Cannot create push promises from client!"
end

def initialize(framer)

@parameter framer [Framer] The frame handler for reading/writing HTTP/2 frames.
Initialize a new HTTP/2 client connection.
def initialize(framer)
	super(framer, 1)
end

def local_stream_id?(id)

@returns [bool] True if the stream ID is locally-initiated.
@parameter id [Integer] The stream ID to check.
Client streams have odd numbered IDs.
Check if the given stream ID represents a locally-initiated stream.
def local_stream_id?(id)
	id.odd?
end

def receive_push_promise(frame)

@returns [Array(Stream, Hash) | Nil] The promised stream and request headers, or nil if no associated stream.
@parameter frame [PushPromiseFrame] The push promise frame to process.
Process a push promise frame received from the server.
def receive_push_promise(frame)
	if frame.stream_id == 0
		raise ProtocolError, "Cannot receive headers for stream 0!"
	end
	
	if stream = @streams[frame.stream_id]
		# This is almost certainly invalid:
		promised_stream, request_headers = stream.receive_push_promise(frame)
		
		return promised_stream, request_headers
	end
end

def remote_stream_id?(id)

@returns [bool] True if the stream ID is remotely-initiated.
@parameter id [Integer] The stream ID to check.
Server streams have even numbered IDs.
Check if the given stream ID represents a remotely-initiated stream.
def remote_stream_id?(id)
	id.even?
end

def send_connection_preface(settings = [])

@yields Allows custom processing during preface exchange.
@raises [ProtocolError] If called when not in the new state.
@parameter settings [Array] Optional settings to send with the connection preface.
This must be called once when the connection is first established.
Send the HTTP/2 connection preface and initial settings.
def send_connection_preface(settings = [])
	if @state == :new
		@framer.write_connection_preface
		
		send_settings(settings)
		
		yield if block_given?
		
		read_frame do |frame|
			unless frame.is_a? SettingsFrame
				raise ProtocolError, "First frame must be #{SettingsFrame}, but got #{frame.class}"
			end
		end
	else
		raise ProtocolError, "Cannot send connection preface in state #{@state}"
	end
end

def valid_remote_stream_id?(stream_id)

@returns [bool] True if the stream ID is valid for remote initiation.
@parameter stream_id [Integer] The stream ID to validate.
Server-initiated streams must have even numbered IDs.
Check if the given stream ID is valid for remote initiation.
def valid_remote_stream_id?(stream_id)
	stream_id.even?
end