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
include FrameDebugger
include FrameDebugger
include FrameDebugger
Public Class Methods
# 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
is this connection still attached to the server reactor?
# File lib/h2/server/connection.rb, line 43 def attached? @attached end
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
closes this connection's socket if attached
# File lib/h2/server/connection.rb, line 58 def close socket.close if socket && attached? && !closed? end
is this connection's socket closed?
# File lib/h2/server/connection.rb, line 64 def closed? socket.closed? end
prevent this server reactor from handling this connection
# File lib/h2/server/connection.rb, line 70 def detach @attached = false self end
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
queue a goaway frame
# File lib/h2/server/connection.rb, line 84 def goaway server.async.goaway self end
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
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
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
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