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 |
# File 'lib/h2/server/connection.rb', line 24 def initialize socket:, server: @socket = socket @server = server @parser = ::HTTP2::Server.new @attached = true yield self if block_given? bind_events Logger.debug "new H2::Connection: #{self}" if H2.verbose? 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?
39 40 41 |
# File 'lib/h2/server/connection.rb', line 39 def attached? @attached end |
#bind_events ⇒ Object
bind parser events to this instance
45 46 47 48 49 50 |
# File 'lib/h2/server/connection.rb', line 45 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
54 55 56 |
# File 'lib/h2/server/connection.rb', line 54 def close socket.close if socket && attached? end |
#closed? ⇒ Boolean
is this connection’s socket closed?
60 61 62 |
# File 'lib/h2/server/connection.rb', line 60 def closed? socket.closed? end |
#detach ⇒ Object
prevent this server reactor from handling this connection
66 67 68 69 |
# File 'lib/h2/server/connection.rb', line 66 def detach @attached = false self end |
#each_stream(&block) ⇒ Object
accessor for stream handler
73 74 75 76 |
# File 'lib/h2/server/connection.rb', line 73 def each_stream &block @each_stream = block if block_given? @each_stream end |
#goaway ⇒ Object
queue a goaway frame
80 81 82 |
# File 'lib/h2/server/connection.rb', line 80 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/h2/server/connection.rb', line 87 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.verbose? close end end |