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

#read_connection_prefaceObject



67
68
69
70
71
72
73
74
75
# File 'lib/http/protocol/http2/framer.rb', line 67

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_frameObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/http/protocol/http2/framer.rb', line 77

def read_frame
  # 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)
  
  return frame
end

#read_headerObject



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

def read_header
  return Frame.parse_header(@io.read(9))
end

#write_connection_prefaceObject



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

def write_connection_preface
  @io.write(CONNECTION_PREFACE_MAGIC)
end

#write_frame(frame) ⇒ Object



93
94
95
96
97
# File 'lib/http/protocol/http2/framer.rb', line 93

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