Class: H2::Server::Connection
- Inherits:
-
Object
- Object
- H2::Server::Connection
- Defined in:
- lib/h2/server/connection.rb
Overview
handles reading data from the @socket into the HTTP2::Server
@parser, callbacks from the @parser, and closing of the @socket
Constant Summary collapse
- PARSER_EVENTS =
each @parser event method is wrapped in a block to call a local instance method of the same name
[ :frame, :stream, :goaway ]
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
include FrameDebugger.
-
#server ⇒ Object
readonly
include FrameDebugger.
-
#socket ⇒ Object
readonly
include FrameDebugger.
Instance Method Summary collapse
-
#attached? ⇒ Boolean
is this connection still attached to the server reactor?.
-
#bind_events ⇒ Object
bind parser events to this instance.
-
#close ⇒ Object
closes this connection’s socket if attached.
-
#closed? ⇒ Boolean
is this connection’s socket closed?.
-
#detach ⇒ Object
prevent this server reactor from handling this connection.
-
#each_stream(&block) ⇒ Object
accessor for stream handler.
-
#goaway ⇒ Object
queue a goaway frame.
-
#initialize(socket:, server:) {|_self| ... } ⇒ Connection
constructor
A new instance of Connection.
-
#read ⇒ Object
begins the read loop, handling all errors with a log message, backtrace, and closing the @socket.
Constructor Details
#initialize(socket:, server:) {|_self| ... } ⇒ Connection
Returns a new instance of Connection.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |
Instance Attribute Details
#parser ⇒ Object (readonly)
include FrameDebugger
22 23 24 |
# File 'lib/h2/server/connection.rb', line 22 def parser @parser end |
#server ⇒ Object (readonly)
include FrameDebugger
22 23 24 |
# File 'lib/h2/server/connection.rb', line 22 def server @server end |
#socket ⇒ Object (readonly)
include FrameDebugger
22 23 24 |
# File 'lib/h2/server/connection.rb', line 22 def socket @socket end |
Instance Method Details
#attached? ⇒ Boolean
is this connection still attached to the server reactor?
43 44 45 |
# File 'lib/h2/server/connection.rb', line 43 def attached? @attached end |
#bind_events ⇒ Object
bind parser events to this instance
49 50 51 52 53 54 |
# 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 ⇒ Object
closes this connection’s socket if attached
58 59 60 |
# File 'lib/h2/server/connection.rb', line 58 def close socket.close if socket && attached? && !closed? end |
#closed? ⇒ Boolean
is this connection’s socket closed?
64 65 66 |
# File 'lib/h2/server/connection.rb', line 64 def closed? socket.closed? end |
#detach ⇒ Object
prevent this server reactor from handling this connection
70 71 72 73 |
# File 'lib/h2/server/connection.rb', line 70 def detach @attached = false self end |
#each_stream(&block) ⇒ Object
accessor for stream handler
77 78 79 80 |
# File 'lib/h2/server/connection.rb', line 77 def each_stream &block @each_stream = block if block_given? @each_stream end |
#goaway ⇒ Object
queue a goaway frame
84 85 86 |
# File 'lib/h2/server/connection.rb', line 84 def goaway server.async.goaway self end |
#read ⇒ Object
begins the read loop, handling all errors with a log message, backtrace, and closing the @socket
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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.} - closing socket" STDERR.puts e.backtrace if H2::Logger.level == ::Logger::DEBUG close end end |