class H2::Server::Connection

handles reading data from the +@socket+ into the HTTP2::Server +@parser+, callbacks from the +@parser+, and closing of the +@socket+

Constants

PARSER_EVENTS

each +@parser+ event method is wrapped in a block to call a local instance method of the same name

Attributes

parser[R]

include FrameDebugger

server[R]

include FrameDebugger

socket[R]

include FrameDebugger

Public Class Methods

new(socket:, server: @socket = socket) { |self| ... } click to toggle source
# File lib/h2/server/connection.rb, line 24
def initialize socket:, server:
  @socket   = socket
  @server   = server
  @parser   = ::HTTP2::Server.new
  @attached = true

  # set a default stream handler that raises +NotImplementedError+
  #
  @each_stream = ->(s){ raise NotImplementedError }

  yield self if block_given?

  bind_events

  Logger.debug "new H2::Connection: #{self}"
end

Public Instance Methods

attached?() click to toggle source

is this connection still attached to the server reactor?

# File lib/h2/server/connection.rb, line 43
def attached?
  @attached
end
bind_events() click to toggle source

bind parser events to this instance

# File lib/h2/server/connection.rb, line 49
def bind_events
  PARSER_EVENTS.each do |e|
    on = "on_#{e}".to_sym
    @parser.on(e) { |x| __send__ on, x }
  end
end
close() click to toggle source

closes this connection's socket if attached

# File lib/h2/server/connection.rb, line 58
def close
  socket.close if socket && attached? && !closed?
end
closed?() click to toggle source

is this connection's socket closed?

# File lib/h2/server/connection.rb, line 64
def closed?
  socket.closed?
end
detach() click to toggle source

prevent this server reactor from handling this connection

# File lib/h2/server/connection.rb, line 70
def detach
  @attached = false
  self
end
each_stream(&block) click to toggle source

accessor for stream handler

# File lib/h2/server/connection.rb, line 77
def each_stream &block
  @each_stream = block if block_given?
  @each_stream
end
goaway() click to toggle source

queue a goaway frame

# File lib/h2/server/connection.rb, line 84
def goaway
  server.async.goaway self
end
read() click to toggle source

begins the read loop, handling all errors with a log message, backtrace, and closing the +@socket+

# File lib/h2/server/connection.rb, line 91
def read
  begin
    while attached? && !@socket.closed? && !(@socket.eof? rescue true)
      data = @socket.readpartial(4096)
      @parser << data
    end
    close

  rescue => e
    Logger.error "Exception: #{e.message} - closing socket"
    STDERR.puts e.backtrace if H2::Logger.level == ::Logger::DEBUG
    close

  end
end

Protected Instance Methods

on_frame(bytes) click to toggle source

called by +@parser+ with a binary frame to write to the +@socket+

# File lib/h2/server/connection.rb, line 113
def on_frame bytes
  @socket.write bytes
rescue IOError, Errno::EPIPE => e
  Logger.error e.message
  close
end
on_goaway(event) click to toggle source

the +@parser+ calls this when a goaway frame is received from the client

# File lib/h2/server/connection.rb, line 129
def on_goaway event
  close
end
on_stream(stream) click to toggle source

the +@parser+ calls this when a new stream has been initiated by the client

# File lib/h2/server/connection.rb, line 123
def on_stream stream
  H2::Server::Stream.new connection: self, stream: stream
end