Class: HTTP::Protocol::HTTP2::Framer

Inherits:
Object
  • Object
show all
Defined in:
lib/http/protocol/http2/framer.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, frames = FRAMES) ⇒ Framer

Returns a new instance of Framer.



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

def initialize(io, frames = FRAMES)
	@io = io
	@frames = frames
	
	@buffer = String.new.b
end

Instance Method Details

#closeObject



63
64
65
# File 'lib/http/protocol/http2/framer.rb', line 63

def close
	@io.close
end

#read_connection_prefaceObject



71
72
73
74
75
76
77
78
79
# File 'lib/http/protocol/http2/framer.rb', line 71

def read_connection_preface
	string = @io.read(CONNECTION_PREFACE_MAGIC.bytesize)
	
	unless string == CONNECTION_PREFACE_MAGIC
		raise ProtocolError, "Invalid connection preface: #{string.inspect}"
	end
	
	return string
end

#read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/http/protocol/http2/framer.rb', line 81

def read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE)
	# Read the header:
	length, type, flags, stream_id = read_header
	
	# puts "framer: read_frame #{type} #{length}"
	
	# Allocate the frame:
	klass = @frames[type] || Frame
	frame = klass.new(stream_id, flags, type, length)
	
	# Read the payload:
	frame.read(@io, maximum_frame_size)
	
	return frame
end

#read_headerObject



104
105
106
107
108
109
110
111
# File 'lib/http/protocol/http2/framer.rb', line 104

def read_header
	if buffer = @io.read(9)
		return Frame.parse_header(buffer)
	else
		# TODO: Is this necessary? I thought the IO would throw this.
		raise EOFError
	end
end

#write_connection_prefaceObject



67
68
69
# File 'lib/http/protocol/http2/framer.rb', line 67

def write_connection_preface
	@io.write(CONNECTION_PREFACE_MAGIC)
end

#write_frame(frame) ⇒ Object



97
98
99
100
101
102
# File 'lib/http/protocol/http2/framer.rb', line 97

def write_frame(frame)
	# puts "framer: write_frame #{frame.inspect}"
	frame.write(@io)
	
	@io.flush
end